saveCountryOptions()

This commit is contained in:
frankknoll
2022-03-24 10:04:20 +01:00
parent 502a09af98
commit bd2e6603a6
4 changed files with 188 additions and 21 deletions

View File

@@ -1,16 +1,16 @@
<!DOCTYPE html> <!DOCTYPE html>
<meta charset="utf-8">
<html> <html>
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<title>Batch Codes of Coronavirus 2019 Vaccines</title> <title>Batch Codes of Coronavirus 2019 Vaccines</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.css"> <link href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="batchCodeTable.css"> <link href="batchCodeTable.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="forkMeOnGitHub.css"> <link href="forkMeOnGitHub.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" <script charset="utf8" src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"
src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script> type="text/javascript"></script>
<script src="./batchCodeTable.js"></script> <script src="./batchCodeTable.js"></script>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
@@ -32,7 +32,7 @@
<p> <p>
<label>Select country: <label>Select country:
<select id="countrySelect" name="country"> <select id="countrySelect" name="country">
<option value="Global" selected>Global</option> <option selected="" value="Global">Global</option>
<option value="Afghanistan">Afghanistan</option> <option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option> <option value="Albania">Albania</option>
<option value="Algeria">Algeria</option> <option value="Algeria">Algeria</option>
@@ -161,11 +161,11 @@
<option value="Zambia">Zambia</option> <option value="Zambia">Zambia</option>
</select> </select>
</label> </label>
</p>
<h2 class="heading"></h2> <h2 class="heading"></h2>
<p> <p>
<b>Check out your batch code</b> (Last updated: <span id="last_updated">March 18, 2022</span>) <b>Check out your batch code</b> (Last updated: <span id="last_updated">March 18, 2022</span>)
</p> </p>
<p>
<table class="display" id="batchCodeTable"> <table class="display" id="batchCodeTable">
<thead> <thead>
<tr> <tr>
@@ -181,7 +181,6 @@
</tr> </tr>
</thead> </thead>
</table> </table>
</p>
</body> </body>
</html> </html>

View File

@@ -365,6 +365,80 @@
" return self.countryBatchCodeTable[0:0].droplevel(0)\n" " return self.countryBatchCodeTable[0:0].droplevel(0)\n"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "4db36933",
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"class HtmlTransformerUtil:\n",
" \n",
" def applySoupTransformerToFile(self, file, soupTransformer):\n",
" self._writeSoup(soupTransformer(self._readSoup(file)), file)\n",
"\n",
" def _readSoup(self, file):\n",
" with open(file) as fp:\n",
" soup = BeautifulSoup(fp, 'lxml')\n",
" return soup\n",
"\n",
" def _writeSoup(self, soup, file):\n",
" with open(file, \"w\") as fp:\n",
" fp.write(str(soup)) \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32d4eecf",
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"\n",
"class CountryOptionsSetter:\n",
"\n",
" def setCountryOptions(self, html, options):\n",
" soup = self._setCountryOptions(self._parse(html), self._parseOptions(options))\n",
" return str(soup)\n",
"\n",
" def _setCountryOptions(self, soup, options):\n",
" countrySelect = soup.find(id = \"countrySelect\")\n",
" countrySelect.clear()\n",
" for option in options:\n",
" countrySelect.append(option)\n",
" return soup\n",
"\n",
" def _parseOptions(self, options):\n",
" return [self._parse(option).option for option in options]\n",
"\n",
" def _parse(self, html):\n",
" return BeautifulSoup(html, 'lxml')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f0f9b4b",
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"\n",
"\n",
"def saveCountryOptions(countryOptions):\n",
" HtmlTransformerUtil().applySoupTransformerToFile(\n",
" file = \"../docs/batchCodeTable.html\",\n",
" soupTransformer =\n",
" lambda soup:\n",
" BeautifulSoup(\n",
" CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions),\n",
" 'lxml'))\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -684,6 +758,85 @@
" check_dtype = False)\n" " check_dtype = False)\n"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "125351b3",
"metadata": {},
"outputs": [],
"source": [
"class CountryOptionsSetterTest(unittest.TestCase):\n",
"\n",
" def test_setCountryOptions(self):\n",
" # Given\n",
" countryOptionsSetter = CountryOptionsSetter()\n",
"\n",
" # When\n",
" htmlActual = countryOptionsSetter.setCountryOptions(\n",
" html='''\n",
" <html>\n",
" <body>\n",
" <p>Test<p/>\n",
" <select id=\"countrySelect\" name=\"country\">\n",
" <option value=\"Global\" selected>Global</option>\n",
" <option value=\"Afghanistan\">Afghanistan</option>\n",
" <option value=\"Albania\">Albania</option>\n",
" <option value=\"Algeria\">Algeria</option>\n",
" </select>\n",
" </body>\n",
" </html>\n",
" ''',\n",
" options=[\n",
" '<option value=\"Global\" selected>Global</option>',\n",
" '<option value=\"Azerbaijan\">Azerbaijan</option>',\n",
" '<option value=\"Bahrain\">Bahrain</option>'])\n",
"\n",
" # Then\n",
" assertEqualHTML(\n",
" htmlActual,\n",
" '''\n",
" <html>\n",
" <body>\n",
" <p>Test<p/>\n",
" <select id=\"countrySelect\" name=\"country\">\n",
" <option value=\"Global\" selected>Global</option>\n",
" <option value=\"Azerbaijan\">Azerbaijan</option>\n",
" <option value=\"Bahrain\">Bahrain</option>\n",
" </select>\n",
" </body>\n",
" </html>\n",
" ''')\n",
"\n",
"# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings\n",
"def assertEqualHTML(string1, string2, file1='', file2=''):\n",
" u'''\n",
" Compare two unicode strings containing HTML.\n",
" A human friendly diff goes to logging.error() if they\n",
" are not equal, and an exception gets raised.\n",
" '''\n",
" from bs4 import BeautifulSoup as bs\n",
" import difflib\n",
"\n",
" def short(mystr):\n",
" max = 20\n",
" if len(mystr) > max:\n",
" return mystr[:max]\n",
" return mystr\n",
" p = []\n",
" for mystr, file in [(string1, file1), (string2, file2)]:\n",
" if not isinstance(mystr, str):\n",
" raise Exception(u'string ist not unicode: %r %s' %\n",
" (short(mystr), file))\n",
" soup = bs(mystr)\n",
" pretty = soup.prettify()\n",
" p.append(pretty)\n",
" if p[0] != p[1]:\n",
" for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2):\n",
" display(line)\n",
" display(p[0], ' != ', p[1])\n",
" raise Exception('Not equal %s %s' % (file1, file2))\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -766,13 +919,11 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# FK-TODO: analog zu Intensivstationen.ipynb einen KreisOptionsSetter einsetzen zum automatischen Speichern der Options in der html-Datei\n", "def getCountryOptions(countries):\n",
"def printCountryOptions(countries):\n", " return [getCountryOption(country) for country in countries]\n",
" for country in countries:\n",
" printCountryOption(country)\n",
"\n", "\n",
"def printCountryOption(country):\n", "def getCountryOption(country):\n",
" print('<option value=\"{country}\">{country}</option>'.format(country = country))" " return '<option value=\"{country}\">{country}</option>'.format(country = country)"
] ]
}, },
{ {
@@ -782,8 +933,27 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"countries = sorted(internationalVaersCovid19['COUNTRY'].unique())\n", "countries = sorted(internationalVaersCovid19['COUNTRY'].unique())"
"printCountryOptions(countries)" ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c518028",
"metadata": {},
"outputs": [],
"source": [
"countryOptions = ['<option value=\"Global\" selected>Global</option>'] + getCountryOptions(countries)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d6507ca",
"metadata": {},
"outputs": [],
"source": [
"saveCountryOptions(countryOptions)"
] ]
}, },
{ {

View File

@@ -18,7 +18,7 @@ FK-FIXME:
FK-TODO: FK-TODO:
- Darstellung als Dashboard, siehe https://covid-karte.de/ oder https://experience.arcgis.com/experience/3a132983ad3c4ab8a28704e9addefaba - Darstellung als Dashboard, siehe https://covid-karte.de/ oder https://experience.arcgis.com/experience/3a132983ad3c4ab8a28704e9addefaba
- Rot-Gelb-Grün eingefärbte Deutschland-Karte anzeigen wie in https://experience.arcgis.com/experience/3a132983ad3c4ab8a28704e9addefaba - nach den "Median der Anteile freier Betten"-Werten Rot-Gelb-Grün eingefärbte Deutschland-Karte anzeigen wie in https://experience.arcgis.com/experience/3a132983ad3c4ab8a28704e9addefaba
- Alle Charts mit Slidern versehen? - Alle Charts mit Slidern versehen?
anacron job: anacron job:

View File

@@ -432,8 +432,6 @@
" ''')\n", " ''')\n",
"\n", "\n",
"# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings\n", "# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings\n",
"\n",
"\n",
"def assertEqualHTML(string1, string2, file1='', file2=''):\n", "def assertEqualHTML(string1, string2, file1='', file2=''):\n",
" u'''\n", " u'''\n",
" Compare two unicode strings containing HTML.\n", " Compare two unicode strings containing HTML.\n",