streamlit 1.29.0
Python 3.9.17
import streamlit as st
def get_url_param(key):
url_params = st.experimental_get_query_params()
print(‘url_params’,url_params)
if key in url_params.keys():
return url_params[key][0] # All parameter values are returned as lists, hence the zero subscripting
Try using a callback to update the query parameter:
import streamlit as st
def get_url_param(key):
url_params = st.experimental_get_query_params()
print('url_params',url_params)
if key in url_params.keys():
return url_params[key][0] # All parameter values are returned as lists, hence the zero subscripting
return ''
def set_url_param(key, value):
url_params = st.experimental_get_query_params()
url_param_keys = list(url_params.keys())
url_params.update({key : value})
st.experimental_set_query_params(**url_params)
def update_param_from_widget(key):
# Assumes the key in session state and the name of the parameter are the same
st.session_state[key] = st.session_state[key].strip().upper()
set_url_param(key, st.session_state[key])
url_ref_id = get_url_param('ref_id')
print('url_ref_id',url_ref_id)
ref_id, asset_id = '',''
ref_id = st.text_input(f'Ref ID :', value=url_ref_id, key='ref_id', on_change=update_param_from_widget, args=['ref_id'])
st.write('textbox output',ref_id)
Oh, right. That’s a warning when there’s a difference between the declared default value and a value assigned by Session State. It doesn’t hurt anything, but yes, I recommend not using the value parameter (if you want the text box to update with the reformatted value as I set it to do). The change is simply to make the last section of code assign the value directly in Session State and not use value:
import streamlit as st
def get_url_param(key):
url_params = st.experimental_get_query_params()
print('url_params',url_params)
if key in url_params.keys():
return url_params[key][0] # All parameter values are returned as lists, hence the zero subscripting
return ''
def set_url_param(key, value):
url_params = st.experimental_get_query_params()
url_param_keys = list(url_params.keys())
url_params.update({key : value})
st.experimental_set_query_params(**url_params)
def update_param_from_widget(key):
# Assumes the key in session state and the name of the parameter are the same
st.session_state[key] = st.session_state[key].strip().upper()
set_url_param(key, st.session_state[key])
url_ref_id = get_url_param('ref_id')
st.session_state.ref_id = url_ref_id # Assign the value to State Session instead of the `value` parameter
print('url_ref_id',url_ref_id)
ref_id, asset_id = '',''
ref_id = st.text_input(f'Ref ID :', key='ref_id', on_change=update_param_from_widget, args=['ref_id'])
st.write('textbox output',ref_id)
All I did (and all you need to do in general) is take out value from the widget and use an assignment to Session State right before, using the widget’s key:
Thanks for your time , I see the textbox content is working , but if I put the same use case for radio button , it’s not changing the url . update_param_from_widget() not getting the modified value also.
import streamlit as st
def get_url_param(key):
url_params = st.experimental_get_query_params()
print('url_params',url_params)
if key in url_params.keys():
return url_params[key][0] # All parameter values are returned as lists, hence the zero subscripting
return ''
def get_url_param_index(options, key):
field_value = get_url_param(key)
if field_value != '' and field_value in options:
return options.index(field_value)
return 0
def set_url_param(key, value):
url_params = st.experimental_get_query_params()
url_param_keys = list(url_params.keys())
url_params.update({key : value})
st.experimental_set_query_params(**url_params)
def update_param_from_widget(key):
# Assumes the key in session state and the name of the parameter are the same
st.session_state[key] = st.session_state[key].strip().upper()
set_url_param(key, st.session_state[key])
queue_options = ["Analysis Not Started", "Analysis Started", "Analysis Lead Review", "Analysis CX Review"]
url_queue_index = get_url_param_index(queue_options, "queue")
st.session_state.queue = queue_options[url_queue_index]
queue = st.radio("Queue", queue_options,
on_change=update_param_from_widget, args=['queue'])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.