streamlit application with two selectbox and one button. After we press the button, we need to write the selected values from the selectboxes and then clean the selection to start over
To upload files, please first save the app
import streamlit as st
# Initialize session state for storing the history if it doesn't exist
if 'history' not in st.session_state:
st.session_state.history = []
# Title
st.title("Dual Selection App")
# Create two selectboxes
option1 = st.selectbox(
"Select first option",
options=["Apple", "Banana", "Orange", "Grape"],
key="select1",
index=None,
placeholder="Choose a fruit..."
)
option2 = st.selectbox(
"Select second option",
options=["Red", "Blue", "Green", "Yellow"],
key="select2",
index=None,
placeholder="Choose a color..."
)
# Create a submit button
if st.button("Submit Selections"):
if option1 and option2: # Only proceed if both selections are made
# Add selections to history
st.session_state.history.append((option1, option2))
# Clear the selections
st.session_state.select1 = None
st.session_state.select2 = None
st.rerun()
# Display history
if st.session_state.history:
st.write("### Previous Selections:")
for i, (sel1, sel2) in enumerate(st.session_state.history, 1):
st.write(f"{i}. First selection: {sel1}, Second selection: {sel2}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?