a
    DfL4                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddl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 ddlmZ dZedd Zdd Zd&ddZdd Zdd Zdd Zdd Zdd d!dd"dd#i fd$d%Z dS )'zH
Various utilities for recording and embedding state in a rendered app.
    Ndefaultdict)contextmanager)product)Property)CustomJSWatcher   )param_watchers   )
add_to_docdiff)statez
var state = null
for (var root of cb_obj.document.roots()) {{
  if (root.id == '{id}') {{
    state = root;
    break;
  }}
}}
if (!state) {{ return; }}
state.set_state(cb_obj, {js_getter})
c                 c   s>   dd }| rt j}|t _zd V  W | r:|t _n| r8|t _0 d S )Nc                 S   s   dS )NF )selfnewoldr   r   [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/embed.pymatches*   s    zalways_changed.<locals>.matches)r   r   )enabler   backupr   r   r   always_changed(   s    r   c                 C   s2   t | dd}|d u r ddddS |j|j|jdS )NF)binary{})headermetadatacontent)r   Zheader_jsonZmetadata_jsonZcontent_json)docmsgr   r   r   record_events6   s    r    r    c              	   C   s   i }|   D ]\}}||f }	||k rBt||	|d |||||< qddd |	D d }
tj||
}tj|}tj|st| t|d}t	
|| W d    n1 s0    Y  |}|rtj||
}|||< q|S )Nr   _c                 S   s   g | ]}t |qS r   )str).0ir   r   r   
<listcomp>F       zsave_dict.<locals>.<listcomp>z.jsonw)items	save_dictjoinospathdirnameexistsmakedirsopenjsondump)r   keydepth	max_depth	save_path	load_pathZfilename_dictkvZcurkeyfilenamefilepath	directoryfZrefpathr   r   r   r*   >   s&    


*
r*   c                 C   s   dd t |  D S )Nc                 S   s(   g | ] }|  D ]}|D ]}|qqqS r   )values)r$   Z	pwatchersZ	awatchersr(   r   r   r   r&   U   s   z get_watchers.<locals>.<listcomp>)r   r?   )reactiver   r   r   get_watchersT   s    rA   c                    s   ddl m} ddlm}m} j  jfdd j D } fddt	D }t
|rdd jD  fddt	D }ng } j D ]t
|rd	_q|rt
|r|s|d
 jvst
|s|rdS t| d|d
 S )zE
    Converts Param pane widget links into JS links if possible.
    r
   )Reactive)LiteralInputWidgetc                    s   g | ]\}}| u r|qS r   r   )r$   r9   r:   widgetr   r   r&   b   r'   z#param_to_jslink.<locals>.<listcomp>c                    s$   g | ]}|j vr| j vr|qS r   _internal_callbacksr$   r(   )
param_panerF   r   r   r&   c   s   
c                 S   s   g | ]}t |d d  qS Nr   r$   lr   r   r   r&   i   r'   c                    s,   g | ]$}|j vr|vr| j vr|qS r   rG   rI   )rJ   pobj	tgt_linksr   r   r&   j   s   r2   r   Nvalue)r@   rB   widgetsrC   rD   _param_paneobjectZ_widgetsr)   rA   
isinstance_linksr?   
serializerZ_linkable_paramslink_to_jslink)modelrF   rB   rC   rD   pnameZwatcherstgt_watchersr   )rJ   rO   rP   rF   r   param_to_jslinkY   s4    

r\   c           
      C   s   | j d }|j|ddu sB|j|ddu sB||jvsB||jvrFdS ddlm}m} t|j	||d}|||d|d}	|| |	|| |	S )	z
    Converts links declared in Python into JS Links by using the
    declared forward and reverse JS transforms on the source and target.
    idFNr
   )JSLinkCallbackGeneratorLinkrQ   T)bidirectional
properties)
refZ_source_transformsgetZ_target_transforms_modelslinksr^   r_   dict_rename)
rY   sourcesrc_spectargettgt_specrc   r^   r_   rb   linkr   r   r   rX   |   s    
rX   c                    s8  ddl m} dd jD  t fddtD r<d S g }jD ]}|jfddtD }|jsz|rt|s d S g }|j	 D ]6\}}t
|d d  t| d	 v r|||f q|rF|||f qFg }	|D ]P\}}
|
D ]B\}}t| ||j|}|d u r  d S j| |	| qq|	S )
Nr
   )rD   c                 S   s   g | ]}t |d d  qS rK   r   rM   r   r   r   r&      r'   z$links_to_jslinks.<locals>.<listcomp>c                 3   s    | ]}|j vo| vV  qd S NrG   rI   )	src_linksrF   r   r   	<genexpr>   r'   z#links_to_jslinks.<locals>.<genexpr>c                    s   g | ]}| j vr|qS r   rG   rI   )rk   r   r   r&      r'   rL   rQ   )rR   rD   rV   anyrA   rk   ZtransformedrU   rf   r)   r	   r   appendrX   paramtrigger)rY   rF   rD   rf   rm   r[   ZmappingsrZ   rl   jslinksmappingrj   Zjslinkr   )ro   rk   rF   r   links_to_jslinks   s2    
 
rw   i     Fz./Tc           7   
      s  ddl m } ddlm} ddlm} ddlm ddlm} ddlm	} dd	l
m m |jd
 t| |r~| jjv r~| j} t| |st|| dS tj \}}}}|jd  fdd}fdd| |D }| }g i g   }}}|D ]djv r
qjdurdt|}|durdjjtrtfddtD s| qjr~t|}|r~qjrtfddtD rq||
 |\}}}}}}|j!} t| r|j"d j d #d| i}n(|j d }t|| s|#d| i}j$rZj$|v rZ|j$ d | |j$ d | qt%t&j'|jd
 |dd}!||g|g|||!|f |d |j$< qg }"|D ]\}#}$}}}!}|#d |v rȐq|$D ]}|(||! q|$d }%|$dd D ]4}&|#d j) dd}'|%*|'|&|' |&*|'|%|' q|"|#|$||f qt||d g |j+_,|s^dS dd |"D }(dd |"D })t-t.dd |"D  }*t/|*|kr|j0rdS t1j2j13dt/|*  fd d! }+d"},|	r||*d"t4j5d#n|*D ]}-|+}.d"}/t6|-D ]\}0}1|"|0 \}#}2}}3zBt7|j8" |#D ]}|1|_9q0W d   n1 sT0    Y  W n  t:y   d}/Y  qY n0 |.|3|2d  }.q|/rg |j+_,qd$d |"D fd%d|j+j,D |j+_,t;|}4|,|4d& d'kO },|4r|.<|4 q|,sdS t=|"|(D ]F\\}#}}}}5z|#D ]}|j1j<|5d( q&W n t:yR   Y n0 q|rd)>|t?@ jAg}6tBjC>||6}|durtBjC>||6}tD|+t/|"d ||d*}+|j<||+|)d+d, t6|"D d- |E| |S ).a0  
    Embeds the state of the application on a State model which allows
    exporting a static version of an app. This works by finding all
    widgets with a predefined set of options and evaluating the cross
    product of the widget values and recording the resulting events to
    be replayed when exported. The state is recorded on a State model
    which is attached as an additional root on the Document.

    Arguments
    ---------
    panel: panel.reactive.Reactive
      The Reactive component being exported
    model: bokeh.model.Model
      The bokeh model being exported
    doc: bokeh.document.Document
      The bokeh Document being exported
    max_states: int (default=1000)
      The maximum number of states to export
    max_opts: int (default=3)
      The max number of ticks sampled in a continuous widget like a slider
    json: boolean (default=True)
      Whether to export the data to json files
    json_prefix: str (default='')
      Prefix for JSON filename
    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
    states: dict (default={})
      A dictionary specifying the widget values to embed for each widget
    r   )tqdmr
   )config)Panelr_   )State)PaneBase)DiscreteSliderrD   r]   NZembeddedc                    s8   t | r| jrdS t |  r.| jd jv S | jv S )NFr   )rU   disabled
_compositere   )rT   )r   rD   rc   r   r   is_embeddable   s
    
z"embed_state.<locals>.is_embeddablec                    s   g | ]}| j vr|qS r   )registryrI   r|   r   r   r&      s   
zembed_state.<locals>.<listcomp>Z	compositec                 3   s$   | ]}| j vo|jj vV  qd S rn   )rH   rS   rI   )rO   rF   r   r   rp     s   zembed_state.<locals>.<genexpr>c                 3   s   | ]}| j v V  qd S rn   rG   rI   rE   r   r   rp     r'   r   type)r]   	js_getter)coderQ   Tc                 S   s   g | ]\}}}}|d  j qS r   r`   )r$   wsr"   r   r   r   r&   =  r'   c                 S   s    g | ]\}}}}||d  qS r   r   )r$   r"   msgr   r   r   r&   >  r'   c                 S   s"   g | ]\}}}}|d d d qS )Nr   r   )r$   r"   valsr   r   r   r&   ?  r'   zThe cross product of different application states is very large to explore (N=%d), consider reducing the number of options on the widgets or increase the max_states specified in the function to remove this warningc                      s   t  S rn   r   r   )nested_dictr   r   <lambda>J  r'   zembed_state.<locals>.<lambda>F)Zleavefilec                 S   s   g | ]}|d  D ]}|qqS )r   r   )r$   r:   mr   r   r   r&   _  r'   c                    s   g | ]}|j  vr|qS r   )rY   )r$   e)modelsr   r   r&   `  r'   r   r   r`   r"   )r6   r7   r8   c                 S   s(   i | ] \}\}}}}|d  j d |qS )r   r]   )rc   )r$   r%   r"   r   r   r   r   
<dictcomp>y  r'   zembed_state.<locals>.<dictcomp>)r2   r   r?   rR   )Fry   rz   Zlayoutr{   rf   r_   Zmodels.stater}   Zpaner~   rR   r   rD   rc   rU   re   r   r   Z_viewstagsrr   selectrS   r\   rT   rq   rA   rV   rw   Z_supports_embedallZ_get_embed_staterd   Z_widget_typer   Z
select_onenamer   STATE_JSformatZjs_on_changerh   Zjs_link	callbacksZ_held_eventslistr   lenZ
_doc_buildrs   mainwarningsysstdout	enumerater   Z
safe_embedrQ   	Exceptionr    updatezipr+   uuiduuid4hexr,   r-   r*   Zadd_root)7ZpanelrY   r   Z
max_statesZmax_optsr2   Zjson_prefixr7   r8   progressZstatesry   rz   r{   r}   r~   r"   Zcommr   rR   Zstate_modelZwidget_datamergedignorerm   ru   r(   Zw_modelr   getterZ	on_changer   Zw_typeZjs_callbackr?   r   Zw_modelsZwmZwmoattrrestoreZ	init_valsZcross_productZ
state_dictchangesr4   Zsub_dictskipr%   r9   r   r   eventsr:   Z
random_dirr   )r   r_   rD   r   r   rO   rc   rF   r   embed_state   s    $








" 


 .



r   )r   r   Nr!   N)!__doc__r2   r,   r   r   collectionsr   
contextlibr   	itertoolsr   rs   Zbokeh.core.property.basesr   Zbokeh.modelsr   Zparam.parameterizedr	   utilr   rY   r   r   r   r   r   r    r*   rA   r\   rX   rw   r   r   r   r   r   <module>   s6   

#$