
    Be:@                       d Z ddlm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
rddlmZ  G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z# G d de          Z$ G 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                       e Zd ZU  ej        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	Z fdZedd            ZddZd dZ x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                    t          |t                    rt          |          } t                      j        dd|i| d S )Nr     )
isinstancer   strsuper__init__)selfr    params	__class__s      0lib/python3.11/site-packages/panel/pane/image.pyr+   zFileBase.__init__*   sH    fh'' 	![[F111&11111    c                    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.   s     r/   r6   zFileBase._type_error/   so    fc"" 	::& : :-3: : :   	F#####r0   objr   returnfloat | bool | Nonec                B   | j                             d          d         }| j        p|f}t          d| d          rdS t	          t
                    r!t                                                    t	          t                    rBt          |          rdS t          fd|D                       rdS t          d           rdS n>t	          t                    r)	 |                                dS # t          $ r Y d	S w xY wt          d
          rdS d	S )N+r   _repr__g333333?Tc              3  l   K   | ].}                                                     d |           V  /dS ).N)lowerendswith).0extr8   s     r/   	<genexpr>z#FileBase.applies.<locals>.<genexpr>B   s?      EESYY[[)))c))44EEEEEEr0   g        Fread)r   splitr   hasattrr(   r   r)   absoluter   anybytes	_imgshape	Exception)clsr8   r   extss    `  r/   applieszFileBase.applies7   sP   <%%c**1--8+3,,,,-- 	4c8$$ 	&cllnn%%Cc3 	S$ tEEEEEEEEE tsD!! sU## 	c"""t   uu3 	4us   %C< <
D
	D
datastr | bytesr)   c                    t          |t                    s|                    d          }t          j        |                              d          }d| j         d| S )Nutf-8zdata:image/z;base64,)r(   rK   encodebase64	b64encodedecoder   )r,   rQ   b64s      r/   _b64zFileBase._b64P   s[    $&& 	(;;w''Dt$$++G449T]99C999r0   bytes | Nonec                ^   
  j                             d          d         }t          d| d          r t          d| d                      S t	          t
          t          f          rLt                    r<t          d          5 }|	                                cd d d            S # 1 swxY w Y   nrt	          t                    rS t          d          r9t          d          r                    d           	                                S t          d           sd S dd	lm} |j        rRdd
lm}m} |r |          	                                S ddlm
 
 fd}t+          j         |                       d S dd l}|                    d          }	|	j        S )Nr<   r   r=   r>   rbrF   seekr   )state)
_IN_WORKERfetch_binary)pyfetchc                 h   K                d {V                                   d {V _        d S N)rK   r    )r8   rb   r,   s   r/   replace_contentz'FileBase._data.<locals>.replace_contentn   sJ      /6ws||););););););(B(B(D(D"D"D"D"D"D"DDKKKr0   GET)urlmethod)r   rG   rH   getattrr(   r)   r   r   openrF   rK   r^   r   io.stater_   _is_pyodide
io.pyodider`   ra   pyodide.httprb   asynciocreate_taskrequestsrequestcontent)r,   r8   r   fr_   r`   ra   re   rq   rrb   s   ``        @r/   _datazFileBase._dataV   sB   =&&s++A.3,,,,-- 	573 4 4 4 455777c8_-- 	c{{ $#t__ $6688$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $U## 	JS&!! 	sF## 88::sD!! 	4$$$$$$ 	======== 7#|C((--///000000E E E E E E E#OO$5$566666OOO  S 77A9s   B00B47B4rd   r8   r   r9   r:   )rQ   rR   r9   r)   )r8   r   r9   r[   )r5   
__module____qualname__paramBooleanr   __annotations__r   r   r%   _FileBase__abstractr+   r6   classmethodrP   rZ   rv   __classcell__r.   s   @r/   r   r      s         EM% .0 1 1 1E 48K88883:D/GAAAA- - -     J2 2 2 2 2 2
$ $ $ $ $    [0: : : :       r0   r   c                      e Zd ZU dZ ej        dd          Z ej        dd          Z ej        dd          Z	g dZ
d	ed
<   ddddZded<   ddiZded<   dZed             Z	 dddZd Z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   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link_urlr   r    r!   r"   r#   r$   r%   )r   fixed_aspectr   r   r   r    z!'<img src="' + value + '"></img>'_target_transformsc                    t           )z'Calculate and return image width,height)NotImplementedError)rN   rQ   s     r/   rL   zImageBase._imgshape   s
     "!r0   srcr)   r"   
str | Noner#   c                    | j         r
d| j         nd}|rd| dnd}|rd| dnd}d| d| d| | d	}| j        rd
| j         d| d}t          |          S )Nzalt=  width: ;	 height: 
<img src="z" z? style="max-width: 100%; max-height: 100%; object-fit: contain;"></img>z	<a href="z" target="_blank">z</a>)r   r   r   )r,   r   r"   r#   althtmls         r/   _format_htmlzImageBase._format_html   s     +/-?&T]&&&R',4#5####"*08&V&&&&b~C~~3~~gl~nt~~~= 	KJt}JJJJJDd||r0   c                    | j         }|dv r|r| dnd|r| dnd}}nT|dk    r| j        sd\  }}nA|dk    r| j        sd|r| dnd}}n(|dk    r| j        s|r| dndd}}n|d	v rd
\  }}nd\  }}||fS )N)fixedNpxautostretch_both)100%r   stretch_widthr   stretch_height)scale_heightr   )r   r   )r   r   )sizing_moder   )r,   r"   r#   smodewhs         r/   	_img_dimszImageBase._img_dims   s     O##!&2EF"(4Ff AA n$$T->$!DAqqo%%d.?%V?vMMMMqAA&&&t/@&$)5uLLLLvqAA888!DAqq!DAq!tr0   r8   r   r9   Dict[str, Any]c                6   | j         s+t          |          st          |t          t          f          s|                     |          }nH|                     | j        | j                  \  }}t          | 
                    |||                    S |t          d          S t          |t                    st          j        |          }|                     |          \  }}| j        1| j        t          | j        |z  |z            }n| j        }| j        }n(| j        !t          | j        |z  |z            }| j        }|                     |          }|                     ||          \  }}| 
                    |||          }t          |||          S )Nr    <img></img>)r"   r#   r    )r   r   r(   r)   r   rv   r   r"   r#   dictr   rK   rV   	b64decoderL   intrZ   )	r,   r8   rQ   r   r   r"   r#   r   r   s	            r/   _transform_objectzImageBase._transform_object   s   : 	=&++ 	=Zc8_-M-M 	=::c??DD>>$*dk::DAqt00a;;<<<<<}----$&& 	*#D))Dt,,v:!{"dj.677JEE[$V+U233E[Fiioo~~eV,,1  a++%t<<<<r0   )NN)r   r)   r"   r   r#   r   r8   r   r9   r   )r5   rx   ry   __doc__rz   Stringr   r{   r   r   r%   r|   r   r   _ImageBase__abstractr~   rL   r   r   r   r'   r0   r/   r   r   w   sY          u|D /2 3 3 3H !5= 4   L u|D /   H- - -    
 $D4 4G    
 	9>     J" " ["
 HL	 	 	 	 	  &= = = = = =r0   r   c                  2    e Zd ZdZed
d            Z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
    ... )
    r8   r   r9   r:   c                j   g }t          j        t                                                    D ]m}|t          u r|                    |          }t          |t                    r|r|c S t          |t          t          f          r|
                    |           n|rt          |          d         S dS )NF)rz   concrete_descendentsr   valuesr   rP   r(   boolfloatr   appendsorted)rN   r8   precedencesimg_clsrP   s        r/   rP   zImage.applies   s    1)<<CCEE 	, 	,G%ooc**G'4(( ,W ,GeS\22 ,""7+++ 	++&&r**ur0   r   c                H   d | j                                                                         D             }t          j        t                                                    D ]>}|t
          ur3|                    |          r ||fi |                    |          c S ?ddiS )Nc                "    i | ]\  }}|d v	||S ))namer    r'   )rC   kvs      r/   
<dictcomp>z+Image._transform_object.<locals>.<dictcomp>   s4     
 
 
Q*** q***r0   r    r   )rz   r   itemsr   r   r   rP   r   )r,   r8   r-   r   s       r/   r   zImage._transform_object   s    
 
!Z..006688
 
 
 1)<<CCEE 	E 	EGe##(<(<#ws--f--??DDDDD-((r0   Nrw   r   )r5   rx   ry   r   r~   rP   r   r'   r0   r/   r   r      sR             [) ) ) ) ) )r0   r   c                  8    e Zd ZU dZdZded<   e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
    ... )
    pngr   r   c                    dd l }|                    d|dd                   \  }}t          |          t          |          fS )Nr   z>LL      structunpackr   rN   rQ   r   r   r   s        r/   rL   zPNG._imgshape  s@    }}UDBK0011vvs1vv~r0   Nr5   rx   ry   r   r   r|   r~   rL   r'   r0   r/   r   r     L            $H####  [  r0   r   c                  8    e Zd ZU dZdZded<   e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
    ... )
    gifr   r   c                    dd l }|                    d|dd                   \  }}t          |          t          |          fS )Nr   z<HH   
   r   r   s        r/   rL   zGIF._imgshape5  s@    }}UD2J//11vvs1vv~r0   Nr   r'   r0   r/   r   r   "  r   r0   r   c                  8    e Zd ZU dZdZded<   e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
    ...
    icor   r   c                    dd l }|                    d|dd                   \  }}t          |pd          t          |pd          fS )Nr   z<BBr         r   r   s        r/   rL   zICO._imgshapeO  sI    }}UT!A#Y//118}}c!(smm++r0   Nr   r'   r0   r/   r   r   <  sL            $H####, , [, , ,r0   r   c                  F    e Zd ZU dZdZded<   dZded<   e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   jpgzClassVar[Tuple[str, ...]]r   c           
        dd l }t          |          }|                    d           |                    d          }|rot          |          dk    r[t          |          dk    r(|                    d          }t          |          dk    (t          |          dk    r(|                    d          }t          |          dk    (t          |          dk    rUt          |          dk    rB|                    d           |                    d	|                    d
                    \  }}n}|                    t          |                    d|                    d                    d                   dz
             |                    d          }|rt          |          dk    [t          |          t          |          fS )Nr   r   r                  z>HH   z>H)r   r   rF   ordr   r   )rN   rQ   r   bcr   r   s          r/   rL   zJPG._imgshapel  sk   DMM	q			FF1II 		SVVt^^q66T>>q		A3q66T>>q66T>>q		A3q66T>>A$3q66T>>q			}}UAFF1II661s6==qvvayy99!<==a?@@@q		A  		SVVt^^ 1vvs1vv~r0   N)	r5   rx   ry   r   r   r|   r   r~   rL   r'   r0   r/   r   r   V  s]          " %H$$$$-<K<<<<  [  r0   r   c                       e Zd ZU dZ ej        dd          ZdZded<   dd	iZ	d
ed<   e
j        dgz   Zded<   ed fd            Z fdZ fdZd ZddZ x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   rU   Nr   r   r$   r%   r8   r   r9   r:   c                    t                                          |          p;t          |t                    o&|                                                    d          S N<svg)r*   rP   r(   r)   lstrip
startswith)rN   r8   r.   s     r/   rP   zSVG.applies  sJ    $$ KC%%I#**,,*A*A&*I*I	Lr0   c                    t          |t                    r$t          dt          |           j        z            t                                          |           d S )NzN%s pane cannot parse string that is not a filename, URL or a SVG XML contents.r2   r7   s     r/   r6   zSVG._type_error  s`    fc"" 	Q :<@JJ<OP Q Q QF#####r0   c                    t          |t                    r)|                                                    d          r|S t	                                          |          S r   )r(   r)   r   r   r*   rv   )r,   r8   r.   s     r/   rv   z	SVG._data  sM    sC   	SZZ\\%<%<V%D%D 	Jww}}S!!!r0   c                    | j         | j        fS rd   )r"   r#   )r,   rQ   s     r/   rL   zSVG._imgshape  s    
DK((r0   r   c                .   | j         | j        }}|                     ||          \  }}| j        sgt	          |          sXt          |t                    r'|                                                    d          st          |t          t          f          s| 
                    |          }n%t          |                     |||                    S |t          d          S | j        r3|rd| dnd}|rd| dnd}d|                     |           d	| | d
}n1| j         s| j        s	| j        dvr| j                            d           t          |t$                    r|                    d          }t          ||t)          |                    S )Nr   r   r   r   r   r   r   r   z@" style="max-width: 100%; max-height: 100%; object-fit: contain;r   )Nr   zSVG sizing cannot be scaled if the SVG has been embedded but is not encoded. Either enable encoding or manipulate the SVG itself.rT   )r"   r#   text)r"   r#   r   r   r   r(   r)   r   r   r   rv   r   r   rU   rZ   r   rz   warningrK   rX   r   )	r,   r8   r"   r#   r   r   rQ   wshss	            r/   r   zSVG._transform_object  s   
DKv~~eV,,1: 	=&++ 	=*S#*>*> 	=3::<<CZCZ[aCbCb 	=!+C#x!A!A	=::c??DDt00a;;<<<<<}----; 		$%-A2B%&.!Q!!!!BB B		$  B  Bqs  Buw  B  B  BDDZ 	4; 	$*:/*Q*QJ"  
 dE"" 	(;;w''D%VD\\BBBBr0   rw   r   )r5   rx   ry   r   rz   r{   rU   r   r|   r   r   r%   r~   rP   r6   rv   rL   r   r   r   s   @r/   r   r     s           U]4 .& ' ' 'F (H''''3;T2BGBBBB,5,F(,SSSSSL L L L L [L$ $ $ $ $" " " " "
) ) )C C C C C C C Cr0   r   c                      e Zd ZU dZ ej        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   Zde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   pdfr   r   zClassVar[Model]_bokeh_modelr   r   r   
start_pager$   r%   r8   r   r9   r   c                
   |t          d          S | j        st          |          s|                     |          }t	          |t
                    s|                    d          }t          j        |          	                    d          }| j        rt          |          S d| }| j
        pd| j        pd}}t          | dd           r
d| j         nd	}d
| | d|d|d}t          t          |                    S )Nz<embed></embed>r   rT   )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   rv   r(   rK   rU   rV   rW   rX   r"   r#   ri   r   r   )r,   r8   rQ   rY   r   r   pager   s           r/   r   zPDF._transform_object  s   ;01111Z 	7uSzz 	7::c??DdE** ,{{7++"4((//88Cz &~~~%666Cz#VT[%:F1-4T<-N-NV))))TV[c[4[[[[a[[[&&&&r0   Nr   )r5   rx   ry   r   rz   Integerr   r   r|   _BkPDFr   r   r   r%   r   r'   r0   r/   r   r     s           q /= > > >J $H####$*L****3:G2DGDDDD,4,E,VVVVV' ' ' ' ' 'r0   r   )%r   
__future__r   ro   rV   ior   pathlibr   typingr   r   r   r	   r
   r   r   rz   modelsr   r   utilr   r   markupr   r   bokeh.modelr   r   r   r   r   r   r   r   r   r'   r0   r/   <module>r     s    # " " " " "                                 " " " " " "                 ( ( ( ( ( ( ( ( "!!!!!![ [ [ [ [| [ [ [|e= e= e= e= e= e= e= e=P&) &) &) &) &)I &) &) &)R    )   4    )   4, , , , ,) , , ,4& & & & &) & & &REC EC EC EC EC) EC EC ECP*' *' *' *' *'( *' *' *' *' *'r0   