Py.Cafe

maartenbreddels/

gapminder-explore-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import solara
import solara.lab
import pandas as pd
import plotly.express as px
import sys


print(sys.platform)


df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
print(df)
continents = df["continent"].unique().tolist()
print(continents)


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


# the filtered dataframe gets re-executed when the depending reactive variables
# change
@solara.lab.computed
def df_filtered():
    dff = df
    # we could possibly skip continent if country is set
    if continent.value:
        print("filter on", continent.value)
        dff = dff[dff["continent"] == continent.value]
    if country.value:
        dff = dff[dff["country"] == country.value]
    return dff


def set_continent(value: str):
    continent.value = value
    # possible reset country when continent changes
    if value:  # not empty
        countries = df[df["continent"] == value]["country"].unique().tolist()
        if country.value not in countries:
            country.value = ""


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


@solara.component
def Common():
    solara.Title("Explore Gapminder")
    with solara.AppBar():
        solara.lab.ThemeToggle()
    with solara.Sidebar():
        solara.Select("Continent", value=continent.value, values=[""] + continents, on_value=set_continent)
        if continent.value:
            countries = df[df["continent"] == continent.value]["country"].unique().tolist()
        else:
            countries = df["country"].unique().tolist()
        solara.Select("Country", value=country, values=[""] + countries)
        solara.Button("Reset", on_click=reset)


@solara.component
def Home():
    print("render home")
    # route, routes = solara.use_route(level=-1)
    # print("route", route, routes)
    # Common()
    solara.DataFrame(df_filtered.value)


@solara.component
def Visuals():
    print("render visuals")
    # not sure why this not works right now
    # dark_effective = solara.lab.use_dark_efective()
    dark_effective = False
    Common()
    print("df_filtered.value", df_filtered.value)
    solara.FigurePlotly(
        px.scatter(
            df_filtered.value,
            "gdpPercap",
            "lifeExp",
            size="pop",
            color="continent" if not continent.value else "country",
            size_max=40,
            log_x=True,
            log_y=True,
            template="plotly_dark" if dark_effective else "plotly_white",
        )
    )


@solara.component
def Details():
    solara.Text("Choose a report")
    SubNavigation()


@solara.component
def SubNavigation():
    # custom navigation
    current_route, routes = solara.use_route()
    with solara.Sidebar():
        with solara.Column():
            for route in routes:
                with solara.Link(route, style={"width": "100%", "display": "flex", "flex-direction": "column"}):
                    prefix = "-" if route.path == current_route.path else " "
                    name = "overview" if route.path == "/" else route.path
                    solara.Button(name, style={"font-weight": "800" if route.path == current_route.path else "100"}, text=True)


@solara.component
def Foo():
    SubNavigation()
    solara.Markdown("# Foo report")


@solara.component
def Bar():
    SubNavigation()
    solara.Markdown("# Bar report")


routes = [
    solara.Route("/", component=Home),
    solara.Route("visuals", component=Visuals),
    solara.Route(
        "details",
        children=[
            solara.Route("/", component=Details),
            solara.Route("foo", component=Foo),
            solara.Route("bar", component=Bar),
        ],
    ),
]