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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.