o
    hBu                     @  s  U d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	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 d dlmZmZ d dlm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* d dl+m,Z,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 erd dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZC dZDdeEd< eeFZGdeEd < G d!d" d"eHZIed#d$G d%d& d&ZJG d'd( d(e	ZKG d)d* d*eZLG d+d, d,ZMdS )-    )annotationsN)	dataclassfield)Enum)TYPE_CHECKINGFinal
NamedTuple)config)LocalComponentRegistry)
get_logger)
ForwardMsg)
AppSession)get_data_cache_stats_provider!get_resource_cache_stats_provider)LocalDiskCacheStorageManager)ForwardMsgCachecreate_reference_msgpopulate_hash_if_needed)MediaFileManager)MemorySessionStorage)is_cacheable_msg)
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_LOGGERc                   @  s   e Zd ZdZdS )RuntimeStoppedErrorz;Raised by operations on a Runtime instance that is stopped.N)__name__
__module____qualname____doc__ r0   r0   M/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/runtime.pyr+   K   s    r+   T)frozenc                   @  s   e Zd ZU dZded< ded< ded< ded	< eed
Zded< eed
Z	ded< e
Zded< eed
Zded< dZded< dS )RuntimeConfigz$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_helloN)r,   r-   r.   r/   __annotations__r   r   r;   r
   r<   r!   r=   r   r>   r@   r0   r0   r0   r1   r3   O   s   
 r3   c                   @  s    e Zd ZdZdZdZdZdZdS )RuntimeStateINITIALNO_SESSIONS_CONNECTEDONE_OR_MORE_SESSIONS_CONNECTEDSTOPPINGSTOPPEDN)r,   r-   r.   rC   rD   rE   rF   rG   r0   r0   r0   r1   rB   z   s    rB   c                   @  sB   e Zd ZU dZded< ded< ded< ded< ded	< ded
< dS )AsyncObjectszContainer 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stoppedN)r,   r-   r.   r/   rA   r0   r0   r0   r1   rH      s   
 rH   c                   @  s~  e Zd ZU dZded< edaddZedbdd	ZdcddZe	ddddZ
e	deddZe	dfddZe	dgddZe	dhddZe	diddZe	djd!d"Ze	dkd$d%Zdld)d*Zdmd,d-Zdnd.d/Zdnd0d1Zdod2d3Z		dpdqd;d<Z		dpdqd=d>Zdmd?d@ZdmdAdBZdrdEdFZdsdIdJZe	dtdLdMZdtdNdOZdudQdRZdndSdTZ dvdXdYZ!dndZd[Z"dwd]d^Z#dnd_d`Z$dS )xRuntimeNzRuntime | None	_instancereturnc                 C     | j du r	td| j S )znReturn the singleton Runtime instance. Raise an Error if the
        Runtime hasn't been created yet.
        NzRuntime hasn't been created!)rP   RuntimeErrorclsr0   r0   r1   instance   s   
zRuntime.instancer?   c                 C  s
   | j duS )a2  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)rP   rT   r0   r0   r1   exists   s   
	zRuntime.existsr	   r3   c                 C  s   t jdur	td| t _d| _d| _|j| _|j| _t	j
| _|j| _t | _|j| _t|jd| _|j| _t | _|j|j| j| j| jd| _t | _| jt   | jt!  | j| j | j| j | jt"| j dS )a   Create 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)r>   r9   script_cachemessage_enqueued_callback)#rO   rP   rS   _async_objs_loop_coroutine_taskr5   _main_script_pathr@   	_is_hellorB   rC   _stater<   _component_registryr   _message_cacher9   _uploaded_file_mgrr   r8   _media_file_mgrr;   _cache_storage_managerr   _script_cacher=   r>   _enqueued_some_message_session_mgrr    
_stats_mgrregister_providerr   r   r   )selfr	   r0   r0   r1   __init__   s4   
zRuntime.__init__rB   c                 C     | j S N)r_   rj   r0   r0   r1   state      zRuntime.stater#   c                 C  rl   rm   )r`   rn   r0   r0   r1   r<      rp   zRuntime.component_registryr   c                 C  rl   rm   )ra   rn   r0   r0   r1   message_cache   rp   zRuntime.message_cacher'   c                 C  rl   rm   )rb   rn   r0   r0   r1   uploaded_file_mgr   rp   zRuntime.uploaded_file_mgrr%   c                 C  rl   rm   )rd   rn   r0   r0   r1   r;      rp   zRuntime.cache_storage_managerr   c                 C  rl   rm   )rc   rn   r0   r0   r1   media_file_mgr   rp   zRuntime.media_file_mgrr    c                 C  rl   rm   )rh   rn   r0   r0   r1   	stats_mgr   rp   zRuntime.stats_mgrAwaitable[None]c                 C  s
   |   jS )z?A Future that completes when the Runtime's run loop has exited.)_get_async_objsrN   rn   r0   r0   r1   rN      s   
zRuntime.stopped
session_idr4   SessionClient | Nonec                 C  s   | j |}|du rdS |jS )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)rg   get_active_session_infoclientrj   rw   session_infor0   r0   r1   
get_client	  s   zRuntime.get_clientNonec                 C  s&   | j |}|dur|j  dS dS )zClear the user_info for the given session_id.

        Notes
        -----
        Threading: SAFE. May be called on any thread.
        N)rg   get_session_infosessionclear_user_infor{   r0   r0   r1   clear_user_info_for_session  s   z#Runtime.clear_user_info_for_sessionc                   sX   t t t t t t t d}|| _tj|  dd| _|j	I dH  dS )a  Start 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.
        )rI   rJ   rK   rL   rM   rN   zRuntime.loop_coroutine)nameN)
rH   asyncioget_running_loopEventFuturer[   create_task_loop_coroutiner\   rM   rj   
async_objsr0   r0   r1   start!  s   zRuntime.startc                   s&       fdd} j| dS )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                     s:   j tjtjfv rd S td tj  j  d S )NzRuntime stopping...)	r_   rB   rF   rG   r*   debug
_set_staterJ   setr0   r   rj   r0   r1   stop_on_eventloopI  s
   
z'Runtime.stop.<locals>.stop_on_eventloopN)rv   rI   call_soon_threadsafe)rj   r   r0   r   r1   stop>  s   	zRuntime.stopc                 C  s   | j |S )zTrue if the session_id belongs to an active session.

        Notes
        -----
        Threading: SAFE. May be called on any thread.
        )rg   is_active_session)rj   rw   r0   r0   r1   r   S  s   zRuntime.is_active_sessionrz   r   	user_infodict[str, str | bool | None]existing_session_idr6   session_id_overridec                 C  sr   |r|rJ d| j tjtjfv rtd| j  d| jj|t| j| j	|||d}| 
tj |  j  |S )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.
        zFOnly one of existing_session_id and session_id_override should be set!zCan't connect_session (state=))rz   script_datar   r   r   )r_   rB   rF   rG   r+   rg   connect_sessionr   r]   r^   r   rE   rv   rK   r   )rj   rz   r   r   r   rw   r0   r0   r1   r   \  s   
*zRuntime.connect_sessionc                 C  s   t d | j||||dS )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.)rz   r   r   r   )r*   warningr   )rj   rz   r   r   r   r0   r0   r1   create_session  s   
zRuntime.create_sessionc                 C  6   | j |}|r| j|j | j | |   dS )a9  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)rg   r   ra   remove_refs_for_sessionr   close_session_on_session_disconnectedr{   r0   r0   r1   r     s
   zRuntime.close_sessionc                 C  r   )aZ  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)rg   ry   ra   r   r   disconnect_sessionr   r{   r0   r0   r1   r     s
   zRuntime.disconnect_sessionmsgr$   c                 C  X   | j tjtjfv rtd| j  d| j|}|du r$td| dS |j	
| dS )a+  Send 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))r_   rB   rF   rG   r+   rg   ry   r*   r   r   handle_backmsg)rj   rw   r   r|   r0   r0   r1   r     s   zRuntime.handle_backmsgexcBaseExceptionc                 C  r   )a.  Handle 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))r_   rB   rF   rG   r+   rg   ry   r*   r   r   handle_backmsg_exception)rj   rw   r   r|   r0   r0   r1   (handle_backmsg_deserialization_exception  s   z0Runtime.handle_backmsg_deserialization_exceptiontuple[bool, str]c                   s    | j tjtjtjfvrdS dS )N)Tok)Funavailable)r_   rB   rC   rF   rG   rn   r0   r0   r1   is_ready_for_browser_connection!  s   z'Runtime.is_ready_for_browser_connectionc                   s   t t| j| j| j| j| jddid}zK|d t	 }t
|jvr@t	 | tk r@tdI dH  t
|jvr@t	 | tk s+t
|jvrLW |  dS |jt
 }|rUdnd}||fW |  S |  w )	aJ  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   r9   rY   rZ   r   Ng?)Ftimeoutr   error)r   r   r]   r^   rb   re   rf   request_reruntimeperf_counterr   session_stater)   r   sleepshutdown)rj   r   nowr   r   r0   r0   r1   does_script_run_without_error,  s.   




z%Runtime.does_script_run_without_error	new_statec                 C  s   t d| j| || _d S )NzRuntime state: %s -> %s)r*   r   r_   )rj   r   r0   r0   r1   r   W  s   
zRuntime._set_statec           
   
     s  |   }z| jtjkr| tj n| jtjkrntd| j |j	d |j
 s| jtjkrXtjt|j
 t|j ftjdI dH \}}|D ]}|  qPnI| jtjkr|j  | j D ].}|j }|D ]$}z| || W n ty   | j|jj Y nw tdI dH  qqqhtdI dH  nn)tjt|j
 t|j ftjdI dH \}}|D ]}|  q|j
 r-| j D ]
}| j|jj q| tj |j 	d W dS  t!y }	 z|j "|	 t#$  t%&d W Y d}	~	dS d}	~	ww )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   g{Gz?zJ
Please report this bug at https://github.com/streamlit/streamlit/issues.
)'rv   r_   rB   rC   r   rD   rE   rS   rM   
set_resultrJ   is_setr   waitr   rK   FIRST_COMPLETEDcancelrL   clearrg   list_active_sessionsr   flush_browser_queue_send_messager   r   idr   list_sessionsr   rG   rN   	Exceptionset_exception	traceback	print_excr*   info)
rj   r   _pending_taskstaskactive_session_infomsg_listr   r|   er0   r0   r1   r   [  sr   






7zRuntime._loop_coroutiner|   r   r   c                 C  s   t ||j_|}|jjr6t| | j||j|jr%t	d|j
 t|}t	d|j
 | j||j|j |ddkrg|jtjksNtdrg|jtjkrgt	dtd | jd7  _| j|j|j |j| d	S )
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.
        z$Sending cached message ref (hash=%s)zCaching message (hash=%s)typescript_finishedz4global.includeFragmentRunsInForwardMessageCacheCountzYScript run finished successfully; removing expired entries from MessageCache (max_age=%s)zglobal.maxCachedMessageAge   N)r   metadata	cacheabler   ra   has_message_referencer   script_run_countr*   r   hashr   add_message
WhichOneofr   r   FINISHED_SUCCESSFULLYr	   
get_option"FINISHED_FRAGMENT_RUN_SUCCESSFULLY"remove_expired_entries_for_sessionrz   write_forward_msg)rj   r|   r   msg_to_sendr0   r0   r1   r     s:   

zRuntime._send_messagec                 C  s   |   }|j|jj dS )a  Callback 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)rv   rI   r   rL   r   r   r0   r0   r1   rf     s   	zRuntime._enqueued_some_messagerH   c                 C  rR   )zpReturn our AsyncObjects instance. If the Runtime hasn't been
        started, this will raise an error.
        NzRuntime hasn't started yet!)r[   rS   rn   r0   r0   r1   rv     s   
zRuntime._get_async_objsc                 C  s@   | j tjkr| j dkr|  j  | tj	 dS dS dS )zlSet the runtime state to NO_SESSIONS_CONNECTED if the last active
        session was disconnected.
        r   N)
r_   rB   rE   rg   num_active_sessionsrv   rK   r   r   rD   rn   r0   r0   r1   r     s   z Runtime._on_session_disconnected)rQ   rO   )rQ   r?   )r	   r3   )rQ   rB   )rQ   r#   )rQ   r   )rQ   r'   )rQ   r%   )rQ   r   )rQ   r    )rQ   ru   )rw   r4   rQ   rx   )rw   r4   rQ   r~   )rQ   r~   )rw   r4   rQ   r?   )NN)
rz   r   r   r   r   r6   r   r6   rQ   r4   )rw   r4   r   r$   rQ   r~   )rw   r4   r   r   rQ   r~   )rQ   r   )r   rB   rQ   r~   )r|   r   r   r   rQ   r~   )rQ   rH   )%r,   r-   r.   rP   rA   classmethodrV   rW   rk   propertyro   r<   rq   rr   r;   rs   rt   rN   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rf   rv   r   r0   r0   r0   r1   rO      s^   
 

1





A


 



+

`
@
rO   )N
__future__r   r   r   r   dataclassesr   r   enumr   typingr   r   r   	streamlitr	   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.forward_msg_cacher   r   r   $streamlit.runtime.media_file_managerr   (streamlit.runtime.memory_session_storager   streamlit.runtime.runtime_utilr   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)   rA   r,   r*   r   r+   r3   rB   rH   rO   r0   r0   r0   r1   <module>   sN   *