I’m building a Streamlit app to analyze Excel files. In the app, I have a selectbox where users can choose a column from the uploaded Excel file. However, each time I change the value of the selectbox to a different column, the entire page refreshes. This behavior is not desired as it disrupts the user experience. How can I modify the code to prevent the page from refreshing every time the selectbox value changes?
import pandas as pd
import helpers.script as script
@st.cache_data # Cache the function based on month and year
def get_excel_path(month, year):
return script.get_data(month, year) # Call the original function
def read_excel_file(uploaded_file):
"""Reads the uploaded CSV file and returns a pandas DataFrame."""
try:
df = pd.read_excel(uploaded_file)
return df
except Exception as e:
st.error(f"Error reading the file: {e}")
return None
def display_unique_values(df, selected_column):
"""Displays the unique values in the selected column."""
if selected_column:
unique_values = df[selected_column].unique()
st.write(f"Unique values in '{selected_column}':")
st.dataframe(unique_values)
st.title("Excel File Analyzer")
year = st.number_input("Enter Year:", min_value=2024, max_value=None)
month = st.number_input("Enter Month (1-12)", min_value=1, max_value=12, step=1)
excel_path = None
df = None
if st.button("Generate Report"):
excel_path = get_excel_path(month, year)
if excel_path is not None:
df = read_excel_file(excel_path)
if df is not None:
# Get all column names
column_options = list(df.columns)
# Create a selectbox
selected_column = st.selectbox("Select a column:", column_options, key="column_selectbox")
display_unique_values(df.copy(), selected_column) # Avoid modifying original DataFrame
st.stop()