-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathsidebar.py
128 lines (107 loc) · 2.79 KB
/
sidebar.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
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "altair==5.4.1",
# "marimo",
# "vega-datasets==0.9.0",
# ]
# ///
import marimo
__generated_with = "0.8.20"
app = marimo.App(width="full")
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
!!! tip "This notebook is best viewed as an app."
Hit `Cmd/Ctrl+.` or click the "app view" button in the bottom right.
"""
)
return
@app.cell
def __():
import marimo as mo
import altair as alt
from vega_datasets import data
return alt, data, mo
@app.cell
def __(data):
gapminder = data.gapminder()
return (gapminder,)
@app.cell
def __(gapminder, mo):
# Filters
year = mo.ui.slider.from_series(
gapminder["year"], full_width=True, label="Year", step=5
)
population = mo.ui.range_slider.from_series(
gapminder["pop"], full_width=True, label="Population"
)
return population, year
@app.cell
def __(gapminder, population, year):
# Filter the dataset
filtered_data = gapminder[
(gapminder["year"] == year.value)
& (gapminder["pop"] > population.value[0])
& (gapminder["pop"] < population.value[1])
]
return (filtered_data,)
@app.cell
def __(alt, filtered_data, mo):
chart = mo.ui.altair_chart(
alt.Chart(filtered_data)
.mark_circle(opacity=0.7)
.encode(
x="fertility:Q",
y="life_expect:Q",
color="cluster:N",
size=alt.Size("pop:Q", scale=alt.Scale(range=[100, 2000])),
tooltip=[
"country:N",
"year:O",
"fertility:Q",
"life_expect:Q",
"pop:Q",
],
)
.properties(height=400)
)
chart
return (chart,)
@app.cell
def __(chart):
# Show the chart selection
chart.value if not chart.value.empty else None
return
@app.cell
def __(mo, population, year):
mo.sidebar(
[
mo.md("# Gap Minder"),
mo.md(
"Scrub the year to see life expectancy go up and fertility go down"
),
mo.vstack(
[
year,
mo.md(f"{int(year.value)}"),
population,
mo.md(
f"{int(population.value[0]):,} - {int(population.value[1]):,}"
),
]
).style(padding="20px 10px"),
],
footer=[
mo.md(
f"""
[{mo.icon("lucide:twitter")} Twitter](https://twitter.com/marimo_io)
[{mo.icon("lucide:github")} GitHub](https://github.com/marimo-team/marimo)
"""
)
],
)
return
if __name__ == "__main__":
app.run()