Alternative implementation of session state

You can use it as is. This might make it more obvious:

class Session:
    pass

@st.cache(allow_output_mutation=True)
def fetch_session():
    session = Session()
    session.counter = 1
    return session

def main():
    session_state = fetch_session()
    session_state.counter += 1
2 Likes

Been a while since I played with the previous code, after which I realised:

The preceding code handles session state, if you define a session as a single bringup and teardown of the streamlit app. This is different to a session defined as the connection / interaction and disconnection by a single client from their browser. There can be one of the former, N of the latter.

This code above, actually represents a rather nice ‘View Counter’. You can track how often the application has been visited since it was launched.

If you’re looking for something that can count every interaction (e.g. change slider) between connect & disconnect from a single client, you’ll need to look to the more complicated solutions being advanced here.

1 Like

Hey all, :wave:

Wanted to let everyone know we have some updates regarding Session State! It’s now natively supported in Streamlit via the 0.84.0 release! :partying_face:

To use, upgrade to the latest version:

pip install --upgrade streamlit

Here are some helpful links:

Looking forward to hearing what you think :balloon:

@biellls @stonier @Ramyashree @jetilton @nnpy @benelot @Sumit_Kumar_Ram @M_A @avn3r @Marc