Summary
This is a copy of Bad behavior with set_query_params · Issue #6907 · streamlit/streamlit · GitHub , but maybe the community can answer as well.
st.experimental_set_query_params does not always write its value to the browser.
In the following example, the write will fail exactly every other time - the new selection will be discarded every 2nd selection.
Has anyone seen this behavior? Any workarounds?
Steps to reproduce
Code snippet:
from typing import List, Any
import streamlit as st
def selectbox_with_query_storage(label: str, options: List[Any], query_param_name: str, **kwargs):
default_index = 0
current_query_params = st.experimental_get_query_params()
current_value_params = current_query_params.get(query_param_name, [])
current_value = None
if len(current_value_params) == 1:
current_value_str = current_value_params[0]
current_value = type(options[0])(current_value_str) # Convert to type based on first option.
try:
default_index = options.index(current_value)
except ValueError:
pass
value = st.selectbox(label, options, index=default_index, **kwargs)
if value != current_value:
current_query_params[query_param_name] = value
st.experimental_set_query_params(**current_query_params)
return value
val = selectbox_with_query_storage("Remember me", ["a", "b", "c", "d", "e"], query_param_name="choice")
st.write(val)
https://noamgat-streamlit-example-streamlit-app-x1gje6.streamlit.app/
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Actual behavior:
- Select a value in the dropdown. The value and url will be updated.
- Select a value in the dropdown. The value and url will NOT be updated.
- Select a value in the dropdown. The value and url will be updated.
- Select a value in the dropdown. The value and url will NOT be updated.
Debug info
- Streamlit version: Latest
- Python version: 3.11
- Operating System: Windows 11
- Browser: Chrome
- Virtual environment: Conda
Links
- Link to your GitHub repo: Bad behavior with set_query_params · Issue #6907 · streamlit/streamlit · GitHub
- Link to your deployed app: