|
| 1 | +"""Marimo demo: discover GovData datasets and read the three M1 example datasets. |
| 2 | +
|
| 3 | +Run with: marimo edit demos/govdata_demo.py (needs network access; install the |
| 4 | +"demo" extra for marimo itself). |
| 5 | +""" |
| 6 | + |
| 7 | +import marimo |
| 8 | + |
| 9 | +__generated_with = "0.14.16" |
| 10 | +app = marimo.App(width="medium") |
| 11 | + |
| 12 | + |
| 13 | +@app.cell |
| 14 | +def _(): |
| 15 | + import marimo as mo |
| 16 | + |
| 17 | + return (mo,) |
| 18 | + |
| 19 | + |
| 20 | +@app.cell |
| 21 | +def _(mo): |
| 22 | + mo.md( |
| 23 | + """ |
| 24 | + # mloda-plugin-govdata demo |
| 25 | +
|
| 26 | + German open government data as mloda features: search GovData via the |
| 27 | + paginated CKAN API, then read the three M1 example datasets (population, |
| 28 | + elections, environment) as typed Arrow tables. Every cell below talks to |
| 29 | + the live endpoints; downloads are cached locally after the first run. |
| 30 | +
|
| 31 | + Part of the Prototype Fund project mloda-plugin-govdata (FKZ 16IS26S11). |
| 32 | + """ |
| 33 | + ) |
| 34 | + return |
| 35 | + |
| 36 | + |
| 37 | +@app.cell |
| 38 | +def _(): |
| 39 | + import pandas as pd |
| 40 | + |
| 41 | + from mloda.user import Feature, mloda |
| 42 | + from mloda_plugin_govdata.feature_groups.govdata import ( |
| 43 | + BundeswahlleiterinReader, |
| 44 | + StuttgartPopulationReader, |
| 45 | + UbaAirReader, |
| 46 | + build_client, |
| 47 | + search_datasets, |
| 48 | + uba_measures_url, |
| 49 | + ) |
| 50 | + |
| 51 | + return ( |
| 52 | + BundeswahlleiterinReader, |
| 53 | + Feature, |
| 54 | + StuttgartPopulationReader, |
| 55 | + UbaAirReader, |
| 56 | + build_client, |
| 57 | + mloda, |
| 58 | + pd, |
| 59 | + search_datasets, |
| 60 | + uba_measures_url, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@app.cell |
| 65 | +def _(mo): |
| 66 | + mo.md("""## 1. Discover datasets (paginated CKAN `package_search`)""") |
| 67 | + return |
| 68 | + |
| 69 | + |
| 70 | +@app.cell |
| 71 | +def _(mo): |
| 72 | + query = mo.ui.text(value="einwohner stuttgart altersgruppen", label="GovData search", full_width=True) |
| 73 | + query |
| 74 | + return (query,) |
| 75 | + |
| 76 | + |
| 77 | +@app.cell |
| 78 | +def _(build_client, pd, query, search_datasets): |
| 79 | + with build_client() as _client: |
| 80 | + _hits = list(search_datasets(_client, query.value, max_results=10)) |
| 81 | + hits = pd.DataFrame({"name": [d.name for d in _hits], "title": [d.title for d in _hits]}) |
| 82 | + hits |
| 83 | + return |
| 84 | + |
| 85 | + |
| 86 | +@app.cell |
| 87 | +def _(mo): |
| 88 | + mo.md("""## 2. Population: Stuttgart residents by age group (GovData CSV)""") |
| 89 | + return |
| 90 | + |
| 91 | + |
| 92 | +@app.cell |
| 93 | +def _(Feature, StuttgartPopulationReader, mloda): |
| 94 | + _slug = "einwohner-nach-altersgruppen-und-stadtbezirken" |
| 95 | + _result = mloda.run_all( |
| 96 | + [ |
| 97 | + Feature("Stichtag", options={StuttgartPopulationReader.__name__: _slug}), |
| 98 | + Feature("Stadtbezirk", options={StuttgartPopulationReader.__name__: _slug}), |
| 99 | + Feature("Alter in 10 Gruppen", options={StuttgartPopulationReader.__name__: _slug}), |
| 100 | + Feature("Einwohner", options={StuttgartPopulationReader.__name__: _slug}), |
| 101 | + ], |
| 102 | + compute_frameworks=["PyArrowTable"], |
| 103 | + ) |
| 104 | + population = _result[0].to_pandas() |
| 105 | + population |
| 106 | + return |
| 107 | + |
| 108 | + |
| 109 | +@app.cell |
| 110 | +def _(mo): |
| 111 | + mo.md("""## 3. Elections: Bundestagswahl 2025 results (`kerg.csv`, merged header)""") |
| 112 | + return |
| 113 | + |
| 114 | + |
| 115 | +@app.cell |
| 116 | +def _(BundeswahlleiterinReader, Feature, mloda): |
| 117 | + _kerg = "https://www.bundeswahlleiterin.de/bundestagswahlen/2025/ergebnisse/opendata/btw25/csv/kerg.csv" |
| 118 | + _result = mloda.run_all( |
| 119 | + [Feature("Gebiet", options={BundeswahlleiterinReader.__name__: _kerg})], |
| 120 | + compute_frameworks=["PyArrowTable"], |
| 121 | + ) |
| 122 | + elections = _result[0].to_pandas() |
| 123 | + elections.head(20) |
| 124 | + return |
| 125 | + |
| 126 | + |
| 127 | +@app.cell |
| 128 | +def _(mo): |
| 129 | + mo.md("""## 4. Environment: hourly ozone at one station (UBA Air Data JSON)""") |
| 130 | + return |
| 131 | + |
| 132 | + |
| 133 | +@app.cell |
| 134 | +def _(Feature, UbaAirReader, mloda, uba_measures_url): |
| 135 | + _url = uba_measures_url(station=143, component=3, scope=2, date_from="2025-01-01", date_to="2025-01-01") |
| 136 | + _result = mloda.run_all( |
| 137 | + [ |
| 138 | + Feature("date_start", options={UbaAirReader.__name__: _url}), |
| 139 | + Feature("value", options={UbaAirReader.__name__: _url}), |
| 140 | + ], |
| 141 | + compute_frameworks=["PyArrowTable"], |
| 142 | + ) |
| 143 | + environment = _result[0].to_pandas() |
| 144 | + environment |
| 145 | + return |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + app.run() |
0 commit comments