RuntimeError: Server has not been initialized yet

Hello,
I am using a dashboard that implements a login process with the session, It works on MacOS but I get this error when running it on windows.

Traceback (most recent call last):
File “dashboard.py”, line 194, in
session_state_username = get(username=‘’)
File “C:\Users\Kader\Desktop\py\predictions_dashboard\SessionState.py”, line 81, in get
current_server = Server.get_current()
File “C:\Users\Kader\Desktop\py\demand_prediction_server\env\lib\site-packages\streamlit\server\Server.py”, line 177, in get_current
raise RuntimeError(“Server has not been initialized yet”)
RuntimeError: Server has not been initialized yet

What should I do ?

1 Like

Hello!

I have the same issue as k4der2rg, I think it stems from us using SessionState.py:

# -*- coding: utf-8 -*-

"""Hack to add per-session state to Streamlit.
Usage
-----
>>> import SessionState
>>>
>>> session_state = SessionState.get(user_name='', favorite_color='black')
>>> session_state.user_name
''
>>> session_state.user_name = 'Mary'
>>> session_state.favorite_color
'black'
Since you set user_name above, next time your script runs this will be the
result:
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
'Mary'
"""
try:
    import streamlit.report_thread as ReportThread
    from streamlit.server.server import Server
except Exception:
    # Streamlit >= 0.65.0
    import streamlit.report_thread as ReportThread
    from streamlit.server.server import Server


class SessionState(object):
    def __init__(self, **kwargs):
        """A new SessionState object.
        Parameters
        ----------
        **kwargs : any
            Default values for the session state.
        Example
        -------
        >>> session_state = SessionState(user_name='', favorite_color='black')
        >>> session_state.user_name = 'Mary'
        ''
        >>> session_state.favorite_color
        'black'
        """
        for key, val in kwargs.items():
            setattr(self, key, val)


def get(**kwargs):
    """Gets a SessionState object for the current session.
    Creates a new object if necessary.
    Parameters
    ----------
    **kwargs : any
        Default values you want to add to the session state, if we're creating a
        new one.
    Example
    -------
    >>> session_state = get(user_name='', favorite_color='black')
    >>> session_state.user_name
    ''
    >>> session_state.user_name = 'Mary'
    >>> session_state.favorite_color
    'black'
    Since you set user_name above, next time your script runs this will be the
    result:
    >>> session_state = get(user_name='', favorite_color='black')
    >>> session_state.user_name
    'Mary'
    """
    # Hack to get the session object from Streamlit.

    ctx = ReportThread.get_report_ctx()

    this_session = None

    current_server = Server.get_current()
    if hasattr(current_server, '_session_infos'):
        # Streamlit < 0.56
        session_infos = Server.get_current()._session_infos.values()
    else:
        session_infos = Server.get_current()._session_info_by_id.values()

    for session_info in session_infos:
        s = session_info.session
        if (
            # Streamlit < 0.54.0
            (hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)
            or
            # Streamlit >= 0.54.0
            (not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)
            or
            # Streamlit >= 0.65.2
            (not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)
        ):
            this_session = s

    if this_session is None:
        raise RuntimeError(
            "Oh noes. Couldn't get your Streamlit Session object. "
            'Are you doing something fancy with threads?')

    # Got the session object! Now let's attach some state into it.

    if not hasattr(this_session, '_custom_session_state'):
        this_session._custom_session_state = SessionState(**kwargs)

    return this_session._custom_session_state

For background, I am trying to do topic modelling with LDA in my streamllit app. I am computing coherence scores and it seems that, when I call the coherence model, my app runs from the beginning in loops and says RuntimeError: Server has not been initialized yet. Right at the start of the app I have the following code calling SessionState:

ss = SessionState.get(output_df = pd.DataFrame(), 
    df_raw = pd.DataFrame(),
    _model=None,
    text_col='text',
    is_file_uploaded=False,
    id2word = None, 
    corpus= None,
    is_valid_text_feat = False,
    to_clean_data = False,
    to_encode = False,
    to_train = False,
    to_evaluate = False,
    to_visualize = False,
    to_download_report = False,
    df = pd.DataFrame(),
    txt = 'Paste the text to analyze here',
    default_txt = 'Paste the text to analyze here',
    clean_text = None,
    ldamodel = None,
    topics_df = None)

Does someone know what is happening?

Thanks!

Even I’m facing the same issue from SessionState.py

File “C:\Users\abine\Folder\SessionState.py”, line 74, in get
current_server = Server.get_current()

File “C:\ProgramData\Anaconda3\lib\site-packages\streamlit\server\server.py”, line 213, in get_current
raise RuntimeError(“Server has not been initialized yet”)

RuntimeError: Server has not been initialized yet

Any updates here? Having the same issue using Windows and Streamlit 0.78.0

Hi All,
To fix this run: streamlit run app.py
or the name of your python file which is calling the SessionState.py.
In PyCharm Open Terminal at the bottom of your screen and run the above command.