Skip to main content

No project description provided

Project description

streamlit-server-state

A "server-wide" state shared across the sessions.

Tests

PyPI PyPI - Python Version PyPI - License PyPI - Downloads

ko-fi

Buy Me A Coffee

GitHub Sponsors

import streamlit as st

from streamlit_server_state import server_state, server_state_lock

st.title("Global Counter Example")

with server_state_lock["count"]:  # Lock the "count" state for thread-safety
    if "count" not in server_state:
        server_state.count = 0

increment = st.button("Increment")
if increment:
    with server_state_lock.count:
        server_state.count += 1

decrement = st.button("Decrement")
if decrement:
    with server_state_lock.count:
        server_state.count -= 1

st.write("Count = ", server_state.count)

As above, the API is similar to the built-in SessionState, except one major difference - a "lock" object. The lock object is introduced for thread-safety because the server-state is accessed from multiple sessions, i.e. threads.

Auto-rerun

When you assign a value to a server-state item, server-state[key], server-state automatically triggers re-running of all other sessions in which that server-state item is referred to so that all the references to the server-state return the latest value and all the sessions are kept up-to-date.

For example, with this mechanism, the sample chat app (app_chat.py) keeps showing the latest message list for all users.

Suppressing auto-rerun

When this auto-rerun mechanism is not good for your use case, you can suppress auto-reruns upon the value assignments by using no_rerun context as below.

from streamlit_server_state import server_state, no_rerun


with no_rerun:
    server_state["foo"] = 42  # This does not trigger re-running of other sessions

Manually trigger re-running

Upon each value assignment, server-state checks whether the value has been changed and skips re-running if it has not for efficiency. This works well in most cases, but it does not for example when the value is a complex mutable object and its field is mutated, while such usages are not recommended.

As exceptions, in such cases where the auto-rerun mechanism does not work well, you can manually trigger re-running by using force_rerun_bound_sessions(key).

if "foo" not in server_state:
    server_state["foo"] = SomeComplexObject()

server_state["foo"].field = 42  # If this assignment does not trigger re-running,

force_rerun_bound_sessions("foo")  # You can do this.

Background: https://discuss.streamlit.io/t/new-library-streamlit-server-state-a-new-way-to-share-states-across-sessions-on-the-server/14981/10

Examples

  • app_global_count: A sample app like the official counter example for SessionState which uses streamlit-server-state instead and the counter is shared among all the sessions on the server. This is a nice small example to see the usage and behavior of streamlit-server-state. Try to open the app in multiple browser tabs and see the counter is shared among them.
  • app_global_slider: A slider widget (st.slider) whose value is shared among all sessions.
  • app_chat.py: A simple chat app using streamlit-server-state.
  • app_chat_rooms.py: A simple chat app with room separation. Open in Streamlit

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_server_state-0.20.2.tar.gz (88.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_server_state-0.20.2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_server_state-0.20.2.tar.gz.

File metadata

  • Download URL: streamlit_server_state-0.20.2.tar.gz
  • Upload date:
  • Size: 88.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for streamlit_server_state-0.20.2.tar.gz
Algorithm Hash digest
SHA256 d14423acfbe794e0d2f9dcf73e2cffad77fb210eda69cf5da0bbd9140d1e84b6
MD5 81650ffaa8c50bbb671c557a9040ceea
BLAKE2b-256 41bb4392d151cce26fc5dbe8344d3dc6479dd210224aded0d031aebf37477bc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_server_state-0.20.2.tar.gz:

Publisher: main.yml on whitphx/streamlit-server-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file streamlit_server_state-0.20.2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_server_state-0.20.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e183d2ef076b84fe6e5dcc8c32cec1998519ed62d18bd6cdd38e0ec3b3c4bce5
MD5 12518ef1870783b9e1755b4f831fe2c6
BLAKE2b-256 46c4ac41159f57dcea4973fc7df7b3914e960701a1e991324f59ca1b2c3fa582

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_server_state-0.20.2-py3-none-any.whl:

Publisher: main.yml on whitphx/streamlit-server-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page