Alternative implementation of session state

Following good feedback on my post in Preserving state across sidebar pages I decided to implement a prototype implementation.

This is basically the same as the proposed SessionState but takes advantage of python typing to let the user define a class/dataclass to define state and therefore take advantage of type checking and code completion in modern editors (tested in Pycharm).

It’s basically a file with the implementation logic (adapted from

from typing import Callable, TypeVar

from streamlit import ReportThread
from streamlit.server.Server import Server

T = TypeVar('T')

# noinspection PyProtectedMember
def get_state(setup_func: Callable[..., T], **kwargs) -> T:
    ctx = ReportThread.get_report_ctx()

    session = None
    session_infos = Server.get_current()._session_infos.values()

    for session_info in session_infos:
        if session_info.session._main_dg == ctx.main_dg:
            session = session_info.session

    if 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 getattr(session, '_custom_session_state', None):
        session._custom_session_state = setup_func(**kwargs)

    return session._custom_session_state

And here is a test program using it to increment a counter when a button is pressed.

import streamlit as st

from session_state import get_state

class MyState:
    a: int
    b: str

    def __init__(self, a: int, b: str):
        self.a = a
        self.b = b

def setup(a: int, b: str) -> MyState:
    print('Running setup')
    return MyState(a, b)

state = get_state(setup, a=3, b='bbb')

st.title('Test state')
if st.button('Increment'):
    state.a = state.a + 1
    print(f'Incremented to {state.a}')

In summary, get_state takes a setup function that will only be executed exactly once (this is an improvement over SessionState as far as I can tell because whatever is in the get was always executed). The setup_function needs to return an object, and whatever object type it returns will also be the return type of get_state. If it’s the first time we are calling get_state then we call the setup function that initializes our state (optionally with keyword arguments). The following times we call it it returns the instance of the state.

Any feedback is appreciated.


Slightly better example using a dataclass and not passing kwargs:

import streamlit as st

from session_state import get_state

class MyState:
    a: int
    b: str

def setup() -> MyState:
    print('Running setup')
    return MyState(a=3, b='bbb')

state = get_state(setup)

st.title('Test state')
if st.button('Increment'):
    state.a = state.a + 1
    print(f'Incremented to {state.a}')

I’ve been using this setup for the past few days with great success. It really makes buttons more useful when state can be stored.


Hi @biellls

I really like it.

Is it possible to do an additional simplification of the api?

I imagine you could actual “hide” the setup function in you file and change the public api from state = get_state(setup) to state=get_state(a=3, b='bbb').


Hi @Marc, thanks for the feedback. What you’re proposing is basically what was implemented in the original gist if I understood correctly. Let me try to explain why I think my way is an improvement over that for two reasons:

  1. Arguments get evaluated every time there’s a re-run, so if you want to open a database connection and you do state=get_state(db=psycopg2.connect("dbname=test user=postgres password=secret")) you’ll be opening it lots of times.
    The reason I added a setup function is that this way we can guarantee that the code inside it runs exactly once, which is what a setup should do.
    Ideally we’d have that in a closure/multi-line lambda but python doesn’t allow those (and regular lambdas are untyped) so I had to make it a function.

  2. You lose typing information. The original get_state is an object that gets added instance variables dynamically, whereas in mine I created a class/dataclass. Since that class/dataclass is the return of my setup function, get_state can infer that it’s returning that class/dataclass and then type checking/code complete works in the editor.

Makes sense. Thanks.

Very cool. We’ve been thinking about this as well and will post some possible designs. :+1:

A more generic solution for Classes which the only downside is that all parameters must be initialized with default values.

def MyClass:
    __init__(self, a=None, b=None):
        self.a = a
        self.b = b

def get_class(class_, **kargs):
    return class_(**kargs)

my_class = get_state(get_class, class_=MyClass, a=a, b=b)

@biellls thanks for so useful functions :smiley:

1 Like


I’m getting this error:

AttributeError: ‘ReportSession’ object has no attribute ‘_main_dg’


  File "e:\anaconda3\envs\myenv\lib\site-packages\streamlit\", line 314, in _run_script
    exec(code, module.__dict__)
  File "", line 139, in <module>
    session_state = SessionState.get(question=1)
  File "", line 61, in get
    if session_info.session._main_dg == ctx.main_dg:

any idea how to fix it?

This is the solution I came up with:

class Session:

def fetch_session():
    session = Session()
    session.question = 1
    return session

session_state = fetch_session()

it works so far

Also getting AttributeError: 'ReportSession' object has no attribute '_main_dg' error.

downgraded streamlit version to 0.50.2 and it works now.

Hey there! There is a gist for the session state. From streamlit changelog:
If you’re using the SessionState hack Gist, you should re-download it! Depending on which hack you’re using, here are some links to save you some time:


1 Like

should I just include this in the session state file or ?