Created
April 19, 2022 13:53
-
-
Save asehmi/3e80b865080c969416d6c0232ad0bdbb to your computer and use it in GitHub Desktop.
Setting and getting Streamlit query params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Author
asehmi
commented
Apr 19, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment