Py.Cafe

maartenbreddels/

country-city-selector-solara

Country-City Selector

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import solara

country_city_data = {
    "USA": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
    "Canada": ["Toronto", "Vancouver", "Montreal", "Calgary", "Ottawa"],
    "Australia": ["Sydney", "Melbourne", "Brisbane", "Perth", "Adelaide"],
    "India": ["Mumbai", "Delhi", "Bangalore", "Hyderabad", "Chennai"],
    "UK": ["London", "Manchester", "Birmingham", "Leeds", "Glasgow"]
}

country = solara.reactive("")
city = solara.reactive("")


def set_country(value: str):
    country.value = value
    # reset city when country changes
    city.value = ""


def reset():
    country.value = ""
    city.value = ""


@solara.component
def Page():
    with solara.Card("Select a country and city"):
        solara.Select("Country", value=country.value, values=[""] + list(country_city_data.keys()), on_value=set_country)
        if country.value:
            solara.Select("City", value=city, values=[""] + country_city_data.get(country.value, []))
            with solara.Column():
                solara.Text(f"Selected Country: {country.value}")
                if city.value:
                    solara.Text(f"Selected City: {city.value}")
        solara.Button("Reset", on_click=reset)