diff --git a/src/intensivstationen/Datawrapper.py b/src/intensivstationen/Datawrapper.py new file mode 100644 index 00000000000..021d1ace6a6 --- /dev/null +++ b/src/intensivstationen/Datawrapper.py @@ -0,0 +1,52 @@ +import requests +import json +import pandas as pd + + +class Datawrapper: + + def __init__(self, accessToken): + self.authHeader = {"Authorization": f"Bearer {accessToken}"} + + def setChartTitle(self, title): + response = requests.request( + "PATCH", + "https://api.datawrapper.de/v3/charts/dYmYb", + json={"title": title}, + headers={ + "Accept": "*/*", + "Content-Type": "application/json" + } | self.authHeader) + return json.loads(response.text) + + def uploadChartData(self, data: pd.DataFrame): + response = requests.request( + "PUT", + "https://api.datawrapper.de/v3/charts/dYmYb/data", + data=data.to_csv( + index=False, + columns=['gemeindeschluessel', 'median_free_beds_in_percent', 'Kreis']).encode("utf-8"), + headers={ + "Accept": "*/*", + "Content-Type": "text/csv" + } | self.authHeader) + return response.text + + def fetchChartData(self): + response = requests.request( + "GET", + "https://api.datawrapper.de/v3/charts/dYmYb/data", + headers={ + "Accept": "text/csv" + } | self.authHeader) + + return response.text + + def publishChart(self): + response = requests.request( + "POST", + "https://api.datawrapper.de/v3/charts/dYmYb/publish", + headers={ + "Accept": "*/*" + } | self.authHeader) + return json.loads(response.text) diff --git a/src/intensivstationen/Intensivstationen.ipynb b/src/intensivstationen/Intensivstationen.ipynb index 66036d61eee..3c4c2a932ba 100644 --- a/src/intensivstationen/Intensivstationen.ipynb +++ b/src/intensivstationen/Intensivstationen.ipynb @@ -31,6 +31,7 @@ "from KreisOptionsSetter import KreisOptionsSetter\n", "from TestHelper import TestHelper\n", "from IOUtils import IOUtils\n", + "from Datawrapper import Datawrapper\n", "\n", "pd.set_option('display.max_rows', 100)\n", "pd.set_option('display.max_columns', None)\n", @@ -434,66 +435,6 @@ "medianOfFreeBedsByKreisTable = createMedianOfFreeBedsByKreisTableForChoroplethMap(medianOfFreeBedsByKreisTableFactory)" ] }, - { - "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,