0

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()
8
  • 1
    Please clarify your problem, currently, it is very hard to understand what the problem is. Commented Aug 26 at 18:04
  • 1
    How can I set the nameoption and dateoption fields equal to what the parameters state? Commented Aug 26 at 18:07
  • You should edit that into the question. Commented Aug 26 at 18:17
  • I don't now if I understand problem but if you want to change value inside widget then you have to filter data before creating widget and assign new value when you create widget. Commented Aug 26 at 20:03
  • better create minimal working code which we could test. And you have to better descrive what you want to get. Commented Aug 26 at 20:04

1 Answer 1

0

Frankly, I don't know if I understand what you want to do but I would do:

It displays widgets with parameters from url (query_params)
and it also assigns values to variables nameoptions, dateoption

import datetime
import streamlit as st

params = st.query_params.to_dict()

# ---

all_names = ['Mary','Beth','Sue','Bob']

if 'name' in params and params['name'] in all_names:
    param_index = all_names.index(params['name'])
else:
    param_index = None

nameoption = st.sidebar.selectbox(f"Select from {len(all_names)} names", all_names, index=param_index, placeholder="Name")

# ---

if 'date' in params:
    try:
        param_date = datetime.datetime.strptime(params['date'], '%Y%m%d').date()
    except Exception as ex:  # if `date` has wrong format`
        print(f"{ex = }")
        param_date = datetime.date(2019, 7, 6)
else:
    param_date = datetime.date(2019, 7, 6)

dateoption = st.sidebar.date_input("Select Date", value=param_date)

# ---

st.text(F"name: {nameoption}, date: {dateoption}")

EDIT:

Similar code using only try/except

import datetime
import streamlit as st

params = st.query_params.to_dict()

# ---

all_names = ['Mary','Beth','Sue','Bob']

try:
    param_index = all_names.index(params['name'])
except Exception as ex:
    print(f"{ex = }")
    param_index = None

nameoption = st.sidebar.selectbox(f"Select from {len(all_names)} names", all_names, index=param_index, placeholder="Name")

# ---

try:
    param_date = datetime.datetime.strptime(params['date'], '%Y%m%d').date()
except Exception as ex:
    print(f"{ex = }")
    param_date = datetime.date(2019, 7, 6)

dateoption = st.sidebar.date_input("Select Date", value=param_date)

# ---

st.text(F"name: {nameoption}, date: {dateoption}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.