
    hjeO                    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 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	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)	WebDriver   )PathLike)Document	file_html)INLINE	Resources)Theme)warn   )Statecurstate)default_filenameImage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    H   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)   r9   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3rA   
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.

    rD   r   zNo SVG Plots were found.rE   )r%   lenlogwarningrG   rH   s           r@   r"   r"      sY    L CuVIw^cdddD
4yyA~~.///	T8U333rA   )r9   r,   	resourcesr(   r)   r*   r-   r9   rO   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   ddlm}  |j	        t)          j        |                                        d                              dd||z  ||z  f                              t3          ||z            t3          ||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rO   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://r   r   RGBA)r+   rR   rS   rT   	_tmp_htmlr   documentrV   r#   openpathwriter;   getmaximize_windowwait_until_render_complete_maximize_viewportr$   PILr   ioBytesIOconvertcropresizer4   )r.   r9   r,   rO   r(   r)   r*   r-   rR   rS   rT   tmprV   htmlfile
web_driverdevice_pixel_ratiodprr:   r   s                       r@   r$   r$      s   @          
 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( EJrz#'''&//$1eCi455&#eL0113vl7J3K3KLMMOs7   AEB5EB	EB		B3EEE)r9   r,   rO   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   )rT   rU   rW   rX   rY   r]   )r+   rT   r_   r   r`   rV   r#   ra   rb   rc   rd   rf   r   listr7   execute_script_SVG_SCRIPTr.   r9   r,   rO   r(   r)   r-   rT   rn   rV   ro   rp   rq   rI   s                 r@   rF   rF     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 ru   )r+   rT   r_   r   r`   rV   r#   ra   rb   rc   rd   rf   r   rv   r7   rw   _SVGS_SCRIPTry   s                 r@   r%   r%   0  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 Krz   rU   rV   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 %}
    r6   r7   c            	     0    t           ddd          S )N T)rO   titletemplaterV   suppress_callback_warning_always_newr   )r.   rO   r   rV   s   r@   ro   zget_layout_html.<locals>.htmlV  s/    &*
 
 
 	
rA   Nr   r   zhExport method called with width or height argument on a non-Plot model. The size values will be ignored.)r6   r7   )models.plotsr   
isinstancer   _resized)r.   rO   r(   r)   rV   ro   r   r   s   ``  `  @r@   r#   r#   A  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)WebDriverWaitr9   r   r6   boolc                R    t          t          |                     d                    S )Nzs
            return typeof Bokeh !== "undefined" && Bokeh.documents != null && Bokeh.documents.length != 0
        r   r   rw   r9   s    r@   is_bokeh_loadedz3wait_until_render_complete.<locals>.is_bokeh_loadeds  s-    D&// 1     	rA   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YrA   zThe webdriver raised a TimeoutException while waiting for a 'bokeh:idle' event to signify that the layout has rendered. Something may have gone wrong.)r9   r   r6   r   )selenium.common.exceptionsr   selenium.webdriver.support.waitr   until_log_consoleRuntimeErrorrw   _WAIT_SCRIPTrM   rN   )r9   r,   r   r   r   er   s          r@   rf   rf   l  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CJJJrA   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namer7   ir4   r6   c                8    t          |           \  }}| d| | S )N_)r   )r   r   basenamer   s       r@   _indexedz#_write_collection.<locals>._indexed  s*     #%%Q%%%%rA   r   rW   rX   rY   )r   r7   r   r4   r6   r7   )r   r<   r=   	enumeratera   rc   append)	r   r'   r   	filenamesr   r   itemfnamefs	            r@   rG   rG     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)rd   ).0rM   levelss     r@   
<listcomp>z _log_console.<locals>.<listcomp>  s;    SSSswww7G7G67Q7Q##7Q7Q7QrA   r   zLThere were browser warnings and/or errors that may have affected your export)get_log	ExceptionrL   rM   rN   )r9   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    
++rq   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   )rw   set_window_size)rq   calculate_viewport_sizeviewport_sizecalculate_window_sizer(   r)   epss          r@   rg   rg     sr    
 +5*C*CD[*\*\M 0j/0EVVVVOUF
Cus{FSL999rA   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   _closedr4   fdr7   rb   rn   r   prefixsuffixr   r   r6   r   c                   	 t          ||t          j                              \  | _        | _        d S # t
          $ r" t          ||          \  | _        | _        Y d S w xY w)N)r   r   dirr   )r   r<   getcwdr   rb   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    rA   excr
   valuetbc                .    |                                   d S r   close)r   r   r   r   s       r@   __exit__z_TempFile.__exit__      

rA   c                .    |                                   d S r   r   r   s    r@   __del__z_TempFile.__del__  r   rA   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   unlinkrb   r   s    r@   r   z_TempFile.close  s    < 	F	HTW 	 	 	D		Idi     	 	 	D	 s   % 
22A 
AAN)r   r7   r   r7   r6   r   r6   r   )r   r
   r   r
   r   r
   r6   r   )r6   r   )
__name__
__module____qualname__r   __annotations__r   r   r   r   r   r   rA   r@   r   r     s         GGGGIII(-R G G G G G G              rA   r   c                 $    t          dd          S )Nbokehz.htmlr   )r   r   rA   r@   r_   r_   ,  s    GG4444rA   )r.   r/   r'   r0   r(   r1   r)   r1   r*   r2   r+   r3   r,   r4   r-   r5   r6   r7   )r.   r/   r'   r0   r(   r1   r)   r1   r+   r3   r,   r4   r-   r5   r6   rB   )r.   r/   r'   rJ   r(   r1   r)   r1   r+   r3   r,   r4   r-   r5   r6   rB   )r.   r/   r9   r3   r,   r4   rO   r   r(   r1   r)   r1   r*   r2   r-   r5   r6   rP   )r.   r/   r9   r3   r,   r4   rO   r   r(   r1   r)   r1   r-   r5   r6   rB   )r.   r/   rO   r   r(   r1   r)   r1   rV   r}   r6   r7   )r9   r   r,   r4   r6   r   )r.   r   r(   r1   r)   r1   r6   r   )r   rB   r'   r0   r   r7   r6   rB   )r9   r   r6   r   )rq   r   r6   r   r   )B__doc__
__future__r   logging	getLoggerr   rM   ri   r<   
contextlibr   os.pathr   r   r   tempfiler   typingr	   r
   r   r   #selenium.webdriver.remote.webdriverr   
core.typesr   r`   r   embedr   rO   r   r   themesr   util.warningsr   r-   r   r   utilr   rh   r   r   r   	models.uir   __all__r    r!   r"   r$   rF   r%   r#   rf   r   rG   r   rg   r|   rx   r   r   r_   r   rA   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>O >O >O >O >O >O@ FJZ[%4VZrv     " GK[\%4VZrv     " JP tSW) ) ) ) ) )V   H       *
! 
! 
! 
!   *&
 & & & & & & & &P5 5 5 5 5 5rA   