-
Notifications
You must be signed in to change notification settings - Fork 177
Datawrapper loader example #1585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
1a915a1
51b4209
f4318ac
269bd86
30919d4
0903ddd
e98f242
c2c0bf8
4257f14
d8d0c17
0754b1c
cda1154
d756ada
6e6ce4b
04ab679
32a1691
56a06ff
05d8ddd
7c2b1e3
aa01fed
dfb3b5d
c501f96
2d559bb
5c44d33
34f6f2f
0c9d940
c8c56b3
20a75ab
23ea365
b07b608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| *.pyc | ||
| .DS_Store | ||
| .venv | ||
| /dist/ | ||
| node_modules/ | ||
| yarn-error.log |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [Framework examples →](../) | ||
|
|
||
| # Datawrapper data loader | ||
|
|
||
| View live: <https://observablehq.observablehq.cloud/framework-example-loader-datawrapper/> | ||
|
|
||
| This Observable Framework example demonstrates how to write a data loader in Python that generates an HTML [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) tag that embeds a [Datawrapper](https://www.datawrapper.de/) chart. | ||
|
|
||
| The data loader lives in [`src/data/chart.html.py`](./src/data/chart.html.py). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default { | ||
| root: "src" | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "type": "module", | ||
| "private": true, | ||
| "scripts": { | ||
| "clean": "rimraf src/.observablehq/cache", | ||
| "build": "rimraf dist && observable build", | ||
| "dev": "observable preview", | ||
| "deploy": "observable deploy", | ||
| "observable": "observable" | ||
| }, | ||
| "dependencies": { | ||
| "@observablehq/framework": "^1.7.0" | ||
| }, | ||
| "devDependencies": { | ||
| "rimraf": "^5.0.5" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pandas | ||
| datawrapper | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /.observablehq/cache/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import sys | ||
|
|
||
| import pandas as pd | ||
| from datawrapper import Datawrapper | ||
|
|
||
| # Get an extract of arrests made by the Baltimore Police Department | ||
| df = pd.read_csv( | ||
| "https://raw.githubusercontent.com/palewire/first-automated-chart/main/_notebooks/arrests.csv", | ||
| parse_dates=["ArrestDateTime"] | ||
| ) | ||
|
|
||
| # Create a year column | ||
| df['year'] = df.ArrestDateTime.dt.year | ||
|
|
||
| # Calculate the total number by year | ||
| totals_by_year = df.year.value_counts().sort_index().reset_index() | ||
|
|
||
| # Connect to the datawrapper api | ||
| dw = Datawrapper() | ||
|
|
||
| # Create a chart | ||
| chart_config = dw.create_chart( | ||
| # Headline the chart | ||
| title="Baltimore Arrests", | ||
| # Set the type | ||
| chart_type="column-chart", | ||
| # Give the data | ||
| data=totals_by_year, | ||
| # Configure other bits | ||
| metadata={ | ||
| # Set the description | ||
| "describe": { | ||
| "source-name": "OpenBaltimore", | ||
| "source-url": "https://data.baltimorecity.gov/datasets/baltimore::bpd-arrests/about", | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| # Pull out the chart's unique identifier | ||
| chart_id = chart_config["id"] | ||
|
|
||
| # Publish the chart | ||
| dw.publish_chart(chart_id) | ||
|
|
||
| # Write the chart's embed code to stdout | ||
| html = dw.get_iframe_code(chart_id, responsive=True) | ||
| sys.stdout.write(html) | ||
palewire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Datawrapper data loader | ||
|
|
||
| Here’s a Python data loader that generates an HTML [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) tag that embeds a [Datawrapper](https://www.datawrapper.de/) chart. | ||
|
|
||
| ```python | ||
| import sys | ||
|
|
||
| import pandas as pd | ||
| from datawrapper import Datawrapper | ||
|
|
||
| # Get an extract of arrests made by the Baltimore Police Department | ||
| df = pd.read_csv( | ||
| "https://raw.githubusercontent.com/palewire/first-automated-chart/main/_notebooks/arrests.csv", | ||
| parse_dates=["ArrestDateTime"] | ||
| ) | ||
|
|
||
| # Create a year column | ||
| df['year'] = df.ArrestDateTime.dt.year | ||
|
|
||
| # Calculate the total number by year | ||
| totals_by_year = df.year.value_counts().sort_index().reset_index() | ||
|
|
||
| # Connect to the datawrapper api | ||
| dw = Datawrapper() | ||
|
|
||
| # Create a chart | ||
| chart_config = dw.create_chart( | ||
| # Headline the chart | ||
| title="Baltimore Arrests", | ||
| # Set the type | ||
| chart_type="column-chart", | ||
| # Give the data | ||
| data=totals_by_year, | ||
| # Configure other bits | ||
| metadata={ | ||
| # Set the description | ||
| "describe": { | ||
| "source-name": "OpenBaltimore", | ||
| "source-url": "https://data.baltimorecity.gov/datasets/baltimore::bpd-arrests/about", | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| # Pull out the chart's unique identifier | ||
| chart_id = chart_config["id"] | ||
|
|
||
| # Publish the chart | ||
| dw.publish_chart(chart_id) | ||
|
|
||
| # Write the chart's embed code to stdout | ||
| html = dw.get_iframe_code(chart_id, responsive=True) | ||
| sys.stdout.write(html) | ||
| ``` | ||
|
|
||
| <div class="note"> | ||
|
|
||
| To run this data loader, you will need to create an API token in Datawrapper and set it as an environment variable named `DATAWRAPPER_ACCESS_TOKEN`. | ||
|
||
|
|
||
| You’ll also need python3 and the pandas and datawrapper modules installed and available on your `$PATH`. | ||
palewire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| </div> | ||
|
|
||
| <div class="tip"> | ||
|
|
||
| We recommend using a [Python virtual environment](https://observablehq.com/framework/loaders#venv), such as with venv or uv, and managing required packages via `requirements.txt` rather than installing them globally. | ||
|
|
||
| </div> | ||
|
|
||
| The above data loader lives in `data/chart.html.py`, so we can load the data using `data/chart.html` with `FileAttachment`: | ||
palewire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js echo | ||
| const iframe = FileAttachment("data/chart.html").text(); | ||
| ``` | ||
|
|
||
| We can then insert the HTML into the page by creating an element and setting its `innerHTML` attribute: | ||
|
|
||
| ```html echo | ||
| <div id="chart"></div> | ||
| ``` | ||
|
|
||
| ```js echo | ||
| document.getElementById("chart").innerHTML = iframe; | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.