a
    DfF                     @  sR  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ ddlZddlmZ dd	lmZmZ d
dlmZmZ erddlmZ e ZG dd deZ G dd de Z!G dd de!Z"G dd de!Z#G dd de!Z$G dd de!Z%G dd de!Z&G dd de!Z'G dd de ZG dd  d e!Z(dS )!zP
Contains Image panes including renderers for PNG, SVG, GIF and JPG
file types.
    )annotationsN)BytesIO)PurePath)TYPE_CHECKINGAnyClassVarDictListMappingTuple   )PDF)isfileisurl   )HTMLBasePaneescape)Modelc                      s   e Zd ZU ejdddZded< dZded< d	diZd
ed< g dZ	ded< dZ
d! fdd	Z fddZedddddZdddddZddddd Z  ZS )"FileBaseFz-
        Whether to embed the file as base64.defaultdocClassVar[str]filetypeNz ClassVar[None | Tuple[str, ...]]_extensionsembed"ClassVar[Mapping[str, str | None]]_rename)r   objectstyleswidthheightClassVar[List[str]]_rerender_paramsTc                   s,   t |trt|}t jf d|i| d S )Nr   )
isinstancer   strsuper__init__)selfr   params	__class__ ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/pane/image.pyr'   -   s    
zFileBase.__init__c                   s4   t |tr$tt| j d|dt | d S )Nz9 pane cannot parse string that is not a filename or URL (z).r$   r%   
ValueErrortype__name__r&   _type_errorr(   r   r*   r,   r-   r2   2   s    
zFileBase._type_errorr   float | bool | Noneobjreturnc                   s   | j dd }| jp|f}t d| dr2dS t trHt   t trt |r`dS t	 fdd|D rzdS t d rd	S n0t t
rz|   W dS  ty   Y d
S 0 t drdS d
S )N+r   _repr__g333333?Tc                 3  s"   | ]}   d | V  qdS ).N)lowerendswith).0extr6   r,   r-   	<genexpr>E       z#FileBase.applies.<locals>.<genexpr>g        Fread)r   splitr   hasattrr$   r   r%   absoluter   anybytes	_imgshape	Exception)clsr6   r   extsr,   r@   r-   applies:   s,    






zFileBase.applieszstr | bytesr%   )datar7   c                 C  s6   t |ts|d}t|d}d| j d| S )Nutf-8zdata:image/z;base64,)r$   rH   encodebase64	b64encodedecoder   )r(   rN   b64r,   r,   r-   _b64S   s    

zFileBase._b64zbytes | Nonec                   sX  j dd }t d| dr6t d| d S t ttfrt rt d}|	 W  d    S 1 st0    Y  nBt t
r S t drt dr d  	 S t d sd S dd	lm} |jr8dd
lm}m} | r| 	 S ddlm  fdd}t| }t| |tj ndd l}	|	j dd}
|
jS d S )Nr8   r   r9   r:   rbrC   seekr   )state)
_IN_WORKERfetch_binary)pyfetchc                     s    I d H   I d H _d S N)rH   r   r,   r6   r[   r(   r,   r-   replace_contentq   s    z'FileBase._data.<locals>.replace_contentGET)urlmethod)r   rD   rE   getattrr$   r%   r   r   openrC   rH   rW   r   Zio.staterX   Z_is_pyodideZ
io.pyodiderY   rZ   Zpyodide.httpr[   asyncioZcreate_task_tasksaddZadd_done_callbackdiscardrequestsrequestcontent)r(   r6   r   frX   rY   rZ   r^   Ztaskrh   rr,   r]   r-   _dataY   s8    (





zFileBase._data)N)r1   
__module____qualname__paramBooleanr   __annotations__r   r   r#   Z_FileBase__abstractr'   r2   classmethodrM   rU   rm   __classcell__r,   r,   r*   r-   r      s   
r   c                   @  s   e Zd ZU dZejdddZejdddZejdddZ	ejdddZ
g d	Zd
ed< dddddZded< ddiZded< dZedd Zd ddddddZdd ZdddddZdS )!	ImageBasea_  
    Encodes an image as base64 and wraps it in a Bokeh Div model.
    This is an abstract base class that needs the image type
    to be specified and specific code for determining the image shape.

    The filetype determines the filetype, extension, and MIME type for
    this image. Each image type (png,jpg,gif) has a base class that
    supports anything with a `_repr_X_` method (where X is `png`,
    `gif`, etc.), a local file with the given file extension, or a
    HTTP(S) url with the given extension.  Subclasses of each type can
    provide their own way of obtaining or generating a PNG.
    Nzv
        alt text to add to the image tag. The alt text is shown when a
        user cannot load or display the image.r   z(
        Optional caption for the image.TzT
        Whether the aspect ratio of the image should be forced to be
        equal.zW
        A link URL to make the image clickable and link to some other
        website.)alt_textcaptionlink_urlr   r   r   r    r!   r"   r#   )rv   fixed_aspectrx   rw   r   r   r   z!'<img src="' + value + '"></img>'_target_transformsc                 C  s   t dS )z'Calculate and return image width,heightN)NotImplementedError)rK   rN   r,   r,   r-   rI      s    zImageBase._imgshaper%   z
str | None)srcr    r!   c              
   C  s   | j rd| j nd}|r&d| dnd}|r:d| dnd}| jrHdnd}d| d	| d
| d| | d
}| jrd| j d| d}| jrd| d| j d}t|S )Nzalt=  width: ;	 height: containfill
<img src="z" z7 style="max-width: 100%; max-height: 100%; object-fit: "></img>z	<a href="z" target="_blank">z</a>z<figure>z<figcaption>z</figcaption></figure>)rv   ry   rx   rw   r   )r(   r|   r    r!   Zalt
object_fithtmlr,   r,   r-   _format_html   s    "zImageBase._format_htmlc                 C  s   | j }|dv r6|r| dnd|r,| dnd }}n|dkrN| jsNd\}}nj|dkrv| jsvd|rl| dnd }}nB|dkr| js|r| dndd }}n|d	v rd
\}}nd\}}||fS )N)fixedNZpxautoZstretch_both)100%r   Zstretch_widthr   stretch_height)Zscale_heightr   )r   r   )r   r   )Zsizing_modery   )r(   r    r!   Zsmodewhr,   r,   r-   	_img_dims   s    

zImageBase._img_dimsr   Dict[str, Any]r5   c           	      C  s  | j st|st|ttfs(| |}n(| | j| j\}}t	| 
|||dS |d u rbt	ddS t|tsvt|}| |\}}| jd ur| jd u rt| j| | }n| j}| j}n"| jd urt| j| | }| j}| |}| ||\}}| 
|||}t	|||dS )Nr   <img></img>)r    r!   r   )r   r   r$   r%   r   rm   r   r    r!   dictr   rH   rQ   	b64decoderI   intrU   )	r(   r6   rN   r   r   r    r!   r|   r   r,   r,   r-   _transform_object   s*    






zImageBase._transform_object)NN)r1   rn   ro   __doc__rp   Stringrv   rw   rq   ry   rx   r#   rr   r   rz   Z_ImageBase__abstractrs   rI   r   r   r   r,   r,   r,   r-   ru   |   s"   

 ru   c                   @  s4   e Zd ZdZedddddZddddd	Zd
S )Imageae  
    The `Image` pane embeds any known image format in a panel if
    provided a local path, bytes or remote image link.

    :Example:

    >>> Image(
    ...     'https://panel.holoviz.org/_static/logo_horizontal.png',
    ...     alt_text='The Panel Logo',
    ...     link_url='https://panel.holoviz.org/index.html',
    ...     width=500
    ... )
    r   r4   r5   c                 C  sn   g }t t D ]F}|tu r q||}t|tr@|r@|  S t|tt	fr|
| q|rjt|d S dS )NF)rp   concrete_descendentsru   valuesr   rM   r$   boolfloatr   appendsorted)rK   r6   Zprecedencesimg_clsrM   r,   r,   r-   rM      s    
zImage.appliesr   c                 C  s`   dd | j   D }t t D ]0}|tur&||r&||fi ||  S q&ddiS )Nc                 S  s   i | ]\}}|d vr||qS ))namer   r,   )r>   kvr,   r,   r-   
<dictcomp>	  s   z+Image._transform_object.<locals>.<dictcomp>r   r   )rp   r   itemsr   ru   r   rM   r   )r(   r6   r)   r   r,   r,   r-   r     s    zImage._transform_objectN)r1   rn   ro   r   rs   rM   r   r,   r,   r,   r-   r      s   r   c                   @  s*   e Zd ZU dZdZded< edd ZdS )PNGa  
    The `PNG` pane embeds a .png image file in a panel if provided a local
    path, or will link to a remote image if provided a URL.

    Reference: https://panel.holoviz.org/reference/panes/PNG.html

    :Example:

    >>> PNG(
    ...     'https://panel.holoviz.org/_static/logo_horizontal.png',
    ...     alt_text='The Panel Logo',
    ...     link_url='https://panel.holoviz.org/index.html',
    ...     width=500
    ... )
    Zpngr   r   c                 C  s0   dd l }|d|dd \}}t|t|fS )Nr   z>LL      structunpackr   rK   rN   r   r   r   r,   r,   r-   rI   &  s    zPNG._imgshapeNr1   rn   ro   r   r   rr   rs   rI   r,   r,   r,   r-   r     s   
r   c                   @  s*   e Zd ZU dZdZded< edd ZdS )GIFa  
    The `GIF` pane embeds a .gif image file in a panel if provided a local
    path, or will link to a remote image if provided a URL.

    Reference: https://panel.holoviz.org/reference/panes/GIF.html

    :Example:

    >>> GIF(
    ...     'https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif',
    ...     alt_text='A loading spinner',
    ...     link_url='https://commons.wikimedia.org/wiki/File:Loading_icon.gif',
    ...     width=500
    ... )
    Zgifr   r   c                 C  s0   dd l }|d|dd \}}t|t|fS )Nr   z<HH   
   r   r   r,   r,   r-   rI   @  s    zGIF._imgshapeNr   r,   r,   r,   r-   r   -  s   
r   c                   @  s*   e Zd ZU dZdZded< edd ZdS )ICOa  
    The `ICO` pane embeds an .ico image file in a panel if provided a local
    path, or will link to a remote image if provided a URL.

    Reference: https://panel.holoviz.org/reference/panes/ICO.html

    :Example:

    >>> ICO(
    ...     some_url,
    ...     alt_text='An .ico file',
    ...     link_url='https://en.wikipedia.org/wiki/ICO_(file_format)',
    ...     width=50
    ...
    Zicor   r   c                 C  s8   dd l }|d|dd \}}t|p(dt|p2dfS )Nr   z<BBr         r   r   r,   r,   r-   rI   Z  s    zICO._imgshapeNr   r,   r,   r,   r-   r   G  s   
r   c                   @  s6   e Zd ZU dZdZded< dZded< edd	 Zd
S )JPGa  
    The `JPG` pane embeds a .jpg or .jpeg image file in a panel if
    provided a local path, or will link to a remote image if provided
    a URL.

    Reference: https://panel.holoviz.org/reference/panes/JPG.html

    :Example:

    >>> JPG(
    ...     'https://www.gstatic.com/webp/gallery/4.sm.jpg',
    ...     alt_text='A nice tree',
    ...     link_url='https://en.wikipedia.org/wiki/JPEG',
    ...     width=500
    ... )
    jpegr   r   )r   ZjpgClassVar[Tuple[str, ...]]r   c              	   C  s   dd l }t|}|d |d}|rt|dkrt|dkrL|d}q4t|dkrd|d}qLt|dkrt|dkr|d |d	|d
\}}qn$|t|d|dd d  |d}q$t|t|fS )Nr   r   r                  z>HH   z>H)r   r   rC   ordr   r   )rK   rN   r   bcr   r   r,   r,   r-   rI   w  s    


$zJPG._imgshapeN	r1   rn   ro   r   r   rr   r   rs   rI   r,   r,   r,   r-   r   a  s
   
r   c                      s   e Zd ZU dZejdddZdZded< dd	iZ	d
ed< e
jdg Zded< eddd fddZ fddZ fddZdd ZdddddZ  ZS )SVGa  
    The `SVG` pane embeds a .svg image file in a panel if provided a
    local path, or will link to a remote image if provided a URL.

    Reference: https://panel.holoviz.org/reference/panes/SVG.html

    :Example:

    >>> SVG(
    ...     'https://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg',
    ...     alt_text='A gif vs svg comparison',
    ...     link_url='https://en.wikipedia.org/wiki/SVG',
    ...     width=300, height=400
    ... )
    Tzh
        Whether to enable base64 encoding of the SVG, base64 encoded
        SVGs do not support links.r   zsvg+xmlr   r   rP   Nr   r   r"   r#   r   r4   r5   c                   s$   t  |p"t|to"| dS N<svg)r&   rM   r$   r%   lstrip
startswith)rK   r6   r*   r,   r-   rM     s    zSVG.appliesc                   s,   t |trtdt| j t | d S )NzN%s pane cannot parse string that is not a filename, URL or a SVG XML contents.r.   r3   r*   r,   r-   r2     s
    
zSVG._type_errorc                   s(   t |tr| dr|S t |S r   )r$   r%   r   r   r&   rm   )r(   r6   r*   r,   r-   rm     s    z	SVG._datac                 C  s   | j | jfS r\   )r    r!   )r(   rN   r,   r,   r-   rI     s    zSVG._imgshaper   c           
      C  s  | j | j }}| ||\}}| jsRt|sRt|trD| dsRt|tt	fs^| 
|}nt| |||dS |d u rtddS | jr|rd| dnd}|rd| dnd}| jrdnd	}	d
| | d|	 d| | d}t|tr|d}t||t|dS )Nr   r   r   r~   r   r}   r   r   r   r   z8" style="max-width: 100%; max-height: 100%; object-fit: r   rO   )r    r!   text)r    r!   r   r   r   r$   r%   r   r   r   rm   r   r   rP   ry   rU   rH   rS   r   )
r(   r6   r    r!   r   r   rN   wsZhsr   r,   r,   r-   r     s"    &
"

zSVG._transform_object)r1   rn   ro   r   rp   rq   rP   r   rr   r   ru   r#   rs   rM   r2   rm   rI   r   rt   r,   r,   r*   r-   r     s   
r   c                   @  sl   e Zd ZU dZejdddZdZded< e	Z
ded	< d
d
iZded< ejdg Zded< dddddZdS )r   ao  
    The `PDF` pane embeds a .pdf image file in a panel if provided a
    local path, or will link to a remote image if provided a URL.

    Reference: https://panel.holoviz.org/reference/panes/PDF.html

    :Example:

    >>> PDF(
    ...     'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
    ...     width=300, height=410
    ... )
    r   z:
        Start page of the pdf, by default the first page.r   Zpdfr   r   zClassVar[Model]_bokeh_modelr   r   r   
start_pager"   r#   r   r   r5   c                 C  s   |d u rt ddS | js t|sh| |}t|ts>|d}t|	d}| jr^t |dS d| }| j
ppd| jpxd }}t| dd rd| j nd	}d
| | d|d|d}t t|dS )Nz<embed></embed>r   rO   )r   zdata:application/pdf;base64,r   r   z#page=r}   z<embed src="z" width=z height=z type="application/pdf">)r   r   r   rm   r$   rH   rP   rQ   rR   rS   r    r!   rb   r   r   )r(   r6   rN   rT   r   r   pager   r,   r,   r-   r     s    





zPDF._transform_objectN)r1   rn   ro   r   rp   ZIntegerr   r   rr   _BkPDFr   r   r   r#   r   r,   r,   r,   r-   r     s   
r   c                   @  s6   e Zd ZU dZdZded< dZded< edd	 Zd
S )WebPa  
    The `WebP` pane embeds a .webp image file in a panel if
    provided a local path, or will link to a remote image if provided
    a URL.

    Reference: https://panel.holoviz.org/reference/panes/WebP.html

    :Example:

    >>> WebP(
    ...     'https://assets.holoviz.org/panel/samples/webp_sample.webp',
    ...     alt_text='A nice tree',
    ...     link_url='https://en.wikipedia.org/wiki/WebP',
    ...     width=500,
    ...     caption='A nice tree'
    ... )
    webpr   r   )r   r   r   c                 C  sF  t |}|d |dd}|d d dkr>td|d }|d |dkr|d t|ddd	 }t|ddd	 }n|d
kr|d	 td|dd }|d@ d	 }|d? d@ d	 }n@|dkr|d t|ddd	 }t|ddd	 }W d    n1 s,0    Y  t|t|fS )N   r   rO   r   ZVP8zInvalid WebP fileXlittler   Lz<Ir   i?      r   r   )r   rC   rS   r/   r   
from_bytesr   r   )rK   rN   r   Zchunk_headerZwptyper   r   bitsr,   r,   r-   rI     s*    





6zWebP._imgshapeNr   r,   r,   r,   r-   r     s
   
r   ))r   
__future__r   rd   rQ   r   ior   pathlibr   typingr   r   r   r   r	   r
   r   rp   modelsr   r   utilr   r   Zmarkupr   r   Zbokeh.modelr   setre   r   ru   r   r   r   r   r   r   r   r,   r,   r,   r-   <module>   s0   $`n))C,