Skip to content

Instantly share code, notes, and snippets.

@asehmi
Created April 19, 2022 13:53
Show Gist options
  • Save asehmi/3e80b865080c969416d6c0232ad0bdbb to your computer and use it in GitHub Desktop.
Save asehmi/3e80b865080c969416d6c0232ad0bdbb to your computer and use it in GitHub Desktop.
Setting and getting Streamlit query params
import streamlit as st
# callback to update query param on selectbox change
def update_params():
st.experimental_set_query_params(option=st.session_state.qp)
options = ["cat", "dog", "mouse", "bat", "duck"]
query_params = st.experimental_get_query_params()
# set selectbox value based on query param, or provide a default
ix = 0
if query_params:
try:
ix = options.index(query_params['option'][0])
except ValueError:
pass
selected_option = st.radio(
"Param", options, index=ix, key="qp", on_change=update_params
)
# set query param based on selection
st.experimental_set_query_params(option=selected_option)
# display for debugging purposes
st.write('---', st.experimental_get_query_params())
@asehmi
Copy link
Author

asehmi commented Apr 19, 2022

query_params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment