o
    Zh                     @  s   d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
 er*d dlmZ d dlmZ G dd deZG dd	 d	e	ZG d
d dZdS )    )annotations)Enum)TYPE_CHECKINGAnyDictList
NamedTupleOptional)DeltaGenerator)SupportsStrc                   @  s   e Zd ZdZdZdZdS )	ChildTypezEnumerator of the child type.MARKDOWN	EXCEPTIONN)__name__
__module____qualname____doc__r   r    r   r   o/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/callbacks/streamlit/mutable_expander.pyr      s    r   c                   @  s*   e Zd ZU dZded< ded< ded< dS )	ChildRecordzChild record as a NamedTuple.r   typeDict[str, Any]kwargsr
   dgN)r   r   r   r   __annotations__r   r   r   r   r      s
   
 r   c                   @  s   e Zd ZdZd9dd	Zed:ddZed;ddZd<ddZd=ddZ	dddd>ddZ
	d?dddd@d&d'Zdd(dAd+d,ZdBd1d2ZdCd5d6ZdDd7d8ZdS )EMutableExpanderzJStreamlit expander that can be renamed and dynamically expanded/collapsed.parent_containerr
   labelstrexpandedboolc                 C  s0   || _ || _| | _| j||| _g | _dS )a;  Create a new MutableExpander.

        Parameters
        ----------
        parent_container
            The `st.container` that the expander will be created inside.

            The expander transparently deletes and recreates its underlying
            `st.expander` instance when its label changes, and it uses
            `parent_container` to ensure it recreates this underlying expander in the
            same location onscreen.
        label
            The expander's initial label.
        expanded
            The expander's initial `expanded` value.
        N)_label	_expandedempty_parent_cursorexpander
_container_child_records)selfr   r   r   r   r   r   __init__   s
   

zMutableExpander.__init__returnc                 C     | j S )zExpander's label string.)r!   r(   r   r   r   r   4      zMutableExpander.labelc                 C  r+   )z6True if the expander was created with `expanded=True`.)r"   r,   r   r   r   r   9   r-   zMutableExpander.expandedNonec                 C  s   | j  | _| j  dS )zeRemove the container and its contents entirely. A cleared container can't
        be reused.
        N)r$   r#   r&   r'   clearr,   r   r   r   r/   >   s   zMutableExpander.clearotherc                 C  s(   |j  }|D ]
}| |j|j qdS )z]Append a copy of another MutableExpander's children to this
        MutableExpander.
        N)r'   copy_create_childr   r   )r(   r0   Zother_recordsrecordr   r   r   append_copyE   s   
zMutableExpander.append_copyN)	new_labelnew_expandedr5   Optional[str]r6   Optional[bool]c                C  sz   |du r| j }|du r| j}| j |kr| j|krdS || _ || _| j||| _| j}g | _|D ]
}| |j|j q0dS )z.Change the expander's label and expanded stateN)	r!   r"   r$   r%   r&   r'   r2   r   r   )r(   r5   r6   Zprev_recordsr3   r   r   r   updateM   s   zMutableExpander.updateF)helpindexbodyr   unsafe_allow_htmlr:   r;   Optional[int]intc                C  s<   |||d}|  |jdi |}ttj||}| ||S )z=Add a Markdown element to the container and return its index.)r<   r=   r:   Nr   )_get_dgmarkdownr   r   r   _add_record)r(   r<   r=   r:   r;   r   new_dgr3   r   r   r   rA   e   s   	zMutableExpander.markdown)r;   	exceptionBaseExceptionc                C  s8   d|i}|  |jdi |}ttj||}| ||S )z?Add an Exception element to the container and return its index.rD   Nr   )r@   rD   r   r   r   rB   )r(   rD   r;   r   rC   r3   r   r   r   rD   s   s   zMutableExpander.exceptionr   r   r   r   c                 C  sJ   |t jkr| jdi | dS |t jkr| jdi | dS td| )z(Create a new child with the given paramszUnexpected child type Nr   )r   r   rA   r   rD   RuntimeError)r(   r   r   r   r   r   r2   |   s
   

zMutableExpander._create_childr3   r   c                 C  s0   |dur|| j |< |S | j | t| j d S )zAdd a ChildRecord to self._children. If `index` is specified, replace
        the existing record at that index. Otherwise, append the record to the
        end of the list.

        Return the index of the added record.
        N   )r'   appendlen)r(   r3   r;   r   r   r   rB      s
   
zMutableExpander._add_recordc                 C  sD   |d urd|  krt | jk sn J d| | j| jS | jS )Nr   zBad index: )rI   r'   r   r&   )r(   r;   r   r   r   r@      s   *zMutableExpander._get_dg)r   r
   r   r   r   r    )r*   r   )r*   r    )r*   r.   )r0   r   r*   r.   )r5   r7   r6   r8   r*   r.   )F)
r<   r   r=   r    r:   r7   r;   r>   r*   r?   )rD   rE   r;   r>   r*   r?   )r   r   r   r   r*   r.   )r3   r   r;   r>   r*   r?   )r;   r>   r*   r
   )r   r   r   r   r)   propertyr   r   r/   r4   r9   rA   rD   r2   rB   r@   r   r   r   r   r      s*    


	
	
	r   N)
__future__r   enumr   typingr   r   r   r   r   r	   Zstreamlit.delta_generatorr
   Zstreamlit.type_utilr   r   r   r   r   r   r   r   <module>   s     