L i32ddlmZddlmZddlmZddlmZmZm Z er$ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZGd d eZGd deZeGddZeGddZGddeZGddeZGddeZy)) annotations)abstractmethod) dataclass) TYPE_CHECKINGProtocolcast)Callable) ForwardMsg) AppSession) ScriptData) ScriptCache)UploadedFileManagerceZdZdZy)SessionClientDisconnectedErrorz5Raised by operations on a disconnected SessionClient.N__name__ __module__ __qualname____doc__g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/runtime/session_manager.pyrrs?rrc"eZdZdZeddZy) SessionClientz1Interface for sending data to a session's client.ct)zDeliver a ForwardMsg to the client. If the SessionClient has been disconnected, it should raise a SessionClientDisconnectedError. NotImplementedError)selfmsgs rwrite_forward_msgzSessionClient.write_forward_msg&s "!rN)rr returnNone)rrrrrr rrrrr#s;""rrc4eZdZUdZded<ded<dZded<y ) ActiveSessionInfozType containing data related to an active session. This type is nearly identical to SessionInfo. The difference is that when using it, we are guaranteed that SessionClient is not None. rclientr sessionrintscript_run_countN)rrrr__annotations__r(rrrr$r$0s"  crr$cDeZdZUdZded<ded<dZded<d d Zd d Zy ) SessionInfoz.Type containing data related to an AppSession.zSessionClient | Noner%r r&rr'r(c|jduS)N)r%rs r is_activezSessionInfo.is_activeIs{{$&&rcP|js tdtd|S)Nz.A SessionInfo with no client cannot be active!r$)r. RuntimeErrorrr-s r to_activezSessionInfo.to_activeLs(~~OP P '..rN)r!bool)r!r$)rrrrr)r(r.r1rrrr+r+?s(8  c'/rr+ceZdZdZy)SessionStorageErrorzException class for errors raised by SessionStorage. The original error that causes a SessionStorageError to be (re)raised will generally be an I/O error specific to the concrete SessionStorage implementation. Nrrrrr4r4Vsrr4cTeZdZeddZeddZeddZed dZy) SessionStoragect)a.Return the SessionInfo corresponding to session_id, or None if one does not exist. Parameters ---------- session_id The unique ID of the session being fetched. Returns ------- SessionInfo or None Raises ------ SessionStorageError Raised if an error occurs while attempting to fetch the session. This will generally happen if there is an error with the underlying storage backend (e.g. if we lose our connection to it). rr session_ids rgetzSessionStorage.get_ *"!rct)aSave the given session. Parameters ---------- session_info The SessionInfo being saved. Raises ------ SessionStorageError Raised if an error occurs while saving the given session. r)r session_infos rsavezSessionStorage.savev "!rct)aMark the session corresponding to session_id for deletion and stop tracking it. Note that: - Calling delete on an ID corresponding to a nonexistent session is a no-op. - Calling delete on an ID should cause the given session to no longer be tracked by this SessionStorage, but exactly when and how the session's data is eventually cleaned up is a detail left up to the implementation. Parameters ---------- session_id The unique ID of the session to delete. Raises ------ SessionStorageError Raised if an error occurs while attempting to delete the session. rr8s rdeletezSessionStorage.deleter;rct)zList all sessions tracked by this SessionStorage. Returns ------- List[SessionInfo] Raises ------ SessionStorageError Raised if an error occurs while attempting to list sessions. rr-s rlistzSessionStorage.lists "!rNr9strr!zSessionInfo | None)r=r+r!r"r9rEr!r"r!zlist[SessionInfo])rrrrr:r>rArCrrrr6r6^sP"", " """, " "rr6ceZdZdZe ddZe d ddZeddZeddZeddZ ddZ dd Z dd Z dd Z dd Zdd Zy)SessionManagera7SessionManagers are responsible for encapsulating all session lifecycle behavior that the Streamlit Runtime may care about. A SessionManager must define the following required methods: - __init__ - connect_session - close_session - get_session_info - list_sessions SessionManager implementations may also choose to define the notions of active and inactive sessions. The precise definitions of active/inactive are left to the concrete implementation. SessionManagers that wish to differentiate between active and inactive sessions should have the required methods listed above operate on *all* sessions. Additionally, they should define the following methods for working with active sessions: - disconnect_session - get_active_session_info - is_active_session - list_active_sessions When active session-related methods are left undefined, their default implementations are the naturally corresponding required methods. The Runtime, unless there's a good reason to do otherwise, should generally work with the active-session versions of a SessionManager's methods. There isn't currently a need for us to be able to operate on inactive sessions stored in SessionStorage outside of the SessionManager itself. However, it's highly likely that we'll eventually have to do so, which is why the abstractions allow for this now. Notes ----- Threading: All SessionManager methods are *not* threadsafe -- they must be called from the runtime's eventloop thread. ct)aInitialize a SessionManager with the given SessionStorage. Parameters ---------- session_storage The SessionStorage instance backing this SessionManager. uploaded_file_manager Used to manage files uploaded by users via the Streamlit web client. script_cache ScriptCache instance. Caches user script bytecode. message_enqueued_callback A callback invoked after a message is enqueued to be sent to a web client. r)rsession_storageuploaded_file_manager script_cachemessage_enqueued_callbacks r__init__zSessionManager.__init__s 0"!rNct)aCreate a new session or connect to an existing one. Parameters ---------- client A concrete SessionClient implementation for communicating with the session's client. script_data Contains parameters related to running a script. 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 a SessionManager supports reconnecting to an existing session is left up to the concrete SessionManager implementation. Those that do not support reconnection should simply ignore this argument. 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. r)rr% script_data user_infoexisting_session_idsession_id_overrides rconnect_sessionzSessionManager.connect_sessions V"!rct)a*Close and completely delete the session with the given id. 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. rr8s r close_sessionzSessionManager.close_sessions "!rct)zReturn the SessionInfo for the given id, or None if no such session exists. Parameters ---------- session_id The session's unique ID. Returns ------- SessionInfo or None rr8s rget_session_infozSessionManager.get_session_info'r?rct)zReturn the SessionInfo for all sessions managed by this SessionManager. Returns ------- List[SessionInfo] rr-s r list_sessionszSessionManager.list_sessions7s "!rc4t|jS)zReturn the number of sessions tracked by this SessionManager. Subclasses of SessionManager shouldn't provide their own implementation of this method without a *very* good reason. Returns ------- int )lenr[r-s r num_sessionszSessionManager.num_sessionsAs4%%'((rc&|j|y)zDisconnect the given session. This method should be idempotent. Parameters ---------- session_id The session's unique ID. N)rWr8s rdisconnect_sessionz!SessionManager.disconnect_sessionRs :&rcj|j|}||jsy|jS)a/Return the ActiveSessionInfo for the given id, or None if either no such session exists or the session is not active. Parameters ---------- session_id The active session's unique ID. Returns ------- ActiveSessionInfo or None N)rYr.r1)rr9r&s rget_active_session_infoz&SessionManager.get_active_session_info^s6'' 3 ?'"3"3"5  ""rc(|j|duS)z~Return True if the given session exists and is active, False otherwise. Returns ------- bool N)rbr8s ris_active_sessionz SessionManager.is_active_sessionps++J7tCCrcd|jDcgc]}|jc}Scc}w)zReturn the session info for all active sessions tracked by this SessionManager. Returns ------- List[ActiveSessionInfo] )r[r1)rss rlist_active_sessionsz#SessionManager.list_active_sessionsys'(,'9'9';rvs#!00(988GK@Y@ "H "      // /,)L"XL"^_0X_0r