New library: streamlit-sync, another way to sync states across sessions

Hi everyone

I’m sharing streamlit-sync, a library that I created to share widget states across sessions.

My use case was to be able to have multiple users running and being synchronized to the same state. This could be useful to share a same dashboard in a remote meeting, while ensuring that everyone see exactly about the same results. It makes things more interactive than a screen sharing (even though it can prove to be inefficient.

Open in Streamlit

Here is a demo example I ran locally.

The API is built around a sync method that can be used around “any” existing dashboard (will probably have some issues

import streamlit_sync

room_name = streamlit_sync.select_room_widget()

with streamlit_sync.sync(room_name):
    app()

Internally I’m using private APIs from streamlit, especially to access the internal session state and to trigger reruns. I manually tested the solution on streamlit 1.0.0 to 1.8.0 but I can’t ensure any robustness/security/reliability.

Another library (streamlit-server-state - discussed here) already covers part of what is doing streamlit-sync. The big difference of this new library is the possibility to sync also the state of the widgets, not only the data.

Future experimentation I want to make possible to sync the state of a streamlit app with the local drive. This should be now doable for any data that is pickle-able. The use case I see is to be able to easily experiment different sets of parameter and saving them for later reuse if needed (and keep it between restarts of the server).

9 Likes

Awesome! I’m especially interested in your future experiment a to save the session and to be able to continue.
Let me know if there’s a way to support you. :+1:

Welcome to the community @Wauplin !
This looks awesome! Feel free to add it to our “components” community tracker :slight_smile: Streamlit Components - Community Tracker - Show the Community! - Streamlit

Have a nice day :balloon:
Fanilo

1 Like

Glad to you it could be of interest for you ! :slight_smile:

I worked on the persistent mode and now have a working draft for it. Please checkout this PR: first draft for persistence by Wauplin · Pull Request #1 · Wauplin/streamlit-sync · GitHub . I use diskcache which does the job nicely.

The API syntax would not change a lot:

import streamlit as st

import streamlit_sync

CACHE_DIR = "./.st_sync_cache"

room_name = streamlit_sync.select_room_widget(CACHE_DIR)

with streamlit_sync.sync(room_name, cache_dir=CACHE_DIR):
    y = st.slider("Select a value")
    st.write(y, "squared is", y * y)

By default, sessions are still only synced in memory but a “cache_dir” parameter can be passed. In the backend,

  1. What do you think about the API. Would it be convenient for you to use ? Another way I see is to have a global streamlit_sync.set_cache_dir(...) to call at the beginning of the script. It would avoid the duplicated “cache_dir” argument but also allow less modularity since it’s global.
  2. A nice help would be to try it on your side and tell me if you encounter any problems. Since I only tested it on my example, I might be missing an important use case.

Have a nice day,

Wauplin

Done :+1:

This is interesting, thanks for sharing! The first potential application that comes to mind is setting up a multiplayer game. Obviously streamlit isn’t the best framework for game dev, but maybe for games that could fit into this framework, this could allow a beginner pythonista to set up a simple multiplayer game on streamlit cloud

1 Like

@benlindsay Hi,
That’s a wonderful idea - then let me share this. This is a multi-player game using the server-state (New library: streamlit-server-state, a new way to share states across sessions on the server - #2 by TeddyHuang-00).

As @benlindsay said, Streamlit is not the best for game devs, but this example shows some of its potential :slight_smile:
streamlit-sync-based games can be there too :tada:

2 Likes