• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A "server-wide" state object shared across sessions on a Streamlit server.

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

More Repositories

1

streamlit-webrtc

Real-time video and audio streams over the network, with Streamlit.
Python
1,348
star
2

stlite

In-browser Streamlit ๐ŸŽˆ๐Ÿš€
TypeScript
1,138
star
3

vscode-emacs-mcx

Awesome Emacs Keymap - VSCode emacs keybinding with multi cursor support
TypeScript
367
star
4

streamlit-stt-app

Real time web based Speech-to-Text app with Streamlit
Python
213
star
5

streamlit-webrtc-example

Real time video and audio processing examples with Streamlit and streamlit-webrtc
Python
144
star
6

lear-gist-python

A python wrapper for Lear's GIST implementation working with numpy
C
55
star
7

transformers.js.py

TypeScript
55
star
8

streamlit-component-template-react-hooks

Streamlit component lib with React hooks and template project using it
TypeScript
52
star
9

react-ymd-date-select

Hooks and components for Y-M-D dropdowns with React
TypeScript
45
star
10

streamlit-video-chat-example

Video chat apps with computer vision filters built on top of Streamlit
Python
42
star
11

stlite-image-processing-app

Serverless OpenCV image manipulation app with Streamlit
HTML
31
star
12

streamlit-fesion

TypeScript
18
star
13

streamlit-webrtc-article-tutorial-sample

Python
16
star
14

ReactCrossDeviceTodoExample

Sample app running on iOS / android / web (+ electron) using React, React native, Redux
JavaScript
15
star
15

stlite-desktop-example

Python
12
star
16

streamlit-theme-editor

Python
11
star
17

fastapi-typescript-openapi-example

TypeScript
10
star
18

tiny-streamlit-webrtc

Python
7
star
19

react-redux-pouch-example

Sample project using react, redux, react-router and pouchdb
JavaScript
7
star
20

quarto-stlite

Lua
5
star
21

randomstring-promise

Random string generator with promise interface for nodejs, browsers, and react-native
JavaScript
4
star
22

text-classification-app-example

Text classification server written in python (Flask, scikit-learn) and its interface using Vue.js
Jupyter Notebook
3
star
23

fuel-kana

ๆ—ฅๆœฌ่ชžใฎใƒ•ใƒชใ‚ฌใƒŠใ‚’HTMLใง่กจ็พใ™ใ‚‹ใŸใ‚ใฎ<ruby>ใ‚ฟใ‚ฐใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใƒ˜ใƒซใƒ‘ใƒผ
PHP
3
star
24

whitphx.info

TypeScript
2
star
25

risk-assessment-long-term-investment

Python
2
star
26

stlite-sample

Python
2
star
27

streamlit-appengine-samples

Dockerfile
2
star
28

europython2022-streamlit-webrtc-example

Python
2
star
29

europython2022-streamlit-webrtc

2
star
30

pycon2024-streamlit

1
star
31

stlite-desktop

1
star
32

streamlit-webrtc-hugging-face-transformer-example

Python
1
star
33

csv-to-md

CSV to Markdown converter https://tuttieee.github.io/csv-to-md/
JavaScript
1
star
34

kms7-decoder-bug-example

TypeScript
1
star
35

gstreamer-python-sample

Python
1
star
36

asha-nepal

CSS
1
star
37

europython2022-streamlit-webrtc-example-predeploy

Python
1
star