Multiselect/Selectbox doesn't wait after first selection

All,

I have used multiselect successfully before, but when I try this specific example that I was trying as a POC, the behavior is very weird. Essentially, what I am trying to do is use multiselect to make the app wait for user input at an intermediate step. However, multiselect does not wait for me to select the inputs I want to select, as soon as I select one thing, it just runs and doesnโ€™t even execute correctly. Can someone guide me as to what am I doing wrong ? I am on version 0.82.

I also tested the same using selectbox and am seeing the same behavior.

So, here is what I have:

import streamlit as st
import pandas as pd

print(st.__version__)

def basic_skeleton() -> tuple:
    """Prepare the basic UI for the app"""
    st.sidebar.title('User Inputs')
    beta_expander = st.sidebar.beta_expander("Upload csv")
    with beta_expander:
        user_file_path = st.sidebar.file_uploader(
            label='Random Data',
            type='csv'
        )
    return user_file_path

def get_filtered_dataframe(df) -> pd.DataFrame:
    columns_list = list(df.columns)
    columns_to_aggregate = st.multiselect(
        label='Select columns to summarize',
        options=columns_list
    )
    df1 = df[columns_to_aggregate]
    return df1

def main():
    """Central wrapper to control the UI"""
    # add title
    st.header('Streamlit Testing')

    # add high level site inputs
    user_file_path = basic_skeleton()

    load = st.sidebar.button(label='Load Data')

    if load:
        df = pd.read_csv(user_file_path)
        st.dataframe(df)
        clean_df = get_filtered_dataframe(df)
        run = st.button("Aggregate Selected columns")

        if run:
            result = clean_df.describe(include='all')
            st.dataframe(result)

main()

I did record a screencast to share, but it is in webm format and that is not a format the community allows me to upload. I cant install converters on my work machine, so if there are any alternatives to upload the screencast, please let me know.

Even though you use Streamlit multi-select, once you select one input it will go to the next step. in your case, it will take the first column that you selected then pass to the next row df1 then return the data frame df1 which assigned to the variable clean_df. The variable clean_df used in the main function under st.button. Because of this, itโ€™s completing the program without processing further.

Sol: Use streamlit feature st.form

1 Like

@Venkatesh,
Thank you for your suggestion. But even with the updated usage of using streamlit form, I see the same behavior.

import streamlit as st
import pandas as pd

def basic_skeleton() -> tuple:
    """Prepare the basic UI for the app"""
    st.sidebar.title('User Inputs')
    beta_expander = st.sidebar.beta_expander("Upload csv")
    with beta_expander:
        user_file_path = st.sidebar.file_uploader(
            label='Random Data',
            type='csv'
        )
    return user_file_path

def get_filtered_dataframe(df) -> pd.DataFrame:
    columns_list = df.columns
    with st.form(key='Selecting Columns'):
        columns_to_aggregate = st.selectbox(
            label='Select columns to summarize',
            options=columns_list
        )
        submit_button = st.form_submit_button(label='Submit')
    if submit_button:
        df1 = df[columns_to_aggregate]
        return df1

def main():
    """Central wrapper to control the UI"""
    # add title
    st.header('Streamlit Testing')

    # add high level site inputs
    user_file_path = basic_skeleton()

    load = st.sidebar.button(label='Load Data')

    if load:
        df = pd.read_csv(user_file_path)
        st.dataframe(df)
        clean_df = get_filtered_dataframe(df)
        run = st.button("Aggregate Selected columns")

        if run:
            result = clean_df.describe(include='all')
            st.dataframe(result)

main()

Use checkbox instead of button in the sidebar load data line. Because once you click submit button load value becomes false instead of True.

1 Like