Have to feed the textbox input twice , Earlier it was working piece of code

sample code is local.

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

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)

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’).strip().upper()
st.write(‘textbox output’,ref_id)
set_url_param(‘ref_id’, ref_id)

Hi @Geetha,

Tip: If you enclose the snippet in triple-backticks (```) it will display as code. Otherwise, you can select code formatting in the GUI.

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)

The widget with key “ref_id” was created with a default value but also had its value set via the Session State API.
Getting this error

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)

Sorry for the oversight.

I need value, because the actual code is multi page app , it gets the value from other page and fills it in the textbox

Whatever you assign to the parameter value just needs to be assigned to the widget’s key in Session State immediately before the widget.

can u provide a sample code?

You originally wrote:

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)

Let’s remove the print statement and extra variable assignment to focus on the point at hand. This means you started with:

url_ref_id = get_url_param('ref_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']) 

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:

url_ref_id = get_url_param('ref_id')
st.session_state.ref_id = url_ref_id
ref_id = st.text_input(f'Ref ID :', key='ref_id', on_change=update_param_from_widget, args=['ref_id']) 

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'])


Also I have multiapp page which has similar piece of code in sidebar . Not sure how to handle that

multiapp.py

class MultiApp:
    def __init__(self):
        self.apps = []

    def add_app(self, title, func):
        self.apps.append({
            "title": title,
            "function": func
        })

    def run(self):
        app = st.sidebar.radio(
            'Meter Analysis',
            self.apps,
            format_func=lambda app: app['title'],
            index=get_url_param_index([app['title'] for app in self.apps], 'app')
        )
        #st.write('session state radio', st.session_state )
        set_url_param('app', app['title'])
        st.sidebar.markdown("""---""")
        app['function']()

I could able to replicate your fix . Ignore my previous question .

Thank you so much

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.