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),
],
),
]