
    g|/                       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
 d dlmZ d dlmZ d dlmZmZ  G d	 d
e      Z ed       G d d             Z ed       G d d             Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ G d d      Zy)    )annotationsN)	dataclassfieldreplace)Enum)cast)util)StringTriggerValue)WidgetStateWidgetStatesc                      e Zd ZdZdZdZy)ScriptRequestTypeCONTINUESTOPRERUNN)__name__
__module____qualname__r   r   r        i/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner_utils/script_requests.pyr   r      s    H
 D Er   r   T)frozenc                      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      Zded<   dZded<   dZded<   ddZy)	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_rerunc                ,    t        j                  |       S Nr	   repr_selfs    r   __repr__zRerunData.__repr__:       zz$r   returnr   )r   r   r   __doc__r   __annotations__r   r    r!   r"   r   listr%   r'   r(   r/   r   r   r   r   r   )   se    5L#)-M&-cIs #K"#(#>y>%*d*M4 r   r   c                  D    e Zd ZU dZded<   dZded<   ed	d       Zd
dZy)ScriptRequestz,A STOP or RERUN request and associated data.r   typeNzRerunData | None_rerun_datac                    | j                   t        j                  urt        d      t	        t
        | j                        S )Nz)RerunData is only set for RERUN requests.)r8   r   r   RuntimeErrorr   r   r9   r-   s    r   
rerun_datazScriptRequest.rerun_dataE   s4    99-333JKKIt//00r   c                ,    t        j                  |       S r*   r+   r-   s    r   r/   zScriptRequest.__repr__K   r0   r   )r2   r   r1   )	r   r   r   r3   r4   r9   propertyr<   r/   r   r   r   r7   r7   >   s-    6
$(K!(1 1
 r   r7   c                "    t        |       xr | 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'   s     r   '_fragment_run_should_not_preempt_scriptr@   O   s     !"C+C'CCr   c                   | s|sy| s|S |s| S |j                   D ci c]  }|j                  | }}ddt        d      fg}| j                   D ]  }|D ]{  \  }}|j                  d      |k(  st	        ||      |k7  s+|j                  |j                        }|r%|j                  d      |k(  s]t	        ||      |k(  sm|||j                  <   }  t               }	|	j                   j                  |j                                |	S c c}w )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.
    N)trigger_valueFstring_trigger_value)datavalue)	widgetsidStringTriggerValueProto
WhichOneofgetattrgetr   extendvalues)

old_states
new_stateswstatestates_by_idtrigger_value_types	old_statetrigger_value_typeunset_valuenew_trigger_val	coalesceds
             r   _coalesce_widget_statesrX   ]   s/    j )3(:(:,(:f		6(:  ,
 	!	!8d!CD  ''	/B+$$W-1CCI'9:kI"."2"29<<"@ ' $..w7;MM  1CDS1:L.+ 0C (0 I\0023G,s   Dc                  6    e Zd ZdZd ZddZd	dZd
dZddZy)ScriptRequestszAn interface for communicating with a ScriptRunner. Thread-safe.

    AppSession makes requests of a ScriptRunner through this class, and
    ScriptRunner handles those requests.
    c                ~    t        j                         | _        t        j                  | _        t               | _        y r*   )	threadingLock_lockr   r   _stater   r9   r-   s    r   __init__zScriptRequests.__init__   s'    ^^%
'00$;r   c                p    | j                   5  t        j                  | _        ddd       y# 1 sw Y   yxY w)zRequest that the ScriptRunner stop running. A stopped ScriptRunner
        can't be used anymore. STOP requests succeed unconditionally.
        N)r^   r   r   r_   r-   s    r   request_stopzScriptRequests.request_stop   s"     ZZ+00DK ZZs   ,5c           
        | j                   5  | j                  t        j                  k(  r
	 ddd       y| j                  t        j                  k(  rKt        j
                  | _        |j                  rt        |d|j                  g      }|| _        	 ddd       y| j                  t        j
                  k(  rt        | j                  j                  |j                        }|j                  rBg | j                  j                  }|j                  |vr7|j                  |j                         n|j                  r|j                  }ng }t        |j                  ||j                  |j                   ||j"                  |j$                        | _        	 ddd       yt'        d| j                         # 1 sw Y   yxY w)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(   z Unrecognized ScriptRunnerState: )r^   r_   r   r   r   r   r"   r   r9   rX   r   r%   appendr   r   r    r!   r'   r(   r;   )r.   new_datacoalesced_statesr%   s       r   request_rerunzScriptRequests.request_rerun   s    ZZ{{/444 Z
 {{/888 055 ''& $(+3+?+?*@ H $, ) Z, {{/555 $;$$22H4J4J$  '' )N$*:*:*L*L(M%++3DD)001E1EF//(0(B(B%
 )+%#,!)!6!6"2%-%>%>&00&7-5-N-N"*"8"8$  s Zx !A$++OPPy Zs   F8AF8C:F8 F88Gc                   | j                   t        j                  k(  sQ| j                   t        j                  k(  r5t	        | j
                  j                  | j
                  j                        ry| j                  5  | j                   t        j                  k(  rt	        | j
                  j                  | j
                  j                        r
	 ddd       yt        j                  | _         t        t        j                  | j
                        cddd       S | j                   t        j                  k(  sJ t        t        j                        cddd       S # 1 sw Y   yxY w)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)r_   r   r   r   r@   r9   r%   r'   r^   r7   r   r-   s    r   on_scriptrunner_yieldz$ScriptRequests.on_scriptrunner_yield   s    ;;+444KK,2227  22  99 ZZ{{/555 ;$$66$$==   Z 088$%6%<%<d>N>NO Z ;;"3"8"8888 !2!7!78 ZZs   <AE8E7EE$c                `   | j                   5  | j                  t        j                  k(  rBt        j                  | _        t        t        j                  | j                        cddd       S t        j                  | _        t        t        j                        cddd       S # 1 sw Y   yxY w)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)r^   r_   r   r   r   r7   r9   r   r-   s    r   on_scriptrunner_readyz$ScriptRequests.on_scriptrunner_ready  sq     ZZ{{/555/88$%6%<%<d>N>NO Z ,00DK !2!7!78 ZZs   AB$--B$$B-N)r2   None)re   r   r2   r&   )r2   zScriptRequest | None)r2   r7   )	r   r   r   r3   r`   rb   rg   ri   rk   r   r   r   rZ   rZ      s$    '
1FQP&9P9r   rZ   )r%   r$   r'   r&   r2   r&   )rN   r   rO   r   r2   r   )
__future__r   r\   dataclassesr   r   r   enumr   typingr   	streamlitr	   streamlit.proto.Common_pb2r
   rH    streamlit.proto.WidgetStates_pb2r   r   r   r   r7   r@   rX   rZ   r   r   r   <module>rt      s    #  1 1    T F  $     ( $      D D"D 
D6#61D66rU9 U9r   