I have a simple streamlit app. I wish to take 4 inputs from user and display the graph corresponding to those inputs.. import streamlit as st name_option = st.selectbox("Select Name", ['A', 'B', 'C']) gender_option = st.selectbox("Select Gender", ['M', 'F']) date_option = st.selectbox("Select Date", predefined_date_list) But Streamlit is rerunning a script every time an action occurs for example when I select name it reruns the script. Is there any way I can change this behaviour ? I would like to display the visualization based on the 4 selection (3 in pseudo example above)
To upload files, please first save the app
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
names = ['A', 'B', 'C']
sexes = ['M', 'F']
dates = pd.date_range(start='2023-01-01', periods=10).date.tolist()
# Inputs
name_option = st.selectbox("Select Name", names)
gender_option = st.selectbox("Select Gender", sexes)
date_option = st.selectbox("Select Date", dates)
value_option = st.slider("Select Value", 0, 100)
# Create a placeholder for the figure
fig_placeholder = st.empty()
# Store the selection state
if "inputs" not in st.session_state:
st.session_state.inputs = {"name": None, "gender": None, "date": None, "value": None}
# Update session state on each selection
st.session_state.inputs["name"] = name_option
st.session_state.inputs["gender"] = gender_option
st.session_state.inputs["date"] = date_option
st.session_state.inputs["value"] = value_option
# Visualization based on selections
if all(st.session_state.inputs.values()):
# Generate some data based on user selection
fig, ax = plt.subplots()
ax.bar(st.session_state.inputs["name"], st.session_state.inputs["value"], color=['blue' if st.session_state.inputs["gender"] == 'M' else 'pink'])
ax.set_ylabel('Value')
ax.set_title(f'Visualization for {st.session_state.inputs["name"]}, {st.session_state.inputs["gender"]}, {st.session_state.inputs["date"]}')
# Display the figure
fig_placeholder.pyplot(fig)
else:
fig_placeholder.write("Please select all options to visualize the data.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?