
    chn                    N   % S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  S SK	J
r
  S SKJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJrJr  S SKJr  S SKJr  S SKJr  S SK J!r!  S SK"J#r#  S SK$J%r%J&r&J'r'J(r(J)r)  S SK*J+r+J,r,  S SK-J.r.  S SK/J0r0  \(       a$  S SK1J2r2  S SK3J4r4  S SK5J6r6  S SK7J8r8  S SK9J:r:  S SK;J<r<  Sr=S\>S'   \R~                  S:X  a  SOSr@S\>S'   \" \A5      rBS\>S '    " S! S"\C5      rD\" S#S$9 " S% S&5      5       rE " S' S(\
5      rF " S) S*\5      rG " S+ S,5      rHg)-    )annotationsN)	dataclassfield)Enum)TYPE_CHECKINGFinal
NamedTuple)LocalComponentRegistry)
get_logger)
ForwardMsg)
AppSession)get_data_cache_stats_provider!get_resource_cache_stats_provider)LocalDiskCacheStorageManager)MediaFileManager)MemorySessionStorage)
ScriptData)ScriptCache)ActiveSessionInfoSessionClientSessionClientDisconnectedErrorSessionManagerSessionStorage)SCRIPT_RUN_WITHOUT_ERRORS_KEYSessionStateStatProvider)StatsManager)WebsocketSessionManager)	Awaitable)BaseComponentRegistry)BackMsg)CacheStorageManager)MediaFileStorage)UploadedFileManager<   r   SCRIPT_RUN_CHECK_TIMEOUTwin32g      ?_SIGNAL_CHECK_INTERVAL_LOGGERc                      \ rS rSrSrSrg)RuntimeStoppedErrorI   z;Raised by operations on a Runtime instance that is stopped. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r,       lC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\streamlit/runtime/runtime.pyr*   r*   I   s    Er3   r*   T)frozenc                      \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   \" \S9rS\S'   \" \	S9r
S\S'   \rS\S'   \" \S9rS\S'   SrS\S'   Srg)RuntimeConfigM   z$Config options for StreamlitRuntime.strscript_path
str | Nonecommand_liner"   media_file_storager#   uploaded_file_manager)default_factoryr!   cache_storage_managerr   component_registryztype[SessionManager]session_manager_classr   session_storageFboolis_hellor,   N)r-   r.   r/   r0   r1   __annotations__r   r   r@   r
   rA   r   rB   r   rC   rE   r2   r,   r3   r4   r7   r7   M   s    .   )( /. 2742. 
 16.1- 
 3J/I ',<P&QO^Q Hdr3   r7   c                  (    \ rS rSrSrSrSrSrSrSr	g)	RuntimeStatex   INITIALNO_SESSIONS_CONNECTEDONE_OR_MORE_SESSIONS_CONNECTEDSTOPPINGSTOPPEDr,   N)
r-   r.   r/   r0   rJ   rK   rL   rM   rN   r2   r,   r3   r4   rH   rH   x   s    G3%E"HGr3   rH   c                  V    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   S	\S'   Srg)AsyncObjects   zContainer for all asyncio objects that Runtime manages.
These cannot be initialized until the Runtime's eventloop is assigned.
zasyncio.AbstractEventLoop	eventloopzasyncio.Event	must_stophas_connectionneed_send_datazasyncio.Future[None]startedstoppedr,   N)r-   r.   r/   r0   r1   rF   r2   r,   r3   r4   rP   rP      s7    
 )(  "! "! "! "!r3   rP   c                     \ rS rSr% SrS\S'   \S#S j5       r\S$S j5       rS%S jr	\
S&S j5       r\
S'S	 j5       r\
S(S
 j5       r\
S)S j5       r\
S*S j5       r\
S+S j5       r\
S,S j5       rS-S jrS.S jrS/S jrS/S jrS0S jr  S1         S2S jjr  S1         S2S jjrS.S jrS.S jrS3S jr      S4S jr\
S5S j5       rS5S jrS6S jrS/S jr S7S jr!S/S jr"S8S  jr#S/S! jr$S"r%g)9Runtime   NzRuntime | None	_instancec                J    U R                   c  [        S5      eU R                   $ )z^Return the singleton Runtime instance. Raise an Error if the
Runtime hasn't been created yet.
zRuntime hasn't been created!)r[   RuntimeErrorclss    r4   instanceRuntime.instance   s$    
 == =>>}}r3   c                    U R                   SL$ )a
  True if the singleton Runtime instance has been created.

When a Streamlit app is running in "raw mode" - that is, when the
app is run via `python app.py` instead of `streamlit run app.py` -
the Runtime will not exist, and various Streamlit functions need
to adapt.
N)r[   r^   s    r4   existsRuntime.exists   s     }}D((r3   c                ^   [         R                  b  [        S5      eU [         l        SU l        SU l        UR
                  U l        UR                  U l        [        R                  U l        UR                  U l        UR                  U l        [!        UR"                  S9U l        UR&                  U l        [+        5       U l        UR/                  UR0                  U R                  U R,                  U R2                  S9U l        [7        5       U l        U R8                  R;                  [=        5       5        U R8                  R;                  [?        5       5        U R8                  R;                  U R                  5        U R8                  R;                  [A        U R4                  5      5        g)zCreate a Runtime instance. It won't be started yet.

Runtime is *not* thread-safe. Its public methods are generally
safe to call only on the same thread that its event loop runs on.

Parameters
----------
config
    Config options.
Nz Runtime instance already exists!)storage)rC   r>   script_cachemessage_enqueued_callback)!rY   r[   r]   _async_objs_loop_coroutine_taskr:   _main_script_pathrE   	_is_hellorH   rJ   _staterA   _component_registryr>   _uploaded_file_mgrr   r=   _media_file_mgrr@   _cache_storage_managerr   _script_cacherB   rC   _enqueued_some_message_session_mgrr   
_stats_mgrregister_providerr   r   r   )selfconfigs     r4   __init__Runtime.__init__   sR    (ABB  15 @D!!'!3!3"** $*#<#< "(">">/8Q8QR&,&B&B#(]"88"22"&"9"9++&*&A&A	 9 
 '.))*G*IJ))*K*MN))$*A*AB))*B4CTCT*UVr3   c                    U R                   $ N)rm   rw   s    r4   stateRuntime.state   s    {{r3   c                    U R                   $ r|   )rn   r}   s    r4   rA   Runtime.component_registry   s    '''r3   c                    U R                   $ r|   )ro   r}   s    r4   uploaded_file_mgrRuntime.uploaded_file_mgr   s    &&&r3   c                    U R                   $ r|   )rq   r}   s    r4   r@   Runtime.cache_storage_manager   s    ***r3   c                    U R                   $ r|   )rp   r}   s    r4   media_file_mgrRuntime.media_file_mgr   s    ###r3   c                    U R                   $ r|   )ru   r}   s    r4   	stats_mgrRuntime.stats_mgr   s    r3   c                6    U R                  5       R                  $ )z?A Future that completes when the Runtime's run loop has exited.)_get_async_objsrW   r}   s    r4   rW   Runtime.stopped   s     ##%---r3   c                X    U R                   R                  U5      nUc  gUR                  $ )zGet the SessionClient for the given session_id, or None
if no such session exists.

Notes
-----
Threading: SAFE. May be called on any thread.
N)rt   get_active_session_infoclientrw   
session_idsession_infos      r4   
get_clientRuntime.get_client  s/     ((@@L"""r3   c                v    U R                   R                  U5      nUb  UR                  R                  5         gg)ziClear the user_info for the given session_id.

Notes
-----
Threading: SAFE. May be called on any thread.
N)rt   get_session_infosessionclear_user_infor   s      r4   clear_user_info_for_session#Runtime.clear_user_info_for_session  s7     ((99*E#  002 $r3   c           	       #    [        [        R                  " 5       [        R                  " 5       [        R                  " 5       [        R                  " 5       [        R                  " 5       [        R                  " 5       S9nXl        [        R                  " U R                  5       SS9U l        UR                  I Sh  vN   g N7f)zStart the runtime. This must be called only once, before
any other functions are called.

When this coroutine returns, Streamlit is ready to accept new sessions.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
)rR   rS   rT   rU   rV   rW   zRuntime.loop_coroutine)nameN)
rP   asyncioget_running_loopEventFutureri   create_task_loop_coroutinerj   rV   rw   
async_objss     r4   startRuntime.start  s      "..0mmo"==?"==?NN$NN$

 &$+$7$7  ")A%
!     s   B>C CCc                p   ^ ^ T R                  5       mSUU 4S jjnTR                  R                  U5        g)zRequest that Streamlit close all sessions and stop running.
Note that Streamlit won't stop running immediately.

Notes
-----
Threading: SAFE. May be called from any thread.
c                    > TR                   [        R                  [        R                  4;   a  g [        R                  S5        TR                  [        R                  5        T R                  R                  5         g )NzRuntime stopping...)	rm   rH   rM   rN   r(   debug
_set_staterS   set)r   rw   s   r4   stop_on_eventloop'Runtime.stop.<locals>.stop_on_eventloopA  sT    {{|44l6J6JKKMM/0OOL112  $$&r3   NreturnNone)r   rR   call_soon_threadsafe)rw   r   r   s   ` @r4   stopRuntime.stop6  s4     ))+
	' 	' 	112CDr3   c                8    U R                   R                  U5      $ )zpTrue if the session_id belongs to an active session.

Notes
-----
Threading: SAFE. May be called on any thread.
)rt   is_active_session)rw   r   s     r4   r   Runtime.is_active_sessionK  s       22:>>r3   c                   U(       a  U(       a  [        S5      eU R                  [        R                  [        R                  4;   a  [        SU R                   S35      eU R                  R                  U[        U R                  U R                  5      UUUS9nU R                  [        R                  5        U R                  5       R                  R                  5         U$ )a  Create a new session (or connect to an existing one) and return its unique ID.

Parameters
----------
client
    A concrete SessionClient implementation for communicating with
    the session's client.
user_info
    A dict that contains information about the session's user. For now,
    it only (optionally) contains the user's email address.

    {
        "email": "example@example.com"
    }
existing_session_id
    The ID of an existing session to reconnect to. If one is not provided, a new
    session is created. Note that whether the Runtime's SessionManager supports
    reconnecting to an existing session depends on the SessionManager that this
    runtime is configured with.
session_id_override
    The ID to assign to a new session being created with this method. Setting
    this can be useful when the service that a Streamlit Runtime is running in
    wants to tie the lifecycle of a Streamlit session to some other session-like
    object that it manages. Only one of existing_session_id and
    session_id_override should be set.

Returns
-------
str
    The session's unique string ID.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
z`Only one of existing_session_id and session_id_override should be set. This should never happen.zCan't connect_session (state=))r   script_data	user_infoexisting_session_idsession_id_override)r]   rm   rH   rM   rN   r*   rt   connect_sessionr   rk   rl   r   rL   r   rT   r   )rw   r   r   r   r   r   s         r4   r   Runtime.connect_sessionT  s    T #6, 
 ;;<00,2F2FGG%(Edkk]RS&TUU&&66"4#9#94>>J 3 3 7 

 	CCD--113r3   c                P    [         R                  S5        U R                  UUUUS9$ )zCreate a new session (or connect to an existing one) and return its unique ID.

Notes
-----
This method is simply an alias for connect_session added for backwards
compatibility.
z:create_session is deprecated! Use connect_session instead.)r   r   r   r   )r(   warningr   )rw   r   r   r   r   s        r4   create_sessionRuntime.create_session  s5     	TU## 3 3	 $ 
 	
r3   c                    U R                   R                  U5      nU(       a  U R                   R                  U5        U R                  5         g)a  Close and completely shut down a session.

This differs from disconnect_session in that it always completely shuts down a
session, permanently losing any associated state (session state, uploaded files,
etc.).

This function may be called multiple times for the same session,
which is not an error. (Subsequent calls just no-op.)

Parameters
----------
session_id
    The session's unique ID.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
N)rt   r   close_session_on_session_disconnectedr   s      r4   r   Runtime.close_session  s=    & ((99*E++J7%%'r3   c                    U R                   R                  U5      nU(       a  U R                   R                  U5        U R                  5         g)a  Disconnect a session. It will stop producing ForwardMsgs.

Differs from close_session because disconnected sessions can be reconnected to
for a brief window (depending on the SessionManager/SessionStorage
implementations used by the runtime).

This function may be called multiple times for the same session,
which is not an error. (Subsequent calls just no-op.)

Parameters
----------
session_id
    The session's unique ID.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
N)rt   r   disconnect_sessionr   r   s      r4   r   Runtime.disconnect_session  s=    & ((@@L00<%%'r3   c                2   U R                   [        R                  [        R                  4;   a  [	        SU R                    S35      eU R
                  R                  U5      nUc  [        R                  SU5        gUR                  R                  U5        g)zSend a BackMsg to an active session.

Parameters
----------
session_id
    The session's unique ID.
msg
    The BackMsg to deliver to the session.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
zCan't handle_backmsg (state=r   Nz3Discarding BackMsg for disconnected session (id=%s))rm   rH   rM   rN   r*   rt   r   r(   r   r   handle_backmsg)rw   r   msgr   s       r4   r   Runtime.handle_backmsg  s     ;;<00,2F2FGG%(DT[[MQR&STT((@@LMMEz ++C0r3   c                2   U R                   [        R                  [        R                  4;   a  [	        SU R                    S35      eU R
                  R                  U5      nUc  [        R                  SU5        gUR                  R                  U5        g)zHandle an Exception raised during deserialization of a BackMsg.

Parameters
----------
session_id
    The session's unique ID.
exc
    The Exception.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
z6Can't handle_backmsg_deserialization_exception (state=r   Nz=Discarding BackMsg Exception for disconnected session (id=%s))rm   rH   rM   rN   r*   rt   r   r(   r   r   handle_backmsg_exception)rw   r   excr   s       r4   (handle_backmsg_deserialization_exception0Runtime.handle_backmsg_deserialization_exception  s      ;;<00,2F2FGG%HUVW  ((@@LMMO 55c:r3   c                   #    U R                   [        R                  [        R                  [        R                  4;  a  gg7f)N)Tok)Funavailable)rm   rH   rJ   rM   rN   r}   s    r4   is_ready_for_browser_connection'Runtime.is_ready_for_browser_connection  s9     ;;  !!  
 

 #s   A Ac                  #    [        [        U R                  U R                  5      U R                  U R
                  U R                  SS0S9n UR                  S5        [        R                  " 5       n[        UR                  ;  at  [        R                  " 5       U-
  [        :  aT  [        R                  " S5      I Sh  vN   [        UR                  ;  a"  [        R                  " 5       U-
  [        :  a  MT  [        UR                  ;  a   UR                  5         gUR                  [           nU(       a  SOSnX44UR                  5         $  N! UR                  5         f = f7f)	a
  Load and execute the app's script to verify it runs without an error.

Returns
-------
(True, "ok") if the script completes without error, or (False, err_msg)
if the script raises an exception.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
emailztest@example.com)r   r>   rg   rh   r   Ng?)Ftimeoutr   error)r   r   rk   rl   ro   rr   rs   request_reruntimeperf_counterr   session_stater%   r   sleepshutdown)rw   r   nowr   r   s        r4   does_script_run_without_error%Runtime.does_script_run_without_error  s*     "4#9#94>>J"&"9"9++&*&A&A 23
	!!$'##%C-W5J5JJ&&(3.2JJmmC((( .W5J5JJ&&(3.2JJ -G4I4II'  &&'DEB$'C7 ) sC   AE+A3E E8E =E E+# E E+E E((E+c                R    [         R                  SU R                  U5        Xl        g )NzRuntime state: %s -> %s)r(   r   rm   )rw   	new_states     r4   r   Runtime._set_stateH  s    /iHr3   c                  #    U R                  5       n U R                  [        R                  :X  a   U R	                  [        R
                  5        O7U R                  [        R                  :X  a  O[        SU R                   35      eUR                  R                  S5        UR                  R                  5       (       Gdx  U R                  [        R
                  :X  a  [        R                  " [        R                  " UR                  R                  5       5      [        R                  " UR                  R                  5       5      4[        R                   ["        S9I Sh  vN u  p#U H  nUR%                  5         M     U(       d	  ["        b  M  OU R                  [        R                  :X  a  UR&                  R)                  5         U R*                  R-                  5        HV  nUR.                  R1                  5       nU H3  n U R3                  XW5        [        R:                  " S5      I Sh  vN   M5     MX     [        R:                  " S5      I Sh  vN   OO[        R                  " [        R                  " UR                  R                  5       5      [        R                  " UR&                  R                  5       5      4[        R                   ["        S9I Sh  vN u  p#U H  nUR%                  5         M     U(       d
  ["        b  GMv  UR                  R                  5       (       d  GMx  U R*                  R=                  5        H2  nU R*                  R?                  UR.                  R8                  5        M4     U R	                  [        R@                  5        URB                  R                  S5        g GN`! [4         a3    U R*                  R7                  UR.                  R8                  5         GNf = f GN GN GN'! [D         aO  n	URB                  RG                  U	5        [H        RJ                  " 5         [L        RO                  S5         Sn	A	gSn	A	ff = f7f)zThe main Runtime loop.

This function won't exit until `stop` is called.

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
zBad Runtime state at start: N)return_whenr   r   g{Gz?zJ
Please report this bug at https://github.com/streamlit/streamlit/issues.
)(r   rm   rH   rJ   r   rK   rL   r]   rV   
set_resultrS   is_setr   waitr   rT   FIRST_COMPLETEDr'   cancelrU   clearrt   list_active_sessionsr   flush_browser_queue_send_messager   r   idr   list_sessionsr   rN   rW   	Exceptionset_exception	traceback	print_excr(   info)
rw   r   
done_taskspending_taskstaskactive_session_infomsg_listr   r   es
             r4   r   Runtime._loop_coroutineL  sg     ))+
^	{{l222 B BC K KK"%A$++#OPP ))$/ **1133;;,"D"DD
 7>ll#//
0D0D0I0I0KL#//
0I0I0N0N0PQ %,$;$; 67 1-J !. !. &*@*L [[L$O$OO--335/3/@/@/U/U/W+#6#>#>#R#R#T#+C" $ 2 23F L #*--"222 $, 0X "-----  3:,,++J,@,@,E,E,GH++J,E,E,J,J,LM !( 7 723 -)
 *DKKM * "&<&HA !**1133F !% 1 1 ? ? A !!//0D0D0G0GH	 !B OOL001))$/I10 $B " $ 1 1 D D$7$?$?$B$B!"" 3 .-<  	,,Q/!LL 	s   Q/EP 'O(B$P O
P 7P
8%P PBP )P*AP <B
P Q/P 
9PP PP P P 
Q,AQ'"Q/'Q,,Q/c                    UR                  S5      S:X  a3  UR                  [        R                  :X  a  U=R                  S-  sl        UR
                  R                  U5        g)a  Send a message to a client.
If the client is likely to have already cached the message, we may
instead send a "reference" message that contains only the hash of the
message.

Parameters
----------
session_info : ActiveSessionInfo
    The ActiveSessionInfo associated with websocket
msg : ForwardMsg
    The message to send to the client

Notes
-----
Threading: UNSAFE. Must be called on the eventloop thread.
typescript_finished   N)
WhichOneofr  r   FINISHED_SUCCESSFULLYscript_run_countr   write_forward_msg)rw   r   r   s      r4   r   Runtime._send_message  sQ    ( >>&!%66:#C#CC))Q.) 	--c2r3   c                    U R                  5       nUR                  R                  UR                  R                  5        g)zCallback called by AppSession after the AppSession has enqueued a
message. Sets the "needs_send_data" event, which causes our core
loop to wake up and flush client message queues.

Notes
-----
Threading: SAFE. May be called on any thread.
N)r   rR   r   rU   r   r   s     r4   rs   Runtime._enqueued_some_message  s2     ))+
11*2K2K2O2OPr3   c                J    U R                   c  [        S5      eU R                   $ )z`Return our AsyncObjects instance. If the Runtime hasn't been
started, this will raise an error.
zRuntime hasn't started yet!)ri   r]   r}   s    r4   r   Runtime._get_async_objs  s(     #<==r3   c                   U R                   [        R                  :X  ag  U R                  R	                  5       S:X  aH  U R                  5       R                  R                  5         U R                  [        R                  5        ggg)z\Set the runtime state to NO_SESSIONS_CONNECTED if the last active
session was disconnected.
r   N)
rm   rH   rL   rt   num_active_sessionsr   rT   r   r   rK   r}   s    r4   r    Runtime._on_session_disconnected  se    
 KK<FFF!!5571<  "11779OOL>>? = Gr3   )ri   rq   rn   rl   rj   rk   rp   rr   rt   rm   ru   ro   )r   rY   )r   rD   )rx   r7   r   r   )r   rH   )r   r   )r   r#   )r   r!   )r   r   )r   r   )r   zAwaitable[None])r   r9   r   zSessionClient | None)r   r9   r   r   r   )r   r9   r   rD   )NN)
r   r   r   zdict[str, str | bool | None]r   r;   r   r;   r   r9   )r   r9   r   r    r   r   )r   r9   r   BaseExceptionr   r   )r   ztuple[bool, str])r   rH   r   r   )r   r   r   r   r   r   )r   rP   )&r-   r.   r/   r0   r[   rF   classmethodr`   rc   ry   propertyr~   rA   r   r@   r   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rs   r   r   r2   r,   r3   r4   rY   rY      s    $I~$  ) )-W^   ( ( ' ' + + $ $   . .#	3!:E*? +/*.== 0= (	=
 (= 
=F +/*.

 0
 (	

 (
 

,(0(014;;$1;	;> $ $)V jX38
Q 	@r3   rY   )I
__future__r   r   sysr   r   dataclassesr   r   enumr   typingr   r   r	   1streamlit.components.lib.local_component_registryr
   streamlit.loggerr   streamlit.proto.ForwardMsg_pb2r   streamlit.runtime.app_sessionr   streamlit.runtime.cachingr   r   :streamlit.runtime.caching.storage.local_disk_cache_storager   $streamlit.runtime.media_file_managerr   (streamlit.runtime.memory_session_storager   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   !streamlit.runtime.session_managerr   r   r   r   r   streamlit.runtime.stater   r   streamlit.runtime.statsr   +streamlit.runtime.websocket_session_managerr   collections.abcr   2streamlit.components.types.base_component_registryr   streamlit.proto.BackMsg_pb2r    !streamlit.runtime.caching.storager!   $streamlit.runtime.media_file_storager"   'streamlit.runtime.uploaded_file_managerr#   r%   rF   platformr'   r-   r(   r   r*   r7   rH   rP   rY   r,   r3   r4   <module>r1     s    #  
   (  3 3 T ' 5 4 B I 4 C  1 O)X3EEK #% % $ (+||w'>D  HH% %F) F $! ! !T4 ": "0Y	@ Y	@r3   