I am trying to do charts with multiselect options using Streamlit app. Actually, I only achieve to do a chart with a unique selectbox (no multiple choices). This is the code below that works with a unique selection: df = pd.DataFrame(px.data.gapminder()) def plot(): clist = data['country'].unique() country = st.selectbox('Select country', clist) st.header('You selected:', country) fig = px.line(df[df['country'] == country], x = "year", y = "gdpPercap", title = country) st.plotly_chart(fig) But when I replace st.selectbox by st.multiselect the plot does not work.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import plotly.express as px
df = pd.DataFrame(px.data.gapminder())
def plot():
clist = df['country'].unique()
countries = st.multiselect('Select countries', clist)
if countries:
st.header('You selected:', ', '.join(countries))
filtered_data = df[df['country'].isin(countries)]
fig = px.line(filtered_data, x='year', y='gdpPercap', color='country', title='GDP per Capita over Time')
st.plotly_chart(fig)
else:
st.write('Please select at least one country.')
if __name__ == '__main__':
plot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?