o
    h0                     @  s   d dl mZ d dlZd dlmZmZ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 e
r>d d	lmZ G d
d deZeddG dd dZeddG dd dZd ddZd!ddZG dd dZdS )"    )annotationsN)	dataclassfieldreplace)Enum)TYPE_CHECKINGcast)util)ChatInputValue)WidgetStateWidgetStates)ContextInfoc                   @  s   e Zd ZdZdZdZdS )ScriptRequestTypeCONTINUESTOPRERUNN)__name__
__module____qualname__r   r   r    r   r   h/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/scriptrunner_utils/script_requests.pyr      s    r   T)frozenc                   @  s   e Zd ZU dZdZded< dZded< dZded< dZded	< dZ	d
ed< e
edZded< dZded< dZded< dZded< dddZdS )	RerunDataz+Data attached to RERUN requests. Immutable. strquery_stringNWidgetStates | Nonewidget_statespage_script_hash	page_namez
str | Nonefragment_id)default_factory	list[str]fragment_id_queueFboolis_fragment_scoped_rerunis_auto_rerunzContextInfo | Nonecontext_inforeturnc                 C  
   t | S Nr	   repr_selfr   r   r   __repr__?      
zRerunData.__repr__r(   r   )r   r   r   __doc__r   __annotations__r   r   r   r    r   listr#   r%   r&   r'   r/   r   r   r   r   r   ,   s   
 r   c                   @  s>   e Zd ZU dZded< dZded< edd	d
ZdddZdS )ScriptRequestz,A STOP or RERUN request and associated data.r   typeNzRerunData | None_rerun_datar(   r   c                 C  s    | j tjur
tdtd| jS )Nz)RerunData is only set for RERUN requests.r   )r6   r   r   RuntimeErrorr   r7   r-   r   r   r   
rerun_dataJ   s   zScriptRequest.rerun_datar   c                 C  r)   r*   r+   r-   r   r   r   r/   P   r0   zScriptRequest.__repr__)r(   r   r1   )	r   r   r   r2   r3   r7   propertyr9   r/   r   r   r   r   r5   C   s   
 r5   r#   r"   r%   r$   r(   c                 C  s   t | o| S )a7  Returns whether the currently running script should be preempted due to a
    fragment rerun.

    Reruns corresponding to fragment runs that weren't caused by calls to
    `st.rerun(scope="fragment")` should *not* cancel the current script run
    as doing so will affect elements outside of the fragment.
    )r$   )r#   r%   r   r   r   '_fragment_run_should_not_preempt_scriptT   s   r;   
old_statesr   
new_statesc           	      C  s   | s|sdS | s
|S |s| S dd |j D }ddtddfg}| j D ]2}|D ]-\}}|d|krSt|||krS||j}|rN|d|krSt|||krS|||j< q&q"t }|j |  |S )ad  Coalesce an older WidgetStates into a newer one, and return a new
    WidgetStates containing the result.

    For most widget values, we just take the latest version.

    However, any trigger_values (which are set by buttons) that are True in
    `old_states` will be set to True in the coalesced result, so that button
    presses don't go missing.
    Nc                 S  s   i | ]}|j |qS r   )id).0wstater   r   r   
<dictcomp>u   s    z+_coalesce_widget_states.<locals>.<dictcomp>)trigger_valueFchat_input_value)datavalue)	widgetsChatInputValueProto
WhichOneofgetattrgetr>   r   extendvalues)	r<   r=   states_by_idtrigger_value_types	old_statetrigger_value_typeunset_valuenew_trigger_val	coalescedr   r   r   _coalesce_widget_statesb   s4   

rT   c                   @  s@   e Zd ZdZdd ZdddZdddZdddZdddZdS )ScriptRequestszAn interface for communicating with a ScriptRunner. Thread-safe.

    AppSession makes requests of a ScriptRunner through this class, and
    ScriptRunner handles those requests.
    c                 C  s   t  | _tj| _t | _d S r*   )	threadingLock_lockr   r   _stater   r7   r-   r   r   r   __init__   s   
zScriptRequests.__init__r(   Nonec                 C  s4   | j  tj| _W d   dS 1 sw   Y  dS )zRequest that the ScriptRunner stop running. A stopped ScriptRunner
        can't be used anymore. STOP requests succeed unconditionally.
        N)rX   r   r   rY   r-   r   r   r   request_stop   s   
"zScriptRequests.request_stopnew_datar   r$   c                 C  s  | j  | jtjkr	 W d   dS | jtjkr5tj| _|jr)t|d|jgd}|| _	 W d   dS | jtjkr}t	| jj
|j
}|jrXg | jj}|j|vrW||j n	|jr_|j}ng }t|j||j|j||j|j|jd| _	 W d   dS td| j 1 sw   Y  dS )a5  Request that the ScriptRunner rerun its script.

        If the ScriptRunner has been stopped, this request can't be honored:
        return False.

        Otherwise, record the request and return True. The ScriptRunner will
        handle the rerun request as soon as it reaches an interrupt point.
        NF)r    r#   T)r   r   r   r   r#   r%   r&   r'   z Unrecognized ScriptRunnerState: )rX   rY   r   r   r   r   r    r   r7   rT   r   r#   appendr   r   r   r   r%   r&   r'   r8   )r.   r]   coalesced_statesr#   r   r   r   request_rerun   sR   


=zScriptRequests.request_rerunScriptRequest | Nonec                 C  s   | j tjks| j tjkrt| jj| jjrdS | j? | j tjkrEt| jj| jjr3	 W d   dS tj| _ t	tj| jW  d   S | j tj
ksMJ t	tj
W  d   S 1 s\w   Y  dS )a  Called by the ScriptRunner when it's at a yield point.

        If we have no request or a RERUN request corresponding to one or more fragments
        (that is not a fragment-scoped rerun), return None.

        If we have a (full script or fragment-scoped) RERUN request, return the request
        and set our internal state to CONTINUE.

        If we have a STOP request, return the request and remain stopped.
        N)rY   r   r   r   r;   r7   r#   r%   rX   r5   r   r-   r   r   r   on_scriptrunner_yield   s,   

$z$ScriptRequests.on_scriptrunner_yieldr5   c                 C  sl   | j ) | jtjkrtj| _ttj| jW  d   S tj| _ttjW  d   S 1 s/w   Y  dS )aY  Called by the ScriptRunner when it's about to run its script for
        the first time, and also after its script has successfully completed.

        If we have a RERUN request, return the request and set
        our internal state to CONTINUE.

        If we have a STOP request or no request, set our internal state
        to STOP.
        N)rX   rY   r   r   r   r5   r7   r   r-   r   r   r   on_scriptrunner_ready  s   
$z$ScriptRequests.on_scriptrunner_readyN)r(   r[   )r]   r   r(   r$   )r(   ra   )r(   r5   )	r   r   r   r2   rZ   r\   r`   rb   rc   r   r   r   r   rU      s    


I(rU   )r#   r"   r%   r$   r(   r$   )r<   r   r=   r   r(   r   )
__future__r   rV   dataclassesr   r   r   enumr   typingr   r   	streamlitr	   streamlit.proto.Common_pb2r
   rG    streamlit.proto.WidgetStates_pb2r   r   streamlit.proto.ClientState_pb2r   r   r   r5   r;   rT   rU   r   r   r   r   <module>   s$   

9