updating data of Datawrapper Chart automatically

This commit is contained in:
frankknoll
2022-03-27 22:50:29 +02:00
parent 574e734fb8
commit 47010102cd
5 changed files with 103 additions and 10 deletions

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@ src/tmp/
src/results/ src/results/
docs/data/*.xlsx docs/data/*.xlsx
docs/data/*.html docs/data/*.html
.env

View File

@@ -1 +0,0 @@
theme: jekyll-theme-leap-day

View File

@@ -4,7 +4,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" /> <meta content="width=device-width, initial-scale=1.0" name="viewport" />
<title>Die Lage auf den Intensivstationen in meinem Landkreis von 2020 bis heute</title> <title>Die Lage auf den Intensivstationen von 2020 bis heute</title>
<link href="forkMeOnGitHub.css" rel="stylesheet" type="text/css" /> <link href="forkMeOnGitHub.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/chart.js@^3"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js@^3"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@^2"></script> <script src="https://cdn.jsdelivr.net/npm/moment@^2"></script>
@@ -59,13 +59,14 @@
</head> </head>
<body> <body>
<h1>Die Lage auf den Intensivstationen in meinem Landkreis von 2020 bis heute</h1>
<span id="forkongithub"><a href="https://github.com/KnollFrank/HowBadIsMyBatch">Fork me on GitHub</a></span> <span id="forkongithub"><a href="https://github.com/KnollFrank/HowBadIsMyBatch">Fork me on GitHub</a></span>
<h1>Die Lage auf den Intensivstationen von 2020 bis heute</h1>
<iframe aria-label="Karte" frameborder="0" height="763" id="datawrapper-chart-dYmYb" scrolling="no" <iframe aria-label="Karte" frameborder="0" height="763" id="datawrapper-chart-dYmYb" scrolling="no"
src="https://datawrapper.dwcdn.net/dYmYb/1/" style="width: 0; min-width: 100% !important; border: none;" src="https://datawrapper.dwcdn.net/dYmYb/1/" style="width: 0; min-width: 100% !important; border: none;"
title="Freie Intensivbetten"></iframe> title="Freie Intensivbetten"></iframe>
<script type="text/javascript">!function () { "use strict"; window.addEventListener("message", (function (e) { if (void 0 !== e.data["datawrapper-height"]) { var t = document.querySelectorAll("iframe"); for (var a in e.data["datawrapper-height"]) for (var r = 0; r < t.length; r++) { if (t[r].contentWindow === e.source) t[r].style.height = e.data["datawrapper-height"][a] + "px" } } })) }(); <script type="text/javascript">!function () { "use strict"; window.addEventListener("message", (function (e) { if (void 0 !== e.data["datawrapper-height"]) { var t = document.querySelectorAll("iframe"); for (var a in e.data["datawrapper-height"]) for (var r = 0; r < t.length; r++) { if (t[r].contentWindow === e.source) t[r].style.height = e.data["datawrapper-height"][a] + "px" } } })) }();
</script> </script>
<h1>Die Lage auf den Intensivstationen in meinem Landkreis von 2020 bis heute</h1>
<label>Landkreis:</label> <label>Landkreis:</label>
<select id="kreisSelect" name="kreis"> <select id="kreisSelect" name="kreis">
<option selected="" value="de">Alle Landkreise</option> <option selected="" value="de">Alle Landkreise</option>

View File

@@ -24,7 +24,7 @@
"source": [ "source": [
"from datetime import datetime\n", "from datetime import datetime\n",
"\n", "\n",
"print(datetime.now().strftime(\"%d.%m.%Y, %H:%M:%S Uhr\"))\t" "print(datetime.now().strftime(\"%d.%m.%Y, %H:%M:%S Uhr\"))"
] ]
}, },
{ {
@@ -676,7 +676,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": null,
"id": "f10704f3", "id": "f10704f3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -688,7 +688,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": null,
"id": "396cb4f8", "id": "396cb4f8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -697,8 +697,100 @@
" medianOfFreeBedsByKreisTable = medianOfFreeBedsByKreisTableFactory.createMedianOfFreeBedsByKreisTable('gemeindeschluessel').reset_index()\n", " medianOfFreeBedsByKreisTable = medianOfFreeBedsByKreisTableFactory.createMedianOfFreeBedsByKreisTable('gemeindeschluessel').reset_index()\n",
" return ColumnsAdder(kreise).addKreisAndEinwohnerzahlColumns(medianOfFreeBedsByKreisTable)\n", " return ColumnsAdder(kreise).addKreisAndEinwohnerzahlColumns(medianOfFreeBedsByKreisTable)\n",
" \n", " \n",
"medianOfFreeBedsByKreisTable = createMedianOfFreeBedsByKreisTableForChoroplethMap(medianOfFreeBedsByKreisTableFactory)\n", "medianOfFreeBedsByKreisTable = createMedianOfFreeBedsByKreisTableForChoroplethMap(medianOfFreeBedsByKreisTableFactory)"
"medianOfFreeBedsByKreisTable.to_excel('medianOfFreeBedsByKreisTable.xlsx')" ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4247571",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"\n",
"\n",
"class Datawrapper:\n",
"\n",
" def __init__(self, accessToken):\n",
" self.authHeader = {\"Authorization\": f\"Bearer {accessToken}\"}\n",
"\n",
" def setChartTitle(self, title):\n",
" response = requests.request(\n",
" \"PATCH\",\n",
" \"https://api.datawrapper.de/v3/charts/dYmYb\",\n",
" json={\"title\": title},\n",
" headers={\n",
" \"Accept\": \"*/*\",\n",
" \"Content-Type\": \"application/json\"\n",
" } | self.authHeader)\n",
" return json.loads(response.text)\n",
"\n",
" def uploadChartData(self, data: pd.DataFrame):\n",
" response = requests.request(\n",
" \"PUT\",\n",
" \"https://api.datawrapper.de/v3/charts/dYmYb/data\",\n",
" data=data.to_csv(\n",
" index=False,\n",
" columns=['gemeindeschluessel', 'median_free_beds_in_percent', 'Kreis']).encode(\"utf-8\"),\n",
" headers={\n",
" \"Accept\": \"*/*\",\n",
" \"Content-Type\": \"text/csv\"\n",
" } | self.authHeader)\n",
" return response.text\n",
"\n",
" def fetchChartData(self):\n",
" response = requests.request(\n",
" \"GET\",\n",
" \"https://api.datawrapper.de/v3/charts/dYmYb/data\",\n",
" headers={\n",
" \"Accept\": \"text/csv\"\n",
" } | self.authHeader)\n",
"\n",
" return response.text\n",
"\n",
" def publishChart(self):\n",
" response = requests.request(\n",
" \"POST\",\n",
" \"https://api.datawrapper.de/v3/charts/dYmYb/publish\",\n",
" headers={\n",
" \"Accept\": \"*/*\"\n",
" } | self.authHeader)\n",
" return json.loads(response.text)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1b75357",
"metadata": {},
"outputs": [],
"source": [
"def getChartTitle(dateStart, dateEnd):\n",
" def formatDate(date):\n",
" return date.strftime(\"%d.%m.%Y\")\n",
"\n",
" return f\"Median freier Intensivbetten im Zeitraum {formatDate(dateStart)} bis {formatDate(dateEnd)}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9de2f34e",
"metadata": {},
"outputs": [],
"source": [
"from decouple import AutoConfig\n",
"\n",
"config = AutoConfig(search_path='../..')\n",
"dataWrapper = Datawrapper(config('DATAWRAPPER_API_TOKEN'))\n",
"dataWrapper.setChartTitle(\n",
" getChartTitle(\n",
" dateStart=timeSeries['date'].min(),\n",
" dateEnd=timeSeries['date'].max()))\n",
"dataWrapper.uploadChartData(medianOfFreeBedsByKreisTable)\n",
"dataWrapper.publishChart()\n"
] ]
}, },
{ {
@@ -708,7 +800,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def publish():\n", "def publishGitHubPages():\n",
" %cd /home/frankknoll/Dokumente/Corona/projects/HowBadIsMyBatch-pages\n", " %cd /home/frankknoll/Dokumente/Corona/projects/HowBadIsMyBatch-pages\n",
" ! git add -A\n", " ! git add -A\n",
" ! git commit -m \"updating data\"\n", " ! git commit -m \"updating data\"\n",
@@ -722,7 +814,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"publish()" "publishGitHubPages()"
] ]
} }
], ],