
    dK                    @   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dLd(Z1ddddddddMd*Z2ddddddddNd,Z3dde!dddd-dOd2Z4dde!dddd-dPd3Z5dde!dddd-dPd4Z6e!dddd5dQd8Z7dRd;Z8e
dSd>            Z9dTdAZ:dUdBZ;dVdEZ<dFZ=dGZ>dHZ? G dI dJ          Z@dWdKZAdS )Xz

    )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	webdrivertimeoutstateobjUIElement | Documentr&   PathLike | Noner'   
int | Noner(   r)   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 )aV   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.

        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(   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+   images           /lib/python3.11/site-packages/bokeh/io/export.pyr   r   I   s    T "#U6)]dlqrrrE#E**{a5<1,,8999y""H	JJx:h''(((    	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.

    r6   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.

    r6   r   zNo SVG Plots were found.rB   )r$   lenlogwarningrD   rE   s           r>   r!   r!      sY    L CuVIw^cdddD
4yyA~~.///	T8U333r?   )r7   r*   	resourcesr'   r(   r+   r7   rL   r   Image.Imagec                  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          |          \  }}}|                                }ddd           n# 1 swxY w Y   t          j        t!          j        |                                        d	                              d
d
||z  ||z  f                              ||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.

        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   webdriver_controlrL   r'   r(   themewutf-8modeencodingNfile://RGBAr   )r)   rP   	_tmp_htmlr   documentrR   r"   openpathwritegetmaximize_windowwait_until_render_complete_maximize_viewportr#   r   ioBytesIOconvertcropresize)r,   r7   r*   rL   r'   r(   r+   rP   tmprR   htmlfile
web_driverdprr8   s                  r>   r#   r#      s   6 -,,,,,	 1$(**.4siuV[`aaa#(w777 	4JJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  &1VV7H7L7L7N7N
""$$$+++,,,":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&%))+s7   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 Nr   rO   rQ   rS   rT   rU   rX   )r)   rP   rZ   r   r[   rR   r"   r\   r]   r^   r_   ra   r   listr4   execute_script_SVG_SCRIPTr,   r7   r*   rL   r'   r(   r+   rP   rh   rR   ri   rj   rk   rF   s                 r>   rC   rC   	  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 rn   )r)   rP   rZ   r   r[   rR   r"   r\   r]   r^   r_   ra   r   ro   r4   rp   _SVGS_SCRIPTrr   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 Krs   rQ   rR   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 %}
    r3   r4   c            	     0    t           ddd          S )N T)rL   titletemplaterR   suppress_callback_warning_always_newr   )r,   rL   r|   rR   s   r>   ri   zget_layout_html.<locals>.html@  s/    &*
 
 
 	
r?   Nr   r   zhExport method called with width or height argument on a non-Plot model. The size values will be ignored.)r3   r4   )models.plotsr   
isinstancer   _resized)r,   rL   r'   r(   rR   ri   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)rx   r   )TimeoutException)WebDriverWaitr7   r   r3   boolc                R    t          t          |                     d                    S )Nzs
            return typeof Bokeh !== "undefined" && Bokeh.documents != null && Bokeh.documents.length != 0
        r   r   rp   r7   s    r>   is_bokeh_loadedz3wait_until_render_complete.<locals>.is_bokeh_loaded]  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_completej  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.)r7   r   r3   r   )selenium.common.exceptionsr   selenium.webdriver.support.waitr   until_log_consoleRuntimeErrorrp   _WAIT_SCRIPTrJ   rK   )r7   r*   r   r   r   er   s          r>   ra   ra   V  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   z  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namer4   ir1   r3   c                8    t          |           \  }}| d| | S )N_)r   )r   r   basenamer   s       r>   _indexedz#_write_collection.<locals>._indexed  s*     #%%Q%%%%r?   r   rS   rT   rU   )r   r4   r   r1   r3   r4   )r   r:   r;   	enumerater\   r^   append)	r   r&   r   	filenamesr   r   itemfnamefs	            r>   rD   rD     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)r_   ).0rJ   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	ExceptionrI   rJ   rK   )r7   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    
++rk   tuple[int, int, int]c                    d}|                      |          }d} | j         |g|R  \  }}d}|                     ||z   ||z              |S )Nz        const root_view = Object.values(Bokeh.index)[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   )rp   set_window_size)rk   calculate_viewport_sizeviewport_sizecalculate_window_sizer'   r(   epss          r>   rb   rb     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)
    }
  }
}

const root_views = Object.values(Bokeh.index)
return [...collect_svgs(root_views)]
a  function* 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)
  }
}

const root_views = Object.values(Bokeh.index)
return [...export_svgs(root_views)]
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   _closedr1   fdr4   r]   rh   rz   prefixsuffixr   r   r3   r   c                   	 t          ||t          j                              \  | _        | _        d S # t
          $ r" t          ||          \  | _        | _        Y d S w xY w)N)r   r   dirr   )r   r:   getcwdr   r]   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   unlinkr]   r   s    r>   r   z_TempFile.close  s    < 	F	HTW 	 	 	D		Idi     	 	 	D	 s   % 
22A 
AAN)r   r4   r   r4   r3   r   r3   r   )r   r
   r   r
   r   r
   r3   r   )r3   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>   rZ   rZ     s    GG4444r?   )r,   r-   r&   r.   r'   r/   r(   r/   r)   r0   r*   r1   r+   r2   r3   r4   )r,   r-   r&   r.   r'   r/   r(   r/   r)   r0   r*   r1   r+   r2   r3   r@   )r,   r-   r&   rG   r'   r/   r(   r/   r)   r0   r*   r1   r+   r2   r3   r@   )r,   r-   r7   r0   r*   r1   rL   r   r'   r/   r(   r/   r+   r2   r3   rM   )r,   r-   r7   r0   r*   r1   rL   r   r'   r/   r(   r/   r+   r2   r3   r@   )r,   r-   rL   r   r'   r/   r(   r/   rR   rv   r3   r4   )r7   r   r*   r1   r3   r   )r,   r   r'   r/   r(   r/   r3   r   )r   r@   r&   r.   r   r4   r3   r@   )r7   r   r3   r   )rk   r   r3   r   r   )B__doc__
__future__r   logging	getLoggerr   rJ   rc   r:   
contextlibr   os.pathr   r   r   tempfiler   typingr	   r
   r   r   PILr   #selenium.webdriver.remote.webdriverr   
core.typesr   r[   r   embedr   rL   r   r   themesr   util.warningsr   r+   r   r   utilr   r   r   	models.uir   __all__r   r    r!   r#   rC   r$   r"   ra   r   rD   r   rb   ru   rq   r   r   rZ   r   r?   r>   <module>r      s     # " " " " " g!! 
			 				 % % % % % % 1 1 1 1 1 1 1 1 1 1                        >====== " ! ! ! ! !             ) ) ) ) ) ) ) )                   " " " " " " " " " " " " " " &######%%%%%% JNcg!VWos5) 5) 5) 5) 5) 5)n JNcg!VWos&4 &4 &4 &4 &4 &4P FJ_c!VWos,4 ,4 ,4 ,4 ,4 ,4d TXhi%4VZrv-+ -+ -+ -+ -+ -+^ FJZ[%4VZrv     " GK[\%4VZrv     " JP tSW) ) ) ) ) )V   H       *
! 
! 
! 
!   *( & & & & & & & &P5 5 5 5 5 5r?   