Hi,
I am quite new to streamlit. I am seaching for a wayto change the existing choice / pick of a st.selectbox element by clicking on a button in the sidebar.
Example: st.selectbox has selected βAβ and a chart βAβ is rendered. I want to click on a button βBβ in the side bar which changes the selection to βBβ in the main window and st.selectbox, so that chart βBβ is displayed in the main window.
I just realized that @blackary already had a great example code in the comments while I was creating one.
Hereβs the one that I came up with:
Code
import streamlit as st
import pandas as pd
# Callback function for A and B
def onClick(selection_input):
if selection_input == 'A':
st.session_state['selection'] = 0
if selection_input == 'B':
st.session_state['selection'] = 1
# Initialize the session state
if 'selection' not in st.session_state:
st.session_state['selection'] = 0
# Select box
selected = st.selectbox('Make a selection:', ('A', 'B'), index=st.session_state['selection'])
# Buttons
st.button('A', on_click=onClick, args='A')
st.button('B', on_click=onClick, args='B')
# Conditional display of data visualization
if selected == 'A':
st.subheader('Data visualization for A')
chart_data = pd.DataFrame(
[0.31, 0.46, 0.86, 0.91, 0.96],
columns=['A'])
st.line_chart(chart_data)
if selected == 'B':
st.subheader('Data visualization for B')
chart_data = pd.DataFrame(
[10, 26, 37, 56, 85],
columns=['B'])
st.line_chart(chart_data)