Code on submit of selectbox or radio selection inside form fails to show up

Hi,

I have been scratching my head for two hours now and have read and re-read forms, selectbox and radio but am getting no where so thought I will ask here.

What I am trying to do is this:

  1. Select Excel 1
  2. Select Excel 2
  3. if Excel 1 exists:
    1. Obtain Sheets in Excel 1
    2. Show Sheets in dropdown
  4. if Excel 2 exists:
    1. Obtain Sheet in Excel 2
    2. Show Sheets in dropdown
  5. User must select a Sheet from each of the above dropdowns
  6. Using Sheetname, read data from that sheet into a dataframe.
  7. Provide dropdown to show column names from each dataset
  8. User must select the columns to compare
  9. Comparison should then show for the specified column Venn Diagram and data with exclusive entries in each dataset.

Now, I have done a simpler version of this where I hard code the sheetname and last 4 steps will work. Problem I am facing is from Steps 3 to 5. So I read the sheets in uploaded excels and the sheets start displaying on the dropdown or as radio button. However, If I donโ€™t have the selectboxes / radio inside a form, then the whole page refreshes and the dataframe gets loaded with the first sheet as that is what is selected by default. An additional entry โ€œSelectโ€ added to โ€˜sheetnamesโ€™ did not help as everything just refreshed anyway.

I then added a form. With form added the screen was not refreshing by itself but after clicking on submit button it just refreshes the page and does not load the dataframes.

Not sure what I am doing wrong and any guidance will be hugely appreciated. The code is as shown below:

import streamlit as st
import pandas as pd
import numpy as np
from pandas import ExcelWriter
import re

def read_upload(obj_file,str_sheetname,col_list=None, date_col_list=False, file_type='xlsx'):
    import pandas as pd
    dataframe=""
    
    try:
        if obj_file is not None:
            if file_type == 'csv':
                dataframe = pd.read_csv(obj_file, usecols=col_list, parse_dates = date_col_list).applymap(lambda s: s.upper() if type(s) == str else s).fillna('')
            else:
                dataframe = pd.read_excel(obj_file,sheet_name = str_sheetname, usecols=col_list, parse_dates = date_col_list).applymap(lambda s: s.upper() if type(s) == str else s).fillna('')
            with st.expander(str_sheetname+" Sample", expanded=False):
                #st.markdown('**Sample of upload**')
                st.write(dataframe.head())
        else:
            dataframe="Empty - Upload a file first"
            st.warning(dataframe)			
    except ValueError as e:
        st.error("Problem: "+ e.args[0])
    finally:
        return dataframe

def get_sheetnames(upload_file):
    from openpyxl import load_workbook
    wb = load_workbook(upload_file, read_only=True, keep_links=False)
    return wb.sheetnames

file_upload_container = st.container()
sheet_selection_container = st.container()
column_selection_container = st.container()

with file_upload_container:
    f_col1, f_col2 = st.columns(2)
    f_button = st.empty()
with sheet_selection_container:
    s_col1, s_col2 = st.columns(2)
    s_button = st.empty()
with column_selection_container:
    c_col1, c_col2 = st.columns(2)
    c_button = st.empty()

with f_col1:
    f1_uploader = st.file_uploader(
        "Choose file 1",
        help="Upload the first file in .xls or .xlsx format",
        type=['xlsx','xls'])
    error_placeholder_f1 = st.empty()
with f_col2:
    f2_uploader = st.file_uploader(
        "Choose file 2",
        help="Upload the second file in .xls or .xlsx format",
        type=['xlsx','xls'])
    error_placeholder_f2 = st.empty()

with f_button:
    submitted = st.button("Upload")
if submitted:
    if f1_uploader is not None and f2_uploader is not None:
        # Read sheet names for both files
        f1_sheets = get_sheetnames(f1_uploader)
#        f1_sheets_mod = ("Select",*f1_sheets)
        f2_sheets = get_sheetnames(f2_uploader)
#        f2_sheets_mod = ("Select",*f2_sheets)
        with st.form(key="Select Column",clear_on_submit=False):
            sheet1 = st.radio("File 1 Sheets",f1_sheets, help = "Select a sheet from this list")
            st.write("You selected: ", sheet1)
            sheet2 = st.radio("File 2 Sheets",f2_sheets, help = "Select a sheet from this list")
            st.write("You selected: ", sheet2)
            submit_button = st.form_submit_button(label='Submit')

            if submit_button:
                #if sheet1 != "Select" and sheet2 != "Select":
                with f_col1:
                    st.write(sheet1)
                    sheet1_data = read_upload(f1_uploader,sheet1)
                with f_col2:
                    st.write(sheet2)
                    sheet2_data = read_upload(f2_uploader,sheet2)
                #else:
                #    st.warning('Sheets for comparison must be selected.')


    elif f1_uploader is None:
        with error_placeholder_f1:
            st.error("Please upload file 1.")
    else:
        with error_placeholder_f2:
            st.error("Please upload file 2.")

Well I have just figured out that it was the button after file uploads which was causing the complete refresh each time. If I remove that button it does work as expected. However, I am none the wiser on how to achieve the button functionality as I intended. Guidance will be appreciated. Code that works as expected is as shown below:

import streamlit as st
import pandas as pd
import numpy as np
from pandas import ExcelWriter
import re

def read_upload(obj_file,str_sheetname,col_list=None, date_col_list=False, file_type='xlsx'):
    import pandas as pd
    dataframe=""
    
    try:
        if obj_file is not None:
            if file_type == 'csv':
                dataframe = pd.read_csv(obj_file, usecols=col_list, parse_dates = date_col_list).applymap(lambda s: s.upper() if type(s) == str else s).fillna('')
            else:
                dataframe = pd.read_excel(obj_file,sheet_name = str_sheetname, usecols=col_list, parse_dates = date_col_list).applymap(lambda s: s.upper() if type(s) == str else s).fillna('')
            with st.expander(str_sheetname+" Sample", expanded=False):
                #st.markdown('**Sample of upload**')
                st.write(dataframe.head())
        else:
            dataframe="Empty - Upload a file first"
            st.warning(dataframe)			
    except ValueError as e:
        st.error("Problem: "+ e.args[0])
    finally:
        return dataframe

def get_sheetnames(upload_file):
    from openpyxl import load_workbook
    wb = load_workbook(upload_file, read_only=True, keep_links=False)
    return wb.sheetnames

file_upload_container = st.container()
sheet_selection_container = st.container()
column_selection_container = st.container()

with file_upload_container:
    f_col1, f_col2 = st.columns(2)
    f_button = st.empty()
with sheet_selection_container:
    s_col1, s_col2 = st.columns(2)
    s_button = st.empty()
with column_selection_container:
    c_col1, c_col2 = st.columns(2)
    c_button = st.empty()

with f_col1:
    f1_uploader = st.file_uploader(
        "Choose file 1",
        help="Upload the first file in .xls or .xlsx format",
        type=['xlsx','xls'])
    error_placeholder_f1 = st.empty()
with f_col2:
    f2_uploader = st.file_uploader(
        "Choose file 2",
        help="Upload the second file in .xls or .xlsx format",
        type=['xlsx','xls'])
    error_placeholder_f2 = st.empty()

#with f_button:
#    submitted = st.button("Upload")
#    if submitted:

if f1_uploader is not None and f2_uploader is not None:
    # Read sheet names for both files
    f1_sheets = get_sheetnames(f1_uploader)
    f1_sheets_mod = ("Select",*f1_sheets)
    f2_sheets = get_sheetnames(f2_uploader)
    f2_sheets_mod = ("Select",*f2_sheets)

    with sheet_selection_container:            
        with s_col1:
            sheet1 = st.selectbox("File 1 Sheets",f1_sheets_mod, help = "Select a sheet from this list")
        with s_col2:
            sheet2 = st.selectbox("File 2 Sheets",f2_sheets_mod, help = "Select a sheet from this list")
        
        if sheet1 == "Select" or sheet2 == "Select":
            st.warning('Sheets from both files must be selected for comparison.')
            st.stop()
        else:
            with s_col1:
                st.write(sheet1)
                sheet1_data = read_upload(f1_uploader,sheet1)
            with s_col2:
                st.write(sheet2)
                sheet2_data = read_upload(f2_uploader,sheet2)
elif f1_uploader is None:
    with error_placeholder_f1:
        st.error("Please upload file 1.")
else:
    with error_placeholder_f2:
        st.error("Please upload file 2.")

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