
    -eO                    D   d Z ddlmZ ddlZ ej        e          ZddlZddlZddl	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZmZmZ ddlmZ erdd	lmZ d
dlmZ d
dlmZ d
dlmZ d
dl m!Z!m"Z" d
dl#m$Z$ d
dl%m&Z& ddl'm(Z(m)Z) ddl*m+Z+ erd
dl,m-Z- d
dl.m/Z/ dZ0dddddddddPd*Z1ddddddd+dQd-Z2ddddddd+dRd/Z3dde!ddddd0dSd5Z4dde!dddd6dTd7Z5dde!dddd6dTd8Z6e!dddd9dUd<Z7dVd?Z8e
dWdB            Z9dXdEZ:dYdFZ;dZdIZ<dJZ=dKZ>dLZ? G dM dN          Z@d[dOZAdS )\z

    )annotationsN)contextmanager)abspath
expandusersplitext)mkstemp)TYPE_CHECKINGAnyIteratorcast)Image)	WebDriver   )PathLike)Document	file_html)INLINE	Resources)Theme)warn   )Statecurstate)default_filenamePlot)	UIElement)
export_png
export_svgexport_svgsget_layout_htmlget_screenshot_as_pngget_svgs   )filenamewidthheightscale_factor	webdrivertimeoutstateobjUIElement | Documentr&   PathLike | Noner'   
int | Noner(   r)   floatr*   WebDriver | Noner+   intr,   State | Nonereturnstrc          	     $   t          | ||||||          }|t          d          }|j        dk    s|j        dk    rt	          d          t          j        |          }|                    |           t          t          |                    S )a   Export the ``UIElement`` object or document as a PNG.

    If the filename is not given, it is derived from the script name (e.g.
    ``/foo/myplot.py`` will create ``/foo/myplot.png``)

    Args:
        obj (UIElement or Document) : a Layout (Row/Column), Plot or Widget
            object or Document to export.

        filename (PathLike, e.g. str, Path, optional) : filename to save document under (default: None)
            If None, infer from the filename.

        width (int) : the desired width of the exported layout obj only if
            it's a Plot instance. Otherwise the width kwarg is ignored.

        height (int) : the desired height of the exported layout obj only if
            it's a Plot instance. Otherwise the height kwarg is ignored.

        scale_factor (float, optional) : A factor to scale the output PNG by,
            providing a higher resolution while maintaining element relative
            scales.

        webdriver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time (in seconds) to wait for
            Bokeh to initialize (default: 5) (Added in 1.1.1).

        state (State, optional) :
            A :class:`State` object. If None, then the current default
            implicit state is used. (default: None).

    Returns:
        filename (str) : the filename where the static file is saved.

    If you would like to access an Image object directly, rather than save a
    file to disk, use the lower-level :func:`~bokeh.io.export.get_screenshot_as_png`
    function.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    )r'   r(   r)   driverr+   r,   Npngr   zunable to save an empty image)
r#   r   r'   r(   
ValueErrorosfspathsaver   r   )	r-   r&   r'   r(   r)   r*   r+   r,   images	            /lib/python3.11/site-packages/bokeh/io/export.pyr   r   I   s    ^ "#U6P\en*1@ @ @E #E**{a5<1,,8999y""H	JJx:h''(((    )r&   r'   r(   r*   r+   r,   	list[str]c               N    t          | |||||          }t          ||d          S )a   Export a layout as SVG file or a document as a set of SVG files.

    If the filename is not given, it is derived from the script name
    (e.g. ``/foo/myplot.py`` will create ``/foo/myplot.svg``)

    Args:
        obj (UIElement object) : a Layout (Row/Column), Plot or Widget object to display

        filename (PathLike, e.g. str, Path, optional) : filename to save document under (default: None)
            If None, infer from the filename.

        width (int) : the desired width of the exported layout obj only if
            it's a Plot instance. Otherwise the width kwarg is ignored.

        height (int) : the desired height of the exported layout obj only if
            it's a Plot instance. Otherwise the height kwarg is ignored.

        webdriver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time (in seconds) to wait for
            Bokeh to initialize (default: 5)

        state (State, optional) :
            A :class:`State` object. If None, then the current default
            implicit state is used. (default: None).

    Returns:
        filenames (list(str)) : the list of filenames where the SVGs files are saved.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    r'   r(   r8   r+   r,   svg)get_svg_write_collectionr-   r&   r'   r(   r*   r+   r,   svgss           r?   r    r       s2    J 3eF9g]bcccDT8U333r@   
str | Nonec                   t          | |||||          }t          |          dk    rt                              d           g S t	          ||d          S )a   Export the SVG-enabled plots within a layout. Each plot will result
    in a distinct SVG file.

    If the filename is not given, it is derived from the script name
    (e.g. ``/foo/myplot.py`` will create ``/foo/myplot.svg``)

    Args:
        obj (UIElement object) : a Layout (Row/Column), Plot or Widget object to display

        filename (str, optional) : filename to save document under (default: None)
            If None, infer from the filename.

        width (int) : the desired width of the exported layout obj only if
            it's a Plot instance. Otherwise the width kwarg is ignored.

        height (int) : the desired height of the exported layout obj only if
            it's a Plot instance. Otherwise the height kwarg is ignored.

        webdriver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time (in seconds) to wait for
            Bokeh to initialize (default: 5) (Added in 1.1.1).

        state (State, optional) :
            A :class:`State` object. If None, then the current default
            implicit state is used. (default: None).

    Returns:
        filenames (list(str)) : the list of filenames where the SVGs files are saved.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    rC   r   zNo SVG Plots were found.rD   )r$   lenlogwarningrF   rG   s           r?   r!   r!      sY    L CuVIw^cdddD
4yyA~~.///	T8U333r@   )r8   r+   	resourcesr'   r(   r)   r,   r8   rN   r   Image.Imagec                  ddl m}m}	m}
 t	                      5 }|pt                      j        j        }t          | ||||          }t          |j
        dd          5 }|                    |           ddd           n# 1 swxY w Y   |0|} |	||          s! ||          }t          d| d	| d
          n|
                    |          }|                                 |                    d|j
                    t          ||           t!          |          \  }}}|                                }ddd           n# 1 swxY w Y   t%          j	        t'          j        |                                        d                              dd||z  ||z  f                              t1          ||z            t1          ||z            f          S )a;   Get a screenshot of a ``UIElement`` object.

    Args:
        obj (UIElement or Document) : a Layout (Row/Column), Plot or Widget
            object or Document to export.

        driver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time to wait for initialization.
            It will be used as a timeout for loading Bokeh, then when waiting for
            the layout to be rendered.

        scale_factor (float, optional) : A factor to scale the output PNG by,
            providing a higher resolution while maintaining element relative
            scales.

        state (State, optional) :
            A :class:`State` object. If None, then the current default
            implicit state is used. (default: None).

    Returns:
        image (PIL.Image.Image) : a pillow image loaded from PNG.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    r   )!get_web_driver_device_pixel_ratio4scale_factor_less_than_web_driver_device_pixel_ratiowebdriver_controlrN   r'   r(   themewutf-8modeencodingNzBExpected the web driver to have a device pixel ratio greater than z6. Was given a web driver with a device pixel ratio of .)r)   file://RGBAr   )r*   rQ   rR   rS   	_tmp_htmlr   documentrU   r"   openpathwriter:   getmaximize_windowwait_until_render_complete_maximize_viewportr#   r   ioBytesIOconvertcropresizer3   )r-   r8   r+   rN   r'   r(   r)   r,   rQ   rR   rS   tmprU   htmlfile
web_driverdevice_pixel_ratiodprr9   s                      r?   r#   r#      s   @ F  F  F  F  F  F  F  F  F  F	 1$(**.4siuV[`aaa#(w777 	4JJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 JGGV`aa o%F%Fz%R%R"  "nfr "n "nXj"n "n "n o o oo
 +..L.IIJ""$$$+++,,,":w7771*==..00%1 1 1 1 1 1 1 1 1 1 1 1 1 1 1( Jrz#'''&//$1eCi455&#eL0113vl7J3K3KLMMOs7   AEB5EB	EB		B3EEE)r8   r+   rN   r'   r(   r,   c               <   ddl m} t                      5 }|pt                      j        j        }	t          | ||||	          }
t          |j        dd          5 }|	                    |
           d d d            n# 1 swxY w Y   ||n|
                                }|
                    d|j                    t          ||           t          t          t                   |                    t                               }d d d            n# 1 swxY w Y   |S Nr   )rS   rT   rV   rW   rX   r\   )r*   rS   r^   r   r_   rU   r"   r`   ra   rb   rc   re   r   listr6   execute_script_SVG_SCRIPTr-   r8   r+   rN   r'   r(   r,   rS   rl   rU   rm   rn   ro   rH   s                 r?   rE   rE     s   ,,,,,,	 	G$(**.4siuV[`aaa#(w777 	4JJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  &1VV7H7L7L7N7N
+++,,,":w777DIz88EEFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G K7   ADA=1D=B	DB	B DDDc               <   ddl m} t                      5 }|pt                      j        j        }	t          | ||||	          }
t          |j        dd          5 }|	                    |
           d d d            n# 1 swxY w Y   ||n|
                                }|
                    d|j                    t          ||           t          t          t                   |                    t                               }d d d            n# 1 swxY w Y   |S rs   )r*   rS   r^   r   r_   rU   r"   r`   ra   rb   rc   re   r   rt   r6   ru   _SVGS_SCRIPTrw   s                 r?   r$   r$   ,  s   ,,,,,,	 	H$(**.4siuV[`aaa#(w777 	4JJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  &1VV7H7L7L7N7N
+++,,,":w777DIz88FFGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H Krx   rT   rU   Theme | Nonec                    dd	 fd}||Yddl m} t           |          st          d           n3t	           ||          5   |            cddd           S # 1 swxY w Y    |            S )


    a%  \
    {% block preamble %}
    <style>
        html, body {
            box-sizing: border-box;
            width: 100%;
            height: 100%;
            margin: 0;
            border: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
    {% endblock %}
    r5   r6   c            	     0    t           ddd          S )N T)rN   titletemplaterU   suppress_callback_warning_always_newr   )r-   rN   r   rU   s   r?   rm   zget_layout_html.<locals>.htmlR  s/    &*
 
 
 	
r@   Nr   r   zhExport method called with width or height argument on a non-Plot model. The size values will be ignored.)r5   r6   )models.plotsr   
isinstancer   _resized)r-   rN   r'   r(   rU   rm   r   r   s   ``  `  @r?   r"   r"   =  s    
H 	
 	
 	
 	
 	
 	
 	
 	
 	
 F.''''''#t$$ 	{||||#uf--  tvv                466Ms   
A""A&)A&r   Nonec                   ddl m} ddlm} dd}	  || |d	
                              |           n,# |$ r$}t          |            t          d          |d}~ww xY w|                     t                     dd}	  || |d	
                              |           n%# |$ r t          
                    d           Y nw xY wt          |            dS # t          |            w xY w)r}   r   )TimeoutException)WebDriverWaitr8   r   r5   boolc                R    t          t          |                     d                    S )Nzs
            return typeof Bokeh !== "undefined" && Bokeh.documents != null && Bokeh.documents.length != 0
        r   r   ru   r8   s    r?   is_bokeh_loadedz3wait_until_render_complete.<locals>.is_bokeh_loadedo  s-    D&// 1     	r@   g?)poll_frequencyz<Bokeh was not loaded in time. Something may have gone wrong.Nc                R    t          t          |                     d                    S )Nz%return window._bokeh_render_complete;r   r   s    r?   is_bokeh_render_completez<wait_until_render_complete.<locals>.is_bokeh_render_complete|  s!    D&//0WXXYYYr@   zThe webdriver raised a TimeoutException while waiting for a 'bokeh:idle' event to signify that the layout has rendered. Something may have gone wrong.)r8   r   r5   r   )selenium.common.exceptionsr   selenium.webdriver.support.waitr   until_log_consoleRuntimeErrorru   _WAIT_SCRIPTrL   rM   )r8   r+   r   r   r   er   s          r?   re   re   h  ss    <;;;;;======   
bfgc:::@@QQQQ b b bVYZZ`aab ,'''Z Z Z Zfgc:::@@AYZZZZ 6 6 6 5 	6 	6 	6 	6 	66
 	VVs>   !4 AAA?!B!  C !C C CC C(r   Iterator[None]c              #  n   K   | j         }| j        }||| _         ||| _        d V  || _         || _        d S N)r'   r(   )r-   r'   r(   	old_width
old_heights        r?   r   r     sI      	IJ	
	EEECICJJJr@   itemsextc                V   |t          |          }t          j        |          }g }dd}t          |           D ]m\  }}|dk    r|n |||          }t	          |dd	
          5 }|                    |           d d d            n# 1 swxY w Y   |                    |           n|S )Nnamer6   ir3   r5   c                8    t          |           \  }}| d| | S )N_)r   )r   r   basenamer   s       r?   _indexedz#_write_collection.<locals>._indexed  s*     #%%Q%%%%r@   r   rV   rW   rX   )r   r6   r   r3   r5   r6   )r   r;   r<   	enumerater`   rb   append)	r   r&   r   	filenamesr   r   itemfnamefs	            r?   rF   rF     s   #C((y""HI& & & & U##    4FF1(=(=%cG444 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   &BB	B	c                   h d	 |                      d          }n# t          $ r Y d S w xY wfd|D             }t          |          dk    r9t                              d           |D ]}t                              |           d S d S )N>   ERRORSEVEREWARNINGbrowserc                h    g | ].}|                     d           v |                     d          /S )levelmessage)rc   ).0rL   levelss     r?   
<listcomp>z _log_console.<locals>.<listcomp>  s;    SSSswww7G7G67Q7Q##7Q7Q7Qr@   r   zLThere were browser warnings and/or errors that may have affected your export)get_log	ExceptionrK   rL   rM   )r8   logsmessagesr   r   s       @r?   r   r     s    +++F~~i((   SSSStSSSH
8}}qbccc 	! 	!GKK     	! 	!s    
++ro   tuple[int, int, int]c                    d}|                      |          }d} | j         |g|R  \  }}d}|                     ||z   ||z              |S )Nz        const root_view = Bokeh.index.roots[0]
        const {width, height} = root_view.el.getBoundingClientRect()
        return [Math.round(width), Math.round(height), window.devicePixelRatio]
    aa          const [width, height, dpr] = arguments
        return [
            // XXX: outer{Width,Height} can be 0 in headless mode under certain window managers
            Math.round(Math.max(0, window.outerWidth - window.innerWidth) + width*dpr),
            Math.round(Math.max(0, window.outerHeight - window.innerHeight) + height*dpr),
        ]
    d   )ru   set_window_size)ro   calculate_viewport_sizeviewport_sizecalculate_window_sizer'   r(   epss          r?   rf   rf     sr    
 +5*C*CD[*\*\M 0j/0EVVVVOUF
Cus{FSL999r@   a  
const {LayoutDOMView} = Bokeh.require("models/layouts/layout_dom")
const {PlotView} = Bokeh.require("models/plots/plot")

function* collect_svgs(views) {
  for (const view of views) {
    if (view instanceof LayoutDOMView) {
      yield* collect_svgs(view.child_views.values())
    }
    if (view instanceof PlotView && view.model.output_backend == "svg") {
      const {ctx} = view.canvas_view.compose()
      yield ctx.get_serialized_svg(true)
    }
  }
}

return [...collect_svgs(Bokeh.index)]
zfunction* export_svgs(views) {
  for (const view of views) {
    // TODO: use to_blob() API in future
    const {ctx} = view.export("svg")
    yield ctx.get_serialized_svg(true)
  }
}

return [...export_svgs(Bokeh.index)]
z
// add private window prop to check that render is complete
window._bokeh_render_complete = false;
function done() {
  window._bokeh_render_complete = true;
}

const doc = Bokeh.documents[0];

if (doc.is_idle)
  done();
else
  doc.idle.connect(done);
c                  b    e Zd ZU dZded<   ded<   ded<   dd	d
ddZddZddZddZddZ	dS )	_TempFileFr   _closedr3   fdr6   ra   rl   r   prefixsuffixr   r   r5   r   c                   	 t          ||t          j                              \  | _        | _        d S # t
          $ r" t          ||          \  | _        | _        Y d S w xY w)N)r   r   dirr   )r   r;   getcwdr   ra   OSError)selfr   r   s      r?   __init__z_TempFile.__init__  so    	G!(v29;;!W!W!WDGTYYY 	G 	G 	G!(v!F!F!FDGTYYYY	Gs   04 (A A c                    | S r    r   s    r?   	__enter__z_TempFile.__enter__  s    r@   excr
   valuetbc                .    |                                   d S r   close)r   r   r   r   s       r?   __exit__z_TempFile.__exit__      

r@   c                .    |                                   d S r   r   r   s    r?   __del__z_TempFile.__del__  r   r@   c                    | j         rd S 	 t          j        | j                   n# t          $ r Y nw xY w	 t          j        | j                   n# t          $ r Y nw xY wd| _         d S )NT)r   r;   r   r   r   unlinkra   r   s    r?   r   z_TempFile.close  s    < 	F	HTW 	 	 	D		Idi     	 	 	D	 s   % 
22A 
AAN)r   r6   r   r6   r5   r   r5   r   )r   r
   r   r
   r   r
   r5   r   )r5   r   )
__name__
__module____qualname__r   __annotations__r   r   r   r   r   r   r@   r?   r   r      s         GGGGIII(-R G G G G G G              r@   r   c                 $    t          dd          S )Nbokehz.htmlr   )r   r   r@   r?   r^   r^   (  s    GG4444r@   )r-   r.   r&   r/   r'   r0   r(   r0   r)   r1   r*   r2   r+   r3   r,   r4   r5   r6   )r-   r.   r&   r/   r'   r0   r(   r0   r*   r2   r+   r3   r,   r4   r5   rA   )r-   r.   r&   rI   r'   r0   r(   r0   r*   r2   r+   r3   r,   r4   r5   rA   )r-   r.   r8   r2   r+   r3   rN   r   r'   r0   r(   r0   r)   r1   r,   r4   r5   rO   )r-   r.   r8   r2   r+   r3   rN   r   r'   r0   r(   r0   r,   r4   r5   rA   )r-   r.   rN   r   r'   r0   r(   r0   rU   r{   r5   r6   )r8   r   r+   r3   r5   r   )r-   r   r'   r0   r(   r0   r5   r   )r   rA   r&   r/   r   r6   r5   rA   )r8   r   r5   r   )ro   r   r5   r   r   )B__doc__
__future__r   logging	getLoggerr   rL   rg   r;   
contextlibr   os.pathr   r   r   tempfiler   typingr	   r
   r   r   PILr   #selenium.webdriver.remote.webdriverr   
core.typesr   r_   r   embedr   rN   r   r   themesr   util.warningsr   r,   r   r   utilr   r   r   	models.uir   __all__r   r    r!   r#   rE   r$   r"   re   r   rF   r   rf   rz   rv   r   r   r^   r   r@   r?   <module>r      s
    # " " " " " g!! 
			 				 % % % % % % 1 1 1 1 1 1 1 1 1 1                        >====== " ! ! ! ! !             ) ) ) ) ) ) ) )                   " " " " " " " " " " " " " " &######%%%%%% JNcg!Z^;) ;) ;) ;) ;) ;)z JNcg!VWos&4 &4 &4 &4 &4 &4P FJ_c!VWos,4 ,4 ,4 ,4 ,4 ,4d TXhi%4VZt9O 9O 9O 9O 9O 9Ov FJZ[%4VZrv     " GK[\%4VZrv     " JP tSW) ) ) ) ) )V   H       *
! 
! 
! 
!   *&
 & & & & & & & &P5 5 5 5 5 5r@   