I'm building an app that, on default, sets date_input and selectbox options
dateoption = st.sidebar.date_input("Select Date", value=get_previous_business_day(dt.date.today()))
nameoption = st.sidebar.selectbox(f"Select from {name_count} names", individual_names.NAME.values.tolist(), index=None, placeholder="Name")
The user can manually select a date and a name from the options.
I now have added functionality where the app's link can be embedded with parameters, so the dateoption and nameoption can be prefilled. However, I am struggling with how I can force select the two variables. This is what I'm trying:
params = st.query_params.to_dict()
if 'name' in params:
nameoption = params['name']
# This did not work, and I also tried selecting the value of the index to place as the option but that just created another nameoption selectbox right below the actual one
#st.session_state.nameoption = individual_names.NAME.values.tolist().index(params['name'])
if 'date' in params:
# This doesn't change the selection
dateoption = datetime.strptime(params['date'], '%Y%m%d').date()
How can I set the nameoption and dateoption fields equal to what the parameters state?
EDIT: A few of you were asking about some working code, please see below:
This is the initialized code:
import datetime
import streamlit as st
dateoption = st.sidebar.date_input("Select Date", value=datetime.date(2019, 7, 6))
nameoption = st.sidebar.selectbox(f"Select from {name_count} names", ['Mary','Beth','Sue','Bob'], index=None, placeholder="Name")
Now let's say that, in my app URL, I add name and date parameters: https://myworkingapp.com:40001/?name=Sue&date=20250501
This can be retrieved by fetching the params variable, which is in the form of a dictionary: params: {'name': 'Sue, 'date': '20250501'}
My question is how can I set the nameoption and dateoption variables to those values from the params dictionary. I tried setting them below, but that didn't work:
params = st.query_params.to_dict()
if 'name' in params:
nameoption = params['name']
# This did not work, and I also tried the line below selecting the value of the index to place as the option but that just created another nameoption selectbox right below the actual one
#st.session_state.nameoption = individual_names.NAME.values.tolist().index(params['name'])
if 'date' in params:
# This doesn't change the selection
dateoption = datetime.strptime(params['date'], '%Y%m%d').date()
minimal working codewhich we could test. And you have to better descrive what you want to get.