I'm working on a dashboard using Streamlit in python. I have a form to make some filters, and this form has some radion buttons with different conditions for each one.
The conditions are working perfectly outside the form, however, anything happens inside the form. Below, is a sample of the code.
import streamlit as st
import pandas as pd
df = pd.DataFrame({'country': ['Burundi', 'Cameroon', 'Central African Republic', 'Chad',
'Republic of Congo', 'Democratic Republic of Congo',
'Equatorial Guinea', 'Gabon', 'São Tomé and Principe', 'Comoros',
'Djibouti', 'Eritrea', 'Ethiopia', 'Kenya', 'Madagascar',
'Mauritius', 'Rwanda', 'Seychelles', 'Somalia', 'Sudan', 'South Sudan',
'Tanzania', 'Uganda']})
countries_regions = {'Central Africa': {'Burundi', 'Cameroon', 'Central African Republic', 'Chad',
'Republic of Congo', 'Democratic Republic of Congo',
'Equatorial Guinea', 'Gabon', 'São Tomé and Principe'},
'Eastern Africa': {'Comoros', 'Djibouti', 'Eritrea', 'Ethiopia', 'Kenya', 'Madagascar',
'Mauritius', 'Rwanda', 'Seychelles', 'Somalia', 'Sudan', 'South Sudan',
'Tanzania', 'Uganda'}}
form = st.sidebar.form("Filters_form", clear_on_submit=True)
countries = df['country'].unique()
selection = form.radio("Select Countries to show", ("Show all countries", "Select region",
"Select one or more countries"))
if selection == "Show all countries":
countries_selected = countries
elif selection == "Select region":
aux_countries = []
countries_selected = form.multiselect('What region do you want to analyze?', countries_regions.keys(),
default='Eastern Africa')
for key in countries_selected:
aux_countries.extend(countries_regions[key])
countries_selected = aux_countries
else:
countries_selected = form.multiselect('What countries do you want to analyze?', countries)