-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathelectric_vehicles.py
226 lines (183 loc) · 4.79 KB
/
electric_vehicles.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "altair==5.4.1",
# "duckdb==1.1.3",
# "marimo",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# ]
# ///
import marimo
__generated_with = "0.8.19"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
# Electric Vehicle Population Data
> This dataset shows the Battery Electric Vehicles (BEVs) and Plug-in Hybrid Electric Vehicles (PHEVs) that are currently registered through Washington State Department of Licensing (DOL).
"""
)
return
@app.cell
def __(evs, mo):
evs = mo.sql(
f"""
create or replace table evs as
from 'https://datasets.marimo.app/gov/Electric_Vehicle_Population_Data.csv';
select * from evs
"""
)
return (evs,)
@app.cell
def __(mo, years):
all_years = years["Model Year"]
year_select = mo.ui.multiselect.from_series(years["Model Year"])
return all_years, year_select
@app.cell
def __(cities, mo):
all_cities = cities["City"]
city_select = mo.ui.multiselect.from_series(cities["City"])
return all_cities, city_select
@app.cell
def __(makes, mo):
all_makes = makes["Make"]
make_select = mo.ui.multiselect.from_series(makes["Make"])
return all_makes, make_select
@app.cell
def __(city_select, make_select, mo, year_select):
mo.hstack([year_select, city_select, make_select], justify="space-between")
return
@app.cell
def __(alt, grouped_by_city, mo):
_chart = (
alt.Chart(grouped_by_city)
.mark_bar()
.encode(
y=alt.Y("City", type="nominal", sort="-x"),
x=alt.X("sum(count)", type="quantitative"),
color=alt.Color("Model Year", type="nominal"),
)
.properties(title="Top 10 City", width="container")
)
chart1 = mo.ui.altair_chart(_chart, chart_selection=False)
return (chart1,)
@app.cell
def __(alt, grouped_by_make, mo):
_chart = (
alt.Chart(grouped_by_make)
.mark_bar()
.encode(
y=alt.Y("Make", type="nominal", sort="-x"),
x=alt.X("sum(count)", type="quantitative"),
color=alt.Color("Model Year", type="nominal"),
)
.properties(title="Top 10 Make", width="container")
)
chart2 = mo.ui.altair_chart(_chart, chart_selection=False)
return (chart2,)
@app.cell
def __(chart1, chart2, mo):
mo.hstack([chart1, chart2], widths="equal")
return
@app.cell
def __(mo):
mo.md(r"""## Appendix""")
return
@app.cell
def __(evs, mo):
years = mo.sql(
f"""
SELECT DISTINCT CAST(evs."Model Year" AS VARCHAR) AS "Model Year" FROM evs;
"""
)
return (years,)
@app.cell
def __(evs, mo):
cities = mo.sql(
f"""
SELECT DISTINCT CAST(evs."City" AS VARCHAR) AS "City" FROM evs WHERE "City" != 'null';
"""
)
return (cities,)
@app.cell
def __(evs, mo):
makes = mo.sql(
f"""
SELECT DISTINCT CAST(evs."Make" AS VARCHAR) AS "Make" FROM evs;
"""
)
return (makes,)
@app.cell
def __(
cast_to_ints,
city_select,
evs,
make_select,
mo,
sql_list,
year_select,
):
grouped_by_city = mo.sql(
f"""
SELECT COUNT(*) AS "count", "City", "Model Year"
FROM evs
WHERE
{sql_list("Model Year", cast_to_ints(year_select.value))}
AND
{sql_list("Make", make_select.value)}
AND
{sql_list("City", city_select.value)}
GROUP BY "City", "Model Year"
HAVING COUNT(*) > 1
ORDER BY "count" DESC
"""
)
return (grouped_by_city,)
@app.cell
def __(
cast_to_ints,
city_select,
evs,
make_select,
mo,
sql_list,
year_select,
):
grouped_by_make = mo.sql(
f"""
SELECT COUNT(*) AS "count", "Make", "Model Year"
FROM evs
WHERE
{sql_list("Model Year", cast_to_ints(year_select.value))}
AND
{sql_list("Make", make_select.value)}
AND
{sql_list("City", city_select.value)}
GROUP BY "Make", "Model Year"
HAVING COUNT(*) > 1
ORDER BY "count" DESC
"""
)
return (grouped_by_make,)
@app.cell
def __():
def sql_list(column, items):
if not items:
return "True == True"
literals = [as_literal(i) for i in items]
return f"\"{column}\" IN ({','.join(literals)})"
def as_literal(v):
return f"'{v}'" if isinstance(v, str) else str(v)
def cast_to_ints(items):
return [int(i) for i in items]
return as_literal, cast_to_ints, sql_list
@app.cell
def __():
# Imports
import marimo as mo
import altair as alt
return alt, mo
if __name__ == "__main__":
app.run()