franekp / streamlit-permalink

MIT License
39 stars 2 forks source link

Widget value is lagging behind the actual URL #2

Closed simon-ging closed 9 months ago

simon-ging commented 1 year ago

Hi, widgets need two clicks to update, the get state is lagging behind. Minimum example below, after the second selection change the selection is always one step behind.

import streamlit as st
import streamlit_permalink as stp

with st.sidebar:
    page = stp.selectbox("Page", ["a", "b", "c"], index=0, help="Select page", url_key="page")
st.write(f"Selected page: {page}")

Versions streamlit 0.1.20 streamlit-permalink 0.3.0

Currently I am solving this by keeping a "live" version of the get state in st.session_state and only actually using the get parameters on reload, instead of using streamlit-permalink.

pavi2410 commented 1 year ago

I encountered this same issue, where I change an input value, other states are lost from the URL.

initial url = localhost:8501
* change input a
url = localhost:8501/?a=foo
* change input b
url = localhost:8501/?b=bar (expected localhost:8501/?a=foo&b=bar)
pavi2410 commented 1 year ago

Here's a minimal working code taking radio component as an example:

import streamlit as st

def get_url(key):
    qp = st.experimental_get_query_params()
    qv = qp.get(key, None)
    if qv:
        return qv[0]
    else:
        return None

def update_url(key):
    qp = st.experimental_get_query_params()
    qp[key] = st.session_state[key]
    print('on_change:', qp[key])
    st.experimental_set_query_params(**qp)

def url_radio(label, options, key, *args, **kwargs):
    def on_change():
        update_url(key)

    qv = get_url(key)

    if key in st.session_state:
        qi = options.index(st.session_state[key])
        print('get ss:', qi)
    elif qv:
        qi = options.index(qv)
        print('get qv:', qi)
    else:
        qi = 0
        print('get def:', qi)

    return st.radio(label=label, options=options, key=key, index=qi, on_change=on_change, *args, **kwargs)

r = url_radio('My radio', ['Me', 'Mine', 'Myself'], key='myradio')

st.write(f"{r=}")
pavi2410 commented 1 year ago

It seems streamlit==1.21.0 fixes this regression.