0

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)
Joicy Xavier
  • 13
  • 1
  • 3

1 Answers1

1

Your code is incomplete. Add the following at the end.

form.form_submit_button()
Output

enter image description here

ferdy
  • 4,396
  • 2
  • 4
  • 16
  • Hi @ferdy. Yeah, I forgot the button on the example code, but this doesn't solve the problem. I don't want to show the options only when submitting but when clicking on the radio button. – Joicy Xavier Apr 13 '22 at 09:04
  • Anything inside the form will not be executed unless you press the form submit button. So what you want is not possible. – ferdy Apr 13 '22 at 09:57