a
    @Df9                     @  s  U d Z ddlmZ ddlZeeZddlZddlm	Z	 ddl
mZ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mZ ddlmZ ddlmZ ddlmZmZ ddlmZm Z  erddlm!Z! dZ"G dd dZ#e	dFdddddddZ$G dd dZ%eG dd dZ&G dd  d Z'd!d"d#d$d%Z(dd&d'dd(d)d*d+Z)d!dd#d,d-Z*d.dd/d0d1Z+d.dd/d2d3Z,d4Z-d5d6d7d8Z.dd5d#d9d:Z/dd;d#d<d=Z0e Z1d>e2d?< d5dd;d@dAdBZ3d5d;dCdDdEZ4dS )Gz

    )annotationsN)contextmanager)TYPE_CHECKINGAnyIteratorSequence)WeakKeyDictionary   )ID)Document)Modelcollect_models)settings)Theme)	dataclassfield) make_globally_unique_css_safe_idmake_globally_unique_id)DocJson)
contains_tex_string
FromCurdocis_tex_stringOutputDocumentFor
RenderItem
RenderRootRenderRootsstandalone_docs_json%standalone_docs_json_and_render_itemssubmodel_has_python_callbacksc                   @  s   e Zd ZdZdS )r   z This class merely provides a non-None default value for ``theme``
    arguments, since ``None`` itself is a meaningful value for users to pass.

    N)__name__
__module____qualname____doc__ r#   r#   ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/bokeh/embed/util.pyr   J   s   r   FzSequence[Model]zTheme | type[FromCurdoc] | NoneboolzIterator[Document])objsapply_theme
always_newreturnc                 #  s  t  tr(t dks(tdd  D s0tddddd}d	d
  D }|rldd fdd}t }n~t|dkrt } D ]}|| qnVt|dkr| }t	 t	|j
krdd fdd}t }n fdd}t }t r|  t|| |V  t| |  dS )a&   Find or create a (possibly temporary) Document to use for serializing
    Bokeh content.

    Typical usage is similar to:

    .. code-block:: python

         with OutputDocumentFor(models):
            (docs_json, [render_item]) = standalone_docs_json_and_render_items(models)

    Inside the context manager, the models will be considered to be part of a single
    Document, with any theme specified, which can thus be serialized as a unit. Where
    possible, OutputDocumentFor attempts to use an existing Document. However, this is
    not possible in three cases:

    * If passed a series of models that have no Document at all, a new Document will
      be created, and all the models will be added as roots. After the context manager
      exits, the new Document will continue to be the models' document.

    * If passed a subset of Document.roots, then OutputDocumentFor temporarily "re-homes"
      the models in a new bare Document that is only available inside the context manager.

    * If passed a list of models that have different documents, then OutputDocumentFor
      temporarily "re-homes" the models in a new bare Document that is only available
      inside the context manager.

    OutputDocumentFor will also perfom document validation before yielding, if
    ``settings.perform_document_validation()`` is True.


        objs (seq[Model]) :
            a sequence of Models that will be serialized, and need a common document

        apply_theme (Theme or FromCurdoc or None, optional):
            Sets the theme for the doc while inside this context manager. (default: None)

            If None, use whatever theme is on the document that is found or created

            If FromCurdoc, use curdoc().theme, restoring any previous theme afterwards

            If a Theme instance, use that theme, restoring any previous theme afterwards

        always_new (bool, optional) :
            Always return a new document, even in cases where it is otherwise possible
            to use an existing document on models.

    Yields:
        Document

    r   c                 s  s   | ]}t |tV  qd S N)
isinstancer   .0xr#   r#   r$   	<genexpr>       z$OutputDocumentFor.<locals>.<genexpr>z8OutputDocumentFor expects a non-empty sequence of ModelsNoner)   c                   S  s   d S r*   r#   r#   r#   r#   r$   finish   s    z!OutputDocumentFor.<locals>.finishc                 S  s   h | ]}|j d ur|j qS r*   )document)r-   objr#   r#   r$   	<setcomp>   r0   z$OutputDocumentFor.<locals>.<setcomp>c                     s   t   d S r*   _dispose_temp_docr#   r&   r#   r$   r3      s       c                     s   t   d S r*   r7   r#   r9   r#   r$   r3      s    c                     s   t   d S r*   r7   r#   r9   r#   r$   r3      s    N)r+   r   lenall
ValueError_create_temp_doc_new_docZadd_rootpopsetrootsr   Zperform_document_validationvalidate_set_temp_theme_unset_temp_theme)r&   r'   r(   r3   docsdocmodelr#   r9   r$   r   Q   s2    8(

r   c                   @  sB   e Zd ZdddddddddZd	d
ddZdddddZdS )r   Nz	ID | None
str | Nonez$list[Model] | dict[Model, ID] | Nonezbool | None)docidtoken	elementidrB   use_for_titlec                 C  sv   |d u r|d u s |d ur(|d ur(t d|d u r8t }nt|trPdd |D }|| _|| _|| _t|| _|| _	d S )Nz*either docid or sessionid must be providedc                 S  s   i | ]}|t  qS r#   )r   r-   rootr#   r#   r$   
<dictcomp>   r0   z'RenderItem.__init__.<locals>.<dictcomp>)
r=   dictr+   listrJ   rK   rL   r   rB   rM   )selfrJ   rK   rL   rB   rM   r#   r#   r$   __init__   s     

zRenderItem.__init__zdict[str, Any]r2   c                 C  sx   i }| j d ur| j |d< n
| j|d< | jd ur8| j|d< | jr`| j |d< dd | jD |d< | jd urt| j|d< |S )	NrJ   rK   rL   rB   c                 S  s   g | ]
}|j qS r#   idrN   r#   r#   r$   
<listcomp>   r0   z&RenderItem.to_json.<locals>.<listcomp>Zroot_idsrM   )rJ   rK   rL   rB   to_jsonrM   )rS   jsonr#   r#   r$   rX      s    





zRenderItem.to_jsonr   r%   )otherr)   c                 C  s$   t || jsdS |  | kS d S )NF)r+   	__class__rX   )rS   rZ   r#   r#   r$   __eq__   s    zRenderItem.__eq__)NNNNN)r   r    r!   rT   rX   r\   r#   r#   r#   r$   r      s
     r   c                   @  s\   e Zd ZU dZded< eddZded< edddZd	ed
< eeddZ	ded< dd Z
dS )r   z Encapsulate data needed for embedding a Bokeh document root.

    Values for ``name`` or ``tags`` are optional. They may be useful for
    querying a collection of roots to find a specific one to embed.

    r
   rL   F)comparerV    )defaultr]   rI   name)default_factoryr]   z	list[Any]tagsc                 C  s   | j pd| _ d S )Nr^   )r`   rS   r#   r#   r$   __post_init__   s    zRenderRoot.__post_init__N)r   r    r!   r"   __annotations__r   rV   r`   rR   rb   rd   r#   r#   r#   r$   r      s   
r   c                   @  sn   e Zd ZdddddZdddd	Zd
d ZdddddZdddddZddddZddddZ	dS )r   zdict[Model, ID]r1   )rB   r)   c                 C  s
   || _ d S r*   )_roots)rS   rB   r#   r#   r$   rT      s    zRenderRoots.__init__zIterator[RenderRoot]r2   c                 c  s"   t dt| D ]}| | V  qd S )Nr   )ranger;   )rS   ir#   r#   r$   __iter__  s    zRenderRoots.__iter__c                 C  s   t | j S r*   )r;   rf   itemsrc   r#   r#   r$   __len__  s    zRenderRoots.__len__z	int | strr   )keyr)   c                 C  sh   t |tr"t| j | \}}n2| j D ]\}}|j|kr, qTq,td|dt||j|j|j	S )Nz
root with z name not found)
r+   intrR   rf   rj   r`   r=   r   rV   rb   )rS   rl   rO   rL   r#   r#   r$   __getitem__
  s    

zRenderRoots.__getitem__strc                 C  s
   |  |S r*   )rn   )rS   rl   r#   r#   r$   __getattr__  s    zRenderRoots.__getattr__zdict[ID, ID]c                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|j |qS r#   rU   )r-   rO   rL   r#   r#   r$   rP     r0   z'RenderRoots.to_json.<locals>.<dictcomp>)rf   rj   rc   r#   r#   r$   rX     s    zRenderRoots.to_jsonc                 C  s
   t | jS r*   )reprrf   rc   r#   r#   r$   __repr__  s    zRenderRoots.__repr__N)
r   r    r!   rT   ri   rk   rn   rp   rX   rr   r#   r#   r#   r$   r      s   r   zSequence[Model | Document]zdict[ID, DocJson])modelsr)   c                 C  s   t | \}}|S )

    )r   )rs   	docs_json_r#   r#   r$   r     s    r   )suppress_callback_warningz-Model | Document | Sequence[Model | Document]z*tuple[dict[ID, DocJson], list[RenderItem]])rs   rw   r)   c                C  s8  t | ttfr| g} t | tr0tdd | D s8tdt| rN|sNtt	 i }| D ]}t |trnd}|}n|}|j
}|du rtd||vrt t f||< || \}}|durt ||< qV|jD ]}t ||< qqVi }| D ]\}\}}	|jdd||< qg }
| D ] \}	\}}|
t||d q||
fS )	rt   c                 s  s   | ]}t |ttfV  qd S r*   )r+   r   r   r,   r#   r#   r$   r/   .  r0   z8standalone_docs_json_and_render_items.<locals>.<genexpr>z>Expected a Model, Document, or Sequence of Models or DocumentsNzHA Bokeh Model must be part of a Document to render as standalone contentF)deferred)rB   )r+   r   r   r   r<   r=   r   logwarning_CALLBACKS_WARNINGr4   r   rQ   r   rB   rj   rX   appendr   )rs   rw   rF   Zmodel_or_docrH   rG   rJ   rB   ru   rv   Zrender_itemsr#   r#   r$   r   &  s:    


r   c                 C  s:   d}t | D ](}t|jdks,t|jdkrd} q6q|S )z@ Traverses submodels to check for Python (event) callbacks

    Fr   T)r   r;   Z
_callbacksZ_event_callbacks)rs   Zhas_python_callbackrH   r#   r#   r$   r   U  s    r   ro   )textr)   c                 C  s:   d}d}d}t j| d| d| t jd}|| duS )z Whether a string begins and ends with MathJax default delimiters

    Args:
        text (str): String to check

    Returns:
        bool: True if string begins and ends with delimiters, False if not
    z^\$\$.*?\$\$$z^\\\[.*?\\\]$z^\\\(.*?\\\)$|flagsN)recompileSmatchr}   ZdollarsZbracesparenspatr#   r#   r$   r   a  s
    	 r   c                 C  s:   d}d}d}t j| d| d| t jd}|| duS )z Whether a string contains any pair of MathJax default delimiters
    Args:
        text (str): String to check
    Returns:
        bool: True if string contains delimiters, False if not
    z\$\$.*?\$\$z\\\[.*?\\\]z\\\(.*?\\\)r~   r   N)r   r   r   searchr   r#   r#   r$   r   q  s
     r   aG  
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

    https://docs.bokeh.org/en/latest/docs/user_guide/interaction/js_callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

    https://docs.bokeh.org/en/latest/docs/user_guide/server.html
r   r2   c                  C  s.   ddl m}  t }|  jj}|jj| |S Nr	   )curdoc)ior   r   	callbacksZ_js_event_callbacksupdate)r   rG   r   r#   r#   r$   r?     s
    
r?   c                 C  sP   t  }| D ]6}||j|j< ||_| D ]}||j|j< ||_q(q
t| |_|S r*   )r?   rs   rV   _temp_document
referencesrR   rf   )rs   rG   mrefr#   r#   r$   r>     s    

r>   r1   c                 C  s(   | D ]}d |_ | D ]
}d |_ qqd S r*   )r   r   )rs   r   r   r#   r#   r$   r8     s    r8   z"WeakKeyDictionary[Document, Theme]_themes)rG   r'   r)   c                 C  s>   | j t| < |tu r*ddlm} | j | _ nt|tr:|| _ d S r   )themer   r   r   r   r+   r   )rG   r'   r   r#   r#   r$   rD     s    

rD   )rG   r)   c                 C  s    | t vrd S t |  | _t | = d S r*   )r   r   )rG   r#   r#   r$   rE     s    
rE   )NF)5r"   
__future__r   logging	getLoggerr   ry   r   
contextlibr   typingr   r   r   r   weakrefr   Z
core.typesr
   Zdocument.documentr   rH   r   r   r   Zthemes.themer   Zutil.dataclassesr   r   Zutil.serializationr   r   r   __all__r   r   r   r   r   r   r   r   r   r   r{   r?   r>   r8   r   re   rD   rE   r#   r#   r#   r$   <module>   sN   
  f- /	