Multi-Chart, Multi-Submission for Data Comparison

I’m trying to compare two charts side by side that can be sourced from different datasets with the same structure.

This is what it looks like:

This is what one of the columns looks like:


    col1, col2 = st.columns(2)

    #Left Column
    with col1:

        dataset1 = st.selectbox(
            'Pick Type',
            datasets)

        df1 = pd.read_csv(os.path.join(".\\data\\",dataset1+".csv"), index_col=False)

        option1 = st.selectbox(
            'Pick Flight',
            df1['log_id_full'].unique(),
            key='option1')

        col1_choice = list(df1.columns)
        col1_choice.remove('timestamp')
        columns1 = st.multiselect(
            'Pick Values',
            col1_choice,
            key='col1'
        )

        if 'df1_filtered' not in st.session_state:
            st.session_state.df1_filtered = df1.loc[df1['log_id_full'] == option1]

        form1= st.form(key='form1')
        submit1 = form1.form_submit_button('Submit')

        if submit1:
            st.line_chart(
                st.session_state.df1_filtered, 
                x = 'timestamp', 
                y = columns1)

    #Right column
    with col2:
        dataset2 = st.selectbox(
            'Pick Type',
            datasets,
            key = 'dataset2')

        df2 = pd.read_csv(os.path.join(".\\data\\",dataset2+".csv"), index_col=False)

        option2 = st.selectbox(
            'Pick Data',
            df2['log_id_full'].unique(),
            key = 'option2')

        col2_choice = list(df2.columns)
        col2_choice.remove('timestamp')
        columns2 = st.multiselect(
            'Pick Values',
            col2_choice,
            key='col2'
        )

        if 'df2_filtered' not in st.session_state:
            st.session_state.df2_filtered = df2.loc[df2['log_id_full'] == option2]

        form2 = st.form(key='form2')
        submit2 = form2.form_submit_button('Submit')

        if submit2:
            st.line_chart(
                st.session_state.df2_filtered, 
                x = 'timestamp', 
                y = columns2)

If I remove the submit button one chart always shows an error until it gets values selected so I basically just need each column to operate independently and preserve state.

The above is how I tried to add stateful-ness based on the documentation examples. But it still behaves such that when I submit one it reruns the app and removes the line chart.

I know there are other optimizations, best-practice improvements I can make but this app is just for a POC and I was hoping to demo this functionality