Multiselect/Selectbox doesn’t wait after first selection - Streamlit

Viewed 1185

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
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()
1 Answers

A user on the streamlit community helped answer this question. I wanted to make sure, the answer was provided here so anybody who comes looking is also provided here:

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):
    columns_list = df.columns
    with st.form(key='Selecting Columns'):
        columns_to_aggregate = st.multiselect(
            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()
    if user_file_path:
        load = st.sidebar.checkbox(label='Load Data')

        if load:
            df = pd.read_csv(user_file_path)
            st.dataframe(df)
            clean_df = get_filtered_dataframe(df)

            if clean_df is not None:
                result = clean_df.describe()
                st.dataframe(result)

main()
Related