Programmatically Generating Streamlit App

I’m coming from the exploratory data analysis side and I have a use case that may be pushing the edge slightly.

Let’s say I’m building a data explorer app and I am going to make 5 plots with different data.

Instead of writing 5 sections of code to add selectboxes, checkboxes, and sliders, I want to write 1 section of code and loop through the sets of data, programmatically adding those checkboxes and sliders based on some configuration file, say a csv. I’ve done this, and it works rather well in automatically generating multiple sections of Streamlet widgets.

So to start off, this is AWESOME. I can write an analysis workflow in 25 lines of code.

My issue comes when I interact with my fancy new programmatically built Streamlit app. For example, because I am generating widgets in a for loop, I may use the st.checkbox function on line 25 multiple times. This sometimes results in the callback for that checkbox being applied to the wrong analysis section.

Here is a code sample where I experience this problem. I am generating lists containing the st objects as an attempt to keep track of the handles:

df = load_data('my_analysis_config.csv')
for ind in df.index:
     data.append(load_data(df['DataPath'][ind]))
     xFields.append(st.sidebar.selectbox('X-Value:',(data[ind].columns)))
     yFields.append(st.sidebar.selectbox('Y-Value:',(data[ind].columns)))
     colorField.append(st.sidebar.selectbox('Color by: ', data[ind].columns))
     fig = px.scatter(data[ind], 
         x=xFields[ind], 
         y=yFields[ind],
         color=colorField[ind])
     figs.append(st.plotly_chart(fig))

However, I also have the same problem when I don’t keep track of handles:

df = load_data('my_analysis_config.csv')
for ind in df.index:
     data = load_data(df['DataPath'][ind]))
     xField = st.sidebar.selectbox('X-Value:',(data.columns))
     yField = st.sidebar.selectbox('Y-Value:',(data.columns))
     colorField = st.sidebar.selectbox('Color by: ', data.columns)
     fig = px.scatter(data, 
         x=xField, 
         y=yField,
         color=colorField)
     st.plotly_chart(fig)

Any suggestions on best practices here?

1 Like

try with place_holder = st.empty() where you want to place your check box and then later when you want to show it say using for loop just use that place holder as paceholder.checkbox() and it will be in the correct section. hope it helped you.

1 Like