Summary
Hi all!
Iām looking for an efficient way of retrieving data from BigQuery based on user input from multiple selection boxes, where the selection boxes are dynamic. Using forms almost works for my use case - Iām unable to make the selection boxes within a form to dynamically update based on the preceding selections.
I can get the selection boxes to be dynamic if I donāt have them defined inside a form, but the trade-off is the database is queried every time a different selection is made in any one of the selection boxes.
Is there a way to maintain the dynamic selection boxes and only query the database when a button is pressed?
Steps to reproduce
Code snippet:
import pandas as pd
import streamlit as st
data = {
'year': [2023, 2023, 2023, 2022, 2022, 2022, 2022, 2022, 2022],
'segment': ['a', 'b', 'c', 'a', 'b', 'c', 'd', 'e', 'f']
}
df = pd.DataFrame(data)
# Select query parameters using a form to retrieve data from BigQuery
with st.sidebar.form(key='Selection'):
st.info('Selection using form')
years = sorted(df.year.unique())
form_year_select = st.selectbox('Select Year', years)
segments = sorted(df.segment[df.year == form_year_select].unique())
form_segment_select = st.selectbox('Select Segment', segments)
selection_submitted = st.form_submit_button('Submit')
form_query = f"""
select * from table where year = {form_year_select} and segment = '{form_segment_select}'
"""
st.sidebar.info(form_query)
# Select query parameters without using a form to retrieve data from BigQuery
st.info('Selection without using form')
years = sorted(df.year.unique())
year_select = st.selectbox('Select Year', years)
segments = sorted(df.segment[df.year == year_select].unique())
segment_select = st.selectbox('Select Segment', segments)
query = f"""
select * from table where year = {year_select} and segment = '{segment_select}'
"""
query
Expected behavior:
Each selection box should be dynamic where it selects only possible values from the DF e.g., if modelyear 2023 is selected, it shouldnāt show values for segments d, e, and f.
Actual behavior:
The options in the selection box are based on the default values when the form is initiated. Only when you click the Submit button do the values change in the selection boxes. This can lead to invalid parameters being parsed into the query e.g., model year = 2023 and segment = f.