Share state through URL

Hi,

this may have been asked in different threads previously, e.g., there is a related thread, however it’s more than a year old and my question is slightly different:

Now that there is some rudimentary session management in streamlit, I was wondering if there was a simple way to encode/hash the entire app state into the URL – so that a URL exists that users can share e.g. to discuss a certain view on some visualised data. So I don’t just want the URL to contain which subpage is viewed but also the state of all checkboxes, sliders, et cetera. Any ideas how this could be archieved?

Cheers

Hi @jml!

I’m not sure how it was done, but @AustinC has created an app that does something similar. It super cool, and allows the user to type up some Streamlit code in an editor and the output is displayed next to it. Then, you can hit a button “share your work” and a url is generated that saves the current state to send to people!
Here is the link

@AustinC maybe you can post your github link to the code you used?

Happy Streamlit-ing!
Marisa

1 Like

Hey @jml,

This is how the flow would look like,

  1. Create a session state
  2. Store widget states in the state.conf as a dict.
  3. dump the state.conf to json and set as URL param.

I just whipped up a template for you :smiley:

import streamlit as st
from state import provide_state
import json

INITIAL_CONF = {
    "checkbox": False,
    "number": 0.0
}

@provide_state
def main(state):
    query_params = st.experimental_get_query_params()
    if "conf" in query_params:
        state.conf = json.loads(query_params["conf"][0])
    state.conf = state.conf or INITIAL_CONF

    state.conf["checkbox"] = st.checkbox("Retain State !", value=state.conf["checkbox"])
    state.conf["number"] = st.number_input("You Too Retain State !", value=state.conf["number"])
    st.experimental_set_query_params(**{"conf": json.dumps(state.conf)})

main()

Get provide_state decorator from here ,https://gist.github.com/ash2shukla/ff180d7fbe8ec3a0240f19f4452acde7
The outcome looks like this,

I use something similar for a ML lifecycle management application I built for my client using streamlit to share experiments. The better thing to do would be to save this session state json in a mongodb or some where else and just set the uid as a URL param and pick up the state on run of main from DB.
Because the URL params cant hold more than 2048 chars :slight_smile:

Hope it helps !

PS. I think Austin is doing something similar, perhaps just dumping the JSONs on fs instead of a document store and all. :smiley:

5 Likes

Hi @jml

And if you’re already using the other session_state hack from here you could also do it like I show here where I also show a way of setting state of other widgets by the click of a button :slight_smile:

@ash2shukla’s approach looks clean though!

3 Likes

That’s great, thanks a lot to all of you! I’ll give it a try and report back :slight_smile:

1 Like

This is very useful. The experimental get_query_params and set_query_params seemed to work well too, rhough I didn’t try out everything.

Dinesh

1 Like