CoolFace
Apppublic

XAI/VisualCorrespondenceHumanStudy

sourceHugging Facemitupdated 4y agoView on Hugging Face
1likes
SessionState.py118 linesDownload Raw Back to root
1"""Hack to add per-session state to Streamlit.2 3Usage4-----5 6>>> import SessionState7>>>8>>> session_state = SessionState.get(user_name='', favorite_color='black')9>>> session_state.user_name10''11>>> session_state.user_name = 'Mary'12>>> session_state.favorite_color13'black'14 15Since you set user_name above, next time your script runs this will be the16result:17>>> session_state = get(user_name='', favorite_color='black')18>>> session_state.user_name19'Mary'20 21"""22try:23    import streamlit.ReportThread as ReportThread24    from streamlit.server.Server import Server25except Exception:26    # Streamlit >= 0.65.027    import streamlit.report_thread as ReportThread28    from streamlit.server.server import Server29 30 31class SessionState(object):32    def __init__(self, **kwargs):33        """A new SessionState object.34 35        Parameters36        ----------37        **kwargs : any38            Default values for the session state.39 40        Example41        -------42        >>> session_state = SessionState(user_name='', favorite_color='black')43        >>> session_state.user_name = 'Mary'44        ''45        >>> session_state.favorite_color46        'black'47 48        """49        for key, val in kwargs.items():50            setattr(self, key, val)51 52 53def get(**kwargs):54    """Gets a SessionState object for the current session.55 56    Creates a new object if necessary.57 58    Parameters59    ----------60    **kwargs : any61        Default values you want to add to the session state, if we're creating a62        new one.63 64    Example65    -------66    >>> session_state = get(user_name='', favorite_color='black')67    >>> session_state.user_name68    ''69    >>> session_state.user_name = 'Mary'70    >>> session_state.favorite_color71    'black'72 73    Since you set user_name above, next time your script runs this will be the74    result:75    >>> session_state = get(user_name='', favorite_color='black')76    >>> session_state.user_name77    'Mary'78 79    """80    # Hack to get the session object from Streamlit.81 82    ctx = ReportThread.get_report_ctx()83 84    this_session = None85 86    current_server = Server.get_current()87    if hasattr(current_server, '_session_infos'):88        # Streamlit < 0.5689        session_infos = Server.get_current()._session_infos.values()90    else:91        session_infos = Server.get_current()._session_info_by_id.values()92 93    for session_info in session_infos:94        s = session_info.session95        if (96            # Streamlit < 0.54.097            (hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)98            or99            # Streamlit >= 0.54.0100            (not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)101            or102            # Streamlit >= 0.65.2103            (not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)104        ):105            this_session = s106 107    if this_session is None:108        raise RuntimeError(109            "Oh noes. Couldn't get your Streamlit Session object. "110            'Are you doing something fancy with threads?')111 112    # Got the session object! Now let's attach some state into it.113 114    if not hasattr(this_session, '_custom_session_state'):115        this_session._custom_session_state = SessionState(**kwargs)116 117    return this_session._custom_session_state118