a
    Dff$                     @  s  d Z ddlmZ ddlZddl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 ddlmZmZ ddlm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  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* ddl+m+Z+ er&ddl,m-Z- ddl.m/Z/ ddl0m1Z1 dZ2e2ejj3_2edddfdddddddZ4dddd d!d"Z5de&i dd#fd$d%d&d'd(d)d*d+d,d-Z6ddddd#d.d/d#d0d1dd2i dfd3d4d&d5d6d(d*ddd*ddd&d*dd7d8d9Z7dS ):zB
Defines utilities to save panel objects to files as HTML or PNG.
    )annotationsN)IOTYPE_CHECKINGAnyDictIterableListOptional)Document)html_page_for_render_items)OutputDocumentFor%standalone_docs_json_and_render_items)get_screenshot_as_png)Model)CDNINLINE	Resources)Comm   )config   )embed_state)
add_to_doc)BASE_TEMPLATECDN_DISTDEFAULT_TITLEr   bundle_resourcesset_resource_mode)state)	ThemeLike)Template)Viewablea  
// add private window prop to check that render is complete
window._bokeh_render_complete = false;
function done() {
  setTimeout(() => { window._bokeh_render_complete = true; }, 500);
}

var doc = Bokeh.documents[0];

if (doc.is_idle)
  done();
else
  doc.idle.connect(done);
   r   strintNone)modelfilenametimeoutreturnc                   s   ddl m} tjs| t_tj} du r.d zz` fdd}tjjj}	|tjj_t	| |||d}
|
j
dksx|
jdkrtd|
j|d	d
 W n ty    Y n0 W  r|	tjj_n r|	tjj_0 dS )a  
    Saves a bokeh model to png

    Arguments
    ---------
    model: bokeh.model.Model
      Model to save to png
    filename: str
      Filename to save to
    resources: str
      Resources
    template:
      template file, as used by bokeh.file_html. If None will use bokeh defaults
    template_variables:
      template_variables file dict, as used by bokeh.file_html
    timeout: int
      The maximum amount of time (in seconds) to wait for
    r   )webdriver_controlNa5  \
        {% block preamble %}
        <style>
        html, body {
        box-sizing: border-box;
            width: 100%;
            height: 100%;
            margin: 0;
            border: 0;
            padding: 0;
            overflow: hidden;
        }
        </style>
        {% endblock %}
        c                   s"   t |}t| |d pi ddS )N T)titletemplatetemplate_variables_always_new)r   
from_bokeh	file_html)obj	resourceswidthheightkwargsr-   r.    Z/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/save.pyget_layout_htmlk   s    
z!save_png.<locals>.get_layout_html)Zdriverr(   r3   zunable to save an empty imagepng)format)Zbokeh.io.webdriverr*   r   	webdrivercreatebokehioexportr:   r   r4   r5   
ValueErrorsave	Exception)r&   r'   r3   r-   r.   r(   r*   r=   r:   Zold_layout_fnimgr8   r7   r9   save_png=   s(    



rF   zIterable[Model])modelsr,   r)   c                 C  sN   |d ur|S | D ]}t |tr|j  S q| D ]}|jd ur.|jj  S q.tS )N)
isinstancer
   r,   documentr   )rG   r,   pr8   r8   r9   _title_from_models   s    

rK   FzModel | Document | List[Model]zResources | NonezOptional[str]zTemplate | strzDict[str, Any]r   bool)rG   r3   r,   r-   r.   themer/   c              	   C  s   g }t | tr| g}nt | tr(| j}n| }t|d< t|||dH t|dd\}}	t||}t||}
t	|
||	|||dW  d    S 1 s0    Y  d S )NZdist_url)Zapply_themeZ
always_newT)Zsuppress_callback_warning)r,   r-   r.   )
rH   r   r
   rootsr   r   r   rK   r   r   )rG   r3   r,   r-   r.   rM   r/   Z
models_seqZ	docs_jsonZrender_itemsZbundler8   r8   r9   r1      s"    





r1   i     r+   z./Tr!   zstr | os.PathLike | IOzBkResources | NonezTemplate | str | None)panelr'   r,   r3   r-   r.   embed
max_statesmax_opts
embed_jsonjson_prefix	save_path	load_pathprogressr)   c                 K  s  ddl m} ddlm} t| |r6t| jdkr6| j} |du rRt|toP|d}t| t	rb| }nt	 }|du rzt
}d}nTt|tr| dkrt
}d}q| dkrt}d}qtd	| nt|tr|j}t }tj|d
 t| t	r| }nt| |r<t| | j||d W d   n1 s,0    Y  |}n<| ||}|rlt| |||||	|
|||| nt||d W d   n1 s0    Y  |rt|f||||d|S t|tr|ds|d }i }|du rd}|r||d< |r||d< tj|dd}t|$ t|||fi |}W d   n1 sH0    Y  t|drt|tjrv|d}| | n<tj!|ddd}| | W d   n1 s0    Y  dS )a  
    Saves Panel objects to file.

    Arguments
    ---------
    panel: Viewable
      The Panel Viewable to save to file
    filename: str or file-like object
      Filename to save the plot to
    title: str
      Optional title for the plot
    resources: bokeh.resources.Resources
      One of the valid bokeh.resources (e.g. CDN or INLINE)
    template: jinja2.Template | str
      template file, as used by bokeh.file_html. If None will use bokeh defaults
    template_variables: Dict[str, Any]
      template_variables file dict, as used by bokeh.file_html
    embed: bool
      Whether the state space should be embedded in the saved file.
    max_states: int
      The maximum number of states to embed
    max_opts: int
      The maximum number of states for a single widget
    embed_json: boolean (default=True)
      Whether to export the data to json files
    json_prefix: str (default='')
      Prefix for the randomly json directory
    save_path: str (default='./')
      The path to save json files to
    load_path: str (default=None)
      The path or URL the json files will be loaded from.
    progress: boolean (default=True)
      Whether to report progress
    embed_states: dict (default={})
      A dictionary specifying the widget values to embed for each widget
    as_png: boolean (default=None)
        To save as a .png. If None save_png will be true if filename is
        string and ends with png.
    r   )PaneBase)BaseTemplater   Nr;   Zcdninlinez>Resources %r not recognized, specify one of 'CDN' or 'INLINE'.)rQ   )r,   T)r3   r'   r-   r.   z.htmlZPanelr-   r.   )absolutewritezutf-8w)modeencoding)"ZpanerY   r-   rZ   rH   lenZlayoutr#   endswithr
   r   lowerr   rB   BkResourcesr_   r   r   setr   Z	_init_docZget_rootr   r   rF   r   r0   r1   hasattrr@   BytesIOencoder]   open)rP   r'   r,   r3   r-   r.   rQ   rR   rS   rT   rU   rV   rW   rX   Zembed_statesZas_pngr6   rY   rZ   docr_   Zcommr&   htmlfr8   r8   r9   rC      s    0




.
,

4
rC   )8__doc__
__future__r   r@   ostypingr   r   r   r   r   r   r	   r?   Zbokeh.document.documentr
   Zbokeh.embed.elementsr   Zbokeh.embed.utilr   r   Zbokeh.io.exportr   Zbokeh.modelr   Zbokeh.resourcesr   r   r   rd   Zpyviz_commsr   r   rQ   r   r&   r   r3   r   r   r   r   r   r   Zbokeh.embed.standaloner   Zjinja2r    Zviewabler!   Z_WAIT_SCRIPTrA   rF   rK   r1   rC   r8   r8   r8   r9   <module>   sL   $ 
C 