Multi-page app with session state

When you use an IDE debugger with this code, it can’t get a Session ID from the IDE itself, so you get this message:
" session_id = get_report_ctx().session_id
AttributeError: ‘NoneType’ object has no attribute ‘session_id’

I understand why we get this error, but what is the solution? How do we debug our code using the debugger when our local IDE can’t generate a sesion_id?

Hello @euler, if you have some data that must be cleared every run, the best way to do it is to keep them out of the session state I guess. Could you describe more precisely your usecase?

Hello @ksxx, unfortunately it’s not possible to change widget values programmatically for now. This session state doesn’t support it, and I don’t know any that does.

Hello @an_pas, I don’t know that object.

If you’re still encountering this issue,you could maybe use @thiago’s SessionState which does not use any hashing mechanism. My implementation is mostly useful if you want to bind widget values to session state variables.

Hello @OlliePage, your IDE tries to run your app as a regular python script, but in that case it won’t load a ReportContext object.

In your case, try to use this _get_state() function. When run as a script, using a module like sys to keep your session state should work fine.

import sys

# ...

def _get_state(hash_funcs=None):
        session = _get_session()
    except (AttributeError, RuntimeError):
        session = sys

    if not hasattr(session, "_custom_session_state"):
        session._custom_session_state = _SessionState(session, hash_funcs)

    return session._custom_session_state

That did the trick. Thanks very much. That’s made the world of difference.

1 Like

I tried to set the page config but an error came out stated that I can only call it once. I suspect it’s probably due to how multiple app structure calling it. Is there any hack around this? @okld

StreamlitAPIException : set_page_config() can only be called once per app, and must be called as the first Streamlit command in your script.