U
    /eJ9                     @  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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  erddlm!Z! dZ"G dd dZ#e	dAdddddddZ$G dd dZ%G dd dZ&G dd dZ'd d!d"d#d$Z(dBd%dd&d'd(d)Z)d dd"d*d+Z*d,dd-d.d/Z+d,dd-d0d1Z,d2Z-dd3d"d4d5Z.dd6d"d7d8Z/e Z0d9e1d:< d3dd6d;d<d=Z2d3d6d>d?d@Z3dS )Cz

    )annotationsN)contextmanager)TYPE_CHECKINGAnyDictIteratorListSequenceTupleType)WeakKeyDictionary   )ID)Document)Modelcollect_models)settings)Theme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%   4/tmp/pip-unpacked-wheel-f5fndrjf/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>   s     z$OutputDocumentFor.<locals>.<genexpr>z.OutputDocumentFor expects a 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 k	r|j qS r,   )document)r/   objr%   r%   r&   	<setcomp>   s     
 z$OutputDocumentFor.<locals>.<setcomp>c                     s   t   d S r,   _dispose_temp_docr%   r(   r%   r&   r4      s       c                     s   t   d S r,   r8   r%   r:   r%   r&   r4      s    c                     s   t   d S r,   r8   r%   r:   r%   r&   r4      s    N)r-   r	   lenall
ValueError_create_temp_docr   Zadd_rootpopsetrootsr   Zperform_document_validationvalidate_set_temp_theme_unset_temp_theme)r(   r)   r*   r4   docsdocmodelr%   r:   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 kr|d ks |d k	r(|d k	r(t d|d k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>   s      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]r3   c                 C  sx   i }| j d k	r| j |d< n
| j|d< | jd k	r8| j|d< | jr`| j |d< dd | jD |d< | jd k	rt| j|d< |S )	NrJ   rK   rL   rB   c                 S  s   g | ]
}|j qS r%   idrN   r%   r%   r&   
<listcomp>   s     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   r[   r%   r%   r&   __eq__   s    zRenderItem.__eq__)NNNNN)r!   r"   r#   rT   rX   r_   r%   r%   r%   r&   r      s       r   c                   @  sF   e Zd ZdZddddddddd	Zd
ddddZd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.

    Nr   rI   zList[Any] | Noner2   )rL   rV   nametagsr+   c                 C  s$   || _ || _|pd| _|pg | _d S )N rL   rV   r`   ra   )rS   rL   rV   r`   ra   r%   r%   r&   rT      s    
zRenderRoot.__init__r   r'   rZ   c                 C  s    t || jsdS | j|jkS d S r\   )r-   r]   rL   r^   r%   r%   r&   r_      s    zRenderRoot.__eq__strr3   c              	   C  s&   d| j  d| j d| j d| j d	S )NzRenderRoot(elementid=z, id=z, name=z, tags=)rc   rS   r%   r%   r&   __repr__   s    zRenderRoot.__repr__)NN)r!   r"   r#   r$   rT   r_   rg   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]r2   )rB   r+   c                 C  s
   || _ d S r,   )_roots)rS   rB   r%   r%   r&   rT     s    zRenderRoots.__init__zIterator[RenderRoot]r3   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<   rh   itemsrf   r%   r%   r&   __len__  s    zRenderRoots.__len__z	int | strr   )keyr+   c                 C  sd   t |tr"t| j | \}}n.| j D ]\}}|j|kr, qPq,td| t||j|j|j	S )Nzroot with '%s' name not found)
r-   intrR   rh   rl   r`   r>   r   rV   ra   )rS   rn   rO   rL   r%   r%   r&   __getitem__  s    

zRenderRoots.__getitem__rd   c                 C  s
   |  |S r,   )rp   )rS   rn   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     s      z'RenderRoots.to_json.<locals>.<dictcomp>)rh   rl   rf   r%   r%   r&   rX     s    zRenderRoots.to_jsonc                 C  s
   t | jS r,   )reprrh   rf   r%   r%   r&   rg      s    zRenderRoots.__repr__N)
r!   r"   r#   rT   rk   rm   rp   rq   rX   rg   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   z-Model | Document | Sequence[Model | Document]z*Tuple[Dict[ID, DocJson], List[RenderItem]])rs   suppress_callback_warningr+   c                 C  s4  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krtd||krt t f||< || \}}|dk	rt ||< qV|jD ]}t ||< qqVi }| 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&   r1   2  s     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 content)rB   )r-   r   r   r	   r=   r>   r    logwarning_CALLBACKS_WARNINGr5   r   rQ   rB   rl   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    Y  s    r    rd   )textr+   c                 C  s:   d}d}d}t j| d| d| t jd}|| dk	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   e  s
    	 r   c                 C  s:   d}d}d}t j| d| d| t jd}|| dk	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   u  s
     r   aD  
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/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   c                 C  sP   t  }| D ]6}||j|j< ||_| D ]}||j|j< ||_q(q
t| |_|S r,   )r   rs   rV   _temp_document
referencesrR   rh   )rs   rG   mrefr%   r%   r&   r?     s    

r?   r2   c                 C  s(   | D ]}d |_ | D ]
}d |_ qqd S r,   )r   r   )rs   r   r   r%   r%   r&   r9     s    r9   z"WeakKeyDictionary[Document, Theme]_themes)rG   r)   r+   c                 C  sB   | j t| < |tkr.ddlm} | | j | _ nt|tr>|| _ d S )Nr   )curdoc)themer   r   ior   r-   r   )rG   r)   r   r%   r%   r&   rD     s    
 
rD   )rG   r+   c                 C  s    | t krd S t |  | _t | = d S r,   )r   r   )rG   r%   r%   r&   rE     s    
rE   )NF)F)4r$   
__future__r   logging	getLoggerr!   rx   r   
contextlibr   typingr   r   r   r   r   r	   r
   r   weakrefr   Z
core.typesr   Zdocument.documentr   rH   r   r   r   Zthemes.themer   Zutil.serializationr   r   __all__r   r   r   r   r   r   r   r    r   r   rz   r?   r9   r   __annotations__rD   rE   r%   r%   r%   r&   <module>   sH   
(
  f-  /