Callback Function Troubleshooting

Hello Streamlit Community,

I am a student and, as part of a final project for a Data Visualization class, my team is using Streamlit to deploy a simple dashboard. We managed to get a working implementation of our dashboard running, but there is a bug in our design of the callback functions. Changes to the dashboard only take effect after the submit button has been clicked twice. See this YouTube video for a demonstration. Our programing skills are basic and we are new to Streamlit, so the code is pretty hacky. But, the overall flow is this:

  1. We load a csv file into a panda’s dataframe.
  2. We set default values for the variables and define the two call back functions, seg_val_tracker and quartile_val_tracker
  3. The dashboard is rendered.
  4. When the user adjusts the form at the bottom of the page and hits submit, the dashboard updates.
  5. The problem is with the callback function in the form. This code reads:
with st.form("segment_form"):
    st.header("Factors")
    race_val = st.selectbox("Race", race_dropdown_vals, 
                    help="select a value")
    income_val = st.select_slider("Income", 
                    options = income_slider_vals, 
                    help = "select a value")
    internet_val = st.select_slider("Internet availability",
                    options = internet_dropdown_vals, 
                    help = "select a value")
    submitted = st.form_submit_button("Submit", on_click=seg_val_tracker, #The code below passes segment values to the callback function
                                    args=(race_val, income_val, internet_val, target_hours_val, ))

The code for the callback function is

def seg_val_tracker(race_val, income_val, internet_val, target_hours_val):
    new_seg_val = pred_df[(pred_df["RACE_ETHNICITY"] == race_val) \
    & (pred_df["INCOME"] == income_slider_vals.index(income_val) + 1)\
    & (pred_df["INTRNTAVAIL"] == internet_dropdown_vals[::-1].index(internet_val) + 1)\
    & (pred_df["SCHLHRS"] == target_hours_val)].iloc[:,[5]].values.astype(float)
    st.session_state["segment_val"] = float(new_seg_val) * 100

The function is a mess, but basically it filters the dataframe and returns the value the user selected. The filtering works as intended. The only problem is with the dashboard updating. As I say, it takes two clicks of the submit button.

The app is in a public repo here: streamlit-app/app.py at master Β· ikerson/streamlit-app Β· GitHub

It is deployed here: http://13.83.15.22:8501/

Thank you for reading this long question and any help you can provide.

Isaac

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