o
    沪g                     @  s   U d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZ er7ddlmZ ddlmZ ddlmZ G d	d
 d
ZdddZe
dZG dd de	e Zeddd dZded< dddZdddZdS ) z
The main purpose of this module (right now at least) is to avoid a dependency
cycle between streamlit.delta_generator and some elements.
    )annotations)
ContextVarToken)TYPE_CHECKINGCallableGenericTypeVar)RootContainer)DeltaGenerator)Dialog)StatusContainerc                   @  s   e Zd ZU dZdZded< edddZdddZe	dddZ
e	dddZe	dddZe	dddZe	d ddZe	d!ddZdS )"DeltaGeneratorSingletona  Used to initialize the DeltaGenerator classes and store them as singletons.
    This module allows us to avoid circular imports between DeltaGenerator and elements,
    because elemens can import this singleton module instead of DeltaGenerator directly.
    NzDeltaGeneratorSingleton | None	_instancereturnc                 C  s   | j du r	td| j S )zReturn the singleton DeltaGeneratorSingleton instance. Raise an Error if the
        DeltaGeneratorSingleton hasn't been created yet.
        Nz,DeltaGeneratorSingleton hasn't been created!)r   RuntimeError)cls r   `/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/delta_generator_singletons.pyinstance)   s   
z DeltaGeneratorSingleton.instancedelta_generator_clstype[DeltaGenerator]status_container_clstype[StatusContainer]dialog_container_clstype[Dialog]c                 C  sl   t jdur	td| t _|tjd| _|tj| jd| _|tj| jd| _	|tj
| jd| _|| _|| _dS )aF  Registers and initializes all delta-generator classes.

        Parameters
        ----------

        delta_generator_cls : type[DeltaGenerator]
            The main DeltaGenerator class.
        status_container_cls : type[StatusContainer]
            The delta-generator class that is used as return value for `st.status`.
        dialog_container_cls : type[Dialog]
            The delta-generator class used is used as return value for `st.dialog`.

        Raises
        ------
        RuntimeError
            If the DeltaGeneratorSingleton instance already exists.
        Nz0DeltaGeneratorSingleton instance already exists!)root_container)r   parent)r   r   r   _RootContainerMAIN_main_dgSIDEBAR_sidebar_dgEVENT	_event_dgBOTTOM
_bottom_dg_status_container_cls_dialog_container_cls)selfr   r   r   r   r   r   __init__2   s   

z DeltaGeneratorSingleton.__init__r
   c                 C     | j S N)r   r(   r   r   r   main_dgZ      zDeltaGeneratorSingleton.main_dgc                 C  r*   r+   )r!   r,   r   r   r   
sidebar_dg^   r.   z"DeltaGeneratorSingleton.sidebar_dgc                 C  r*   r+   )r#   r,   r   r   r   event_dgb   r.   z DeltaGeneratorSingleton.event_dgc                 C  r*   r+   )r%   r,   r   r   r   	bottom_dgf   r.   z!DeltaGeneratorSingleton.bottom_dgc                 C  r*   )zStub for StatusContainer. Since StatusContainer inherits from DeltaGenerator,
        this is used to avoid circular imports.
        )r&   r,   r   r   r   r   j   s   z,DeltaGeneratorSingleton.status_container_clsc                 C  r*   )ztStub for Dialog. Since Dialog inherits from DeltaGenerator,
        this is used to avoid circular imports.
        )r'   r,   r   r   r   r   s   s   z,DeltaGeneratorSingleton.dialog_container_clsr   r   )r   r   r   r   r   r   )r   r
   )r   r   )r   r   )__name__
__module____qualname____doc__r   __annotations__classmethodr   r)   propertyr-   r/   r0   r1   r   r   r   r   r   r   r   !   s$   
 
(r   r   c                   C  s   t  S )z|Return the DeltaGeneratorSingleton instance. Raise an Error if the
    DeltaGeneratorSingleton hasn't been created yet.
    )r   r   r   r   r   r   get_dg_singleton_instance{   s   r:   _Tc                   @  sL   e Zd ZdZdddZdd
dZdddZdddZdddZdddZ	dS ) ContextVarWithLazyDefaulta  The dg_stack tracks the currently active DeltaGenerator, and is pushed to when
    a DeltaGenerator is entered via a `with` block. This is implemented as a ContextVar
    so that different threads or async tasks can have their own stacks.

    We have a wrapper around it because ContextVar default cannot be a function, but
    the default dg (main_dg) might not exist yet when this module is imported.
    namestrdefaultCallable[[], _T]c                C  s   || _ || _d | _d S r+   )_name_default_context_var)r(   r=   r?   r   r   r   r)      s   
z"ContextVarWithLazyDefault.__init__r   Nonec                 C  s   t | j|  d| _d S )Nr?   )r   rA   rB   rC   r,   r   r   r   _init_context_var   s   z+ContextVarWithLazyDefault._init_context_varr;   c                 C     | j d u r	|   | j  S r+   )rC   rF   getr,   r   r   r   rH         

zContextVarWithLazyDefault.getvalue	Token[_T]c                 C  s   | j d u r	|   | j |S r+   )rC   rF   set)r(   rJ   r   r   r   rL      s   
zContextVarWithLazyDefault.settokenc                 C  s"   | j d u r	|   | j | d S r+   )rC   rF   reset)r(   rM   r   r   r   rN      s   
zContextVarWithLazyDefault.resetintc                 C  rG   r+   )rC   rF   __hash__r,   r   r   r   rP      rI   z"ContextVarWithLazyDefault.__hash__N)r=   r>   r?   r@   )r   rD   )r   r;   )rJ   r;   r   rK   )rM   rK   r   rD   )r   rO   )
r3   r4   r5   r6   r)   rF   rH   rL   rN   rP   r   r   r   r   r<      s    




r<   context_dg_stackc                   C  s
   t  jfS r+   )r:   r-   r   r   r   r   <lambda>   s   
 rR   rE   z5ContextVarWithLazyDefault[tuple[DeltaGenerator, ...]]tuple[DeltaGenerator, ...]c                  C  s    t  } | jdu rtd| jfS )zeGet the default dg_stack value with which the dg_stack should
    be initialized and reset if needed.Nzmain_dg is not set)r:   r   r   )r   r   r   r   get_default_dg_stack_value   s   
rT   DeltaGenerator | Nonec                  C  s    t  } t| dkr| d S dS )zGet the last added DeltaGenerator of the stack in the current context.

    Returns None if the stack has only one element or is empty for whatever reason.
       N)rQ   rH   len)current_stackr   r   r   "get_last_dg_added_to_context_stack   s   rZ   Nr2   )r   rS   )r   rU   )r6   
__future__r   contextvarsr   r   typingr   r   r   r   !streamlit.proto.RootContainer_pb2r	   r   streamlit.delta_generatorr
   streamlit.elements.lib.dialogr   /streamlit.elements.lib.mutable_status_containerr   r   r:   r;   r<   rQ   r7   rT   rZ   r   r   r   r   <module>   s&   
Z)


