
    -e9                       U d Z ddlmZ ddlZ ej        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rddlm!Z! dZ" G d d          Z#e		 	 d<d=d            Z$ G d d          Z%e G d d                       Z& G d! d"          Z'd>d&Z(dd'd?d+Z)d@d,Z*dAd/Z+dAd0Z,d1Z-dBd3Z.dCd4Z/dDd6Z0 e            Z1d7e2d8<   dEd:Z3dFd;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                      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__     0lib/python3.11/site-packages/bokeh/embed/util.pyr   r   G   s          	Dr&   r   FobjsSequence[Model]apply_themeTheme | type[FromCurdoc] | None
always_newboolreturnIterator[Document]c              #     K   t           t                    r,t                     dk    st          d  D                       st	          d          dd}d  D             }|rd fd}t                     }nt          |          dk    r)t                      } D ]}|                    |           nvt          |          d	k    rO|                                }t                     t          |j
                  k    rd fd
}t                     }n fd}t                     }t          j                    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              3  @   K   | ]}t          |t                    V  d S N)
isinstancer   .0xs     r'   	<genexpr>z$OutputDocumentFor.<locals>.<genexpr>   s.      BfBf\]:aQVCWCWBfBfBfBfBfBfr&   z8OutputDocumentFor expects a non-empty sequence of Modelsr.   Nonec                     d S r2   r%   r%   r&   r'   finishz!OutputDocumentFor.<locals>.finish   s    r&   c                *    h | ]}|j         	|j         S r2   )document)r5   objs     r'   	<setcomp>z$OutputDocumentFor.<locals>.<setcomp>   s!    EEESCL,DCL,D,D,Dr&   c                 &    t                      d S r2   _dispose_temp_docr(   s   r'   r:   z!OutputDocumentFor.<locals>.finish   s    d#####r&      c                 &    t                      d S r2   r@   rB   s   r'   r:   z!OutputDocumentFor.<locals>.finish   s    %d+++++r&   c                 &    t                      d S r2   r@   rB   s   r'   r:   z!OutputDocumentFor.<locals>.finish   s    !$'''''r&   N)r.   r8   )r3   r   lenall
ValueError_create_temp_doc_new_docadd_rootpopsetrootsr   perform_document_validationvalidate_set_temp_theme_unset_temp_theme)r(   r*   r,   r:   docsdocmodels   `      r'   r   r   N   s     p dH%% UTasBfBfaeBfBfBf?f?fSTTT    FEDEEED )	$ 	$ 	$ 	$ 	$ 	$t$$t99>>**C $ $U####$ YY!^^((**C 4yyC	NN**, , , , , ,&t,, ( ( ( ( ("4((C+-- C%%%
IIIc
FHHHHHr&   c                  ,    e Zd Z	 	 dddZddZddZdS )r   Ndocid	ID | Nonetoken
str | None	elementidrN   $list[Model] | dict[Model, ID] | Noneuse_for_titlebool | Nonec                    ||||t          d          |t                      }n!t          |t                    rd |D             }|| _        || _        || _        t          |          | _        || _	        d S )Nz*either docid or sessionid must be providedc                ,    i | ]}|t                      S r%   )r   r5   roots     r'   
<dictcomp>z'RenderItem.__init__.<locals>.<dictcomp>   s!    GGGT244GGGr&   )
rH   dictr3   listrW   rY   r[   r   rN   r]   )selfrW   rY   r[   rN   r]   s         r'   __init__zRenderItem.__init__   s     Mem1BuGXIJJJ=FFEEt$$ 	HGGGGGE

" ''
*r&   r.   dict[str, Any]c                    i }| j         | j         |d<   n
| j        |d<   | j        
| j        |d<   | j        r0| j                                        |d<   d | j        D             |d<   | j        
| j        |d<   |S )NrW   rY   r[   rN   c                    g | ]	}|j         
S r%   idra   s     r'   
<listcomp>z&RenderItem.to_json.<locals>.<listcomp>   s    ???D???r&   root_idsr]   )rW   rY   r[   rN   to_jsonr]   )rf   jsons     r'   ro   zRenderItem.to_json   s    !:! JDMM JDM>% $D: 	@ J..00DM??DJ???D)$($6D!r&   otherr   r-   c                    t          || j                  sdS |                                 |                                k    S NF)r3   	__class__ro   )rf   rq   s     r'   __eq__zRenderItem.__eq__   s6    %00 	55<<>>U]]__44r&   )NNNNN)
rW   rX   rY   rZ   r[   rX   rN   r\   r]   r^   )r.   rh   )rq   r   r.   r-   )r!   r"   r#   rg   ro   ru   r%   r&   r'   r   r      s[        ae]a+ + + + +"   (5 5 5 5 5 5r&   r   c                      e Zd ZU dZded<    ed          Zded<    edd          Zd	ed
<    eed          Z	de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   r[   F)comparerl    )defaultrw   rZ   name)default_factoryrw   z	list[Any]tagsc                "    | j         pd| _         d S )Nrx   )rz   rf   s    r'   __post_init__zRenderRoot.__post_init__   s    IO			r&   N)r!   r"   r#   r$   __annotations__r   rl   rz   re   r|   r   r%   r&   r'   r   r      s           MMM U5!!!B!!!! uR777D7777 eD%@@@D@@@@$ $ $ $ $r&   r   c                  D    e Zd ZddZddZd ZddZddZddZddZ	dS )r   rN   dict[Model, ID]r.   r8   c                    || _         d S r2   )_roots)rf   rN   s     r'   rg   zRenderRoots.__init__   s    r&   Iterator[RenderRoot]c              #  ^   K   t          dt          |                     D ]}| |         V  d S )Nr   )rangerF   )rf   is     r'   __iter__zRenderRoots.__iter__   s>      q#d))$$ 	 	Aq'MMMM	 	r&   c                N    t          | j                                                  S r2   )rF   r   itemsr~   s    r'   __len__zRenderRoots.__len__  s    4;$$&&'''r&   key	int | strr   c                L   t          |t                    r0t          | j                                                  |         \  }}n?| j                                        D ]\  }}|j        |k    r nt          d|d          t          ||j        |j        |j	                  S )Nz
root with z name not found)
r3   intre   r   r   rz   rH   r   rl   r|   )rf   r   rb   r[   s       r'   __getitem__zRenderRoots.__getitem__  s    c3 	F $T[%6%6%8%8 9 9# >T99#';#4#4#6#6 F Fi9##E $ !!Dc!D!D!DEEE)TWdiCCCr&   strc                ,    |                      |          S r2   )r   )rf   r   s     r'   __getattr__zRenderRoots.__getattr__  s    $$$r&   dict[ID, ID]c                H    d | j                                         D             S )Nc                $    i | ]\  }}|j         |S r%   rk   )r5   rb   r[   s      r'   rc   z'RenderRoots.to_json.<locals>.<dictcomp>  s     NNNtYNNNr&   )r   r   r~   s    r'   ro   zRenderRoots.to_json  s$    NN$+:K:K:M:MNNNNr&   c                *    t          | j                  S r2   )reprr   r~   s    r'   __repr__zRenderRoots.__repr__  s    DK   r&   N)rN   r   r.   r8   )r.   r   )r   r   r.   r   )r   r   r.   r   )r.   r   )r.   r   )
r!   r"   r#   rg   r   r   r   r   ro   r   r%   r&   r'   r   r      s              ( ( (
D 
D 
D 
D% % % %O O O O! ! ! ! ! !r&   r   modelsSequence[Model | Document]dict[ID, DocJson]c                *    t          |           \  }}|S )

    )r   )r   	docs_json_s      r'   r   r     s     9@@LIqr&   )suppress_callback_warning-Model | Document | Sequence[Model | Document]r   *tuple[dict[ID, DocJson], list[RenderItem]]c               H   t          | t          t          f          r| g} t          | t                    rt	          d | D                       st          d          t          |           r!|st                              t                     i }| D ]}t          |t                    rd}|}n|}|j
        }|t          d          ||vrt                      t                      f||<   ||         \  }}|t                      ||<   x|j        D ]}t                      ||<   i }|                                D ]!\  }\  }}	|                    d          ||<   "g }
|                                D ],\  }	\  }}|
                    t%          ||                     -||
fS )r   c              3  N   K   | ] }t          |t          t          f          V  !d S r2   )r3   r   r   r4   s     r'   r7   z8standalone_docs_json_and_render_items.<locals>.<genexpr>+  s2      0b0bVWAx?P1Q1Q0b0b0b0b0b0br&   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)rN   )r3   r   r   r   rG   rH   r   logwarning_CALLBACKS_WARNINGr<   r   rd   r   rN   r   ro   appendr   )r   r   rS   model_or_docrU   rT   rW   rN   r   r   render_itemss              r'   r   r   #  s   
 &5(+,, vx(( [S0b0b[a0b0b0b-b-b [YZZZ$V,, (5N (&'''79D B BlH-- 	mECC E.C{ !kllld??022DFF;DIc;==E%LL B B?AAeB $&I::<< 7 7ZeQ;;;66	%%'L!ZZ\\ < <>E5JuE:::;;;;|$$r&   c                    d}t          |           D ]6}t          |j                  dk    st          |j                  dk    rd} n7|S )z@ Traverses submodels to check for Python (event) callbacks

    Fr   T)r   rF   
_callbacks_event_callbacks)r   has_python_callbackrU   s      r'   r   r   R  se      ''  u  1$$E,B(C(Ca(G(G"&E )H r&   textr   c                    d}d}d}t          j        | d| d| t           j                  }|                    |           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   dollarsbracesparenspats        r'   r   r   ^  sT     GFF
*33&3363324
@
@
@C99T??$&&r&   c                    d}d}d}t          j        | d| d| t           j                  }|                    |           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   s        r'   r   r   n  sV     GFF
*33&3363324
@
@
@C::d4''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                     ddl m}  t                      } |             j        j        }|j        j                            |           |S Nr
   )curdoc)ior   r   	callbacks_js_event_callbacksupdate)r   rT   r   s      r'   rJ   rJ     sN     
**C"6IM%,,Y777Jr&   c                    t                      }| D ]E}||j        |j        <   ||_        |                                D ]}||j        |j        <   ||_        Ft          |           |_        |S r2   )rJ   r   rl   _temp_document
referencesre   r   )r   rT   mrefs       r'   rI   rI     sy    
**C % %
14<<>> 	% 	%C!$CJsv!$C	% fCJJr&   r8   c                Z    | D ]'}d |_         |                                D ]	}d |_         
(d S r2   )r   r   )r   r   r   s      r'   rA   rA     sJ     & &<<>> 	& 	&C!%C	&& &r&   z"WeakKeyDictionary[Document, Theme]_themesrT   c                    | j         t          | <   |t          u rddlm}  |            j         | _         d S t          |t                    r	|| _         d S d S r   )themer   r   r   r   r3   r   )rT   r*   r   s      r'   rQ   rQ     sd    9GCLj  FHHN				K	'	'  			   r&   c                P    | t           vrd S t           |          | _        t           | = d S r2   )r   r   )rT   s    r'   rR   rR     s(    
'CIr&   rs   )r(   r)   r*   r+   r,   r-   r.   r/   )r   r   r.   r   )r   r   r   r-   r.   r   )r   r   r.   r-   )r   r   r.   r-   )r.   r   )r   r)   r.   r   )r   r)   r.   r8   )rT   r   r*   r+   r.   r8   )rT   r   r.   r8   )5r$   
__future__r   logging	getLoggerr!   r   r   
contextlibr   typingr   r   r   r   weakrefr	   
core.typesr   document.documentr   rU   r   r   r   themes.themer   util.dataclassesr   r   util.serializationr   r   r   __all__r   r   r   r   r   r   r   r   r   r   r   rJ   rI   rA   r   r   rQ   rR   r%   r&   r'   <module>r      sI     # " " " " " g!! 
			 % % % % % %            & % % % % %       ( ( ( ( ( ( ) ) ) ) ) ) ) )                   / / / / / / / / Z Z Z Z Z Z Z Z ,++++++*	 	 	 	 	 	 	 	 \` c c c c cL*5 *5 *5 *5 *5 *5 *5 *5Z $ $ $ $ $ $ $ $2! ! ! ! ! ! ! !@    +0-% -% -% -% -% -%^
 
 
 
' ' ' ' ( ( ( (&    	 	 	 	& & & & /@.?.A.A A A A A            r&   