starting to add select2 box for batch codes

This commit is contained in:
frankknoll
2023-04-12 17:17:32 +02:00
parent 2e70164ac6
commit 6c1f43e32b
819 changed files with 50664 additions and 750 deletions

View File

@@ -1,14 +1,27 @@
from bs4 import BeautifulSoup
from HtmlTransformerUtil import HtmlTransformerUtil
from DateProvider import DateProvider
from BatchcodeOptionsSetter import BatchcodeOptionsSetter
from HtmlUtils import getBatchcodeOptions, getBatchcodes
from DateProvider import DateProvider
def updateBatchCodeTableHtmlFile(batchCodeTableHtmlFile):
def updateBatchCodeTableHtmlFile(batchCodeTable, batchCodeTableHtmlFile):
batchcodeOptions = getBatchcodeOptions(getBatchcodes(batchCodeTable.sort_values(by = 'Adverse Reaction Reports', ascending = False)))
_saveBatchcodeOptions(batchcodeOptions, batchCodeTableHtmlFile)
_saveLastUpdatedBatchCodeTable(
DateProvider().getLastUpdatedDataSource(),
batchCodeTableHtmlFile)
def _saveBatchcodeOptions(batchcodeOptions, batchCodeTableHtmlFile):
HtmlTransformerUtil().applySoupTransformerToFile(
file=batchCodeTableHtmlFile,
soupTransformer=lambda soup:
BeautifulSoup(
BatchcodeOptionsSetter().setBatchcodeOptions(
html=str(soup),
options=batchcodeOptions),
'lxml'))
def _saveLastUpdatedBatchCodeTable(lastUpdated, batchCodeTableHtmlFile):
def setLastUpdated(soup):
soup.find(id="last_updated").string.replace_with(

View File

@@ -23,3 +23,4 @@ def createAndSaveGlobalBatchCodeTable(minADRsForLethality, batchCodeTableFactory
'Lethality'
]]
IOUtils.saveDataFrameAsJson(batchCodeTable, '../docs/data/batchCodeTables/Global.json')
return batchCodeTable

View File

@@ -0,0 +1,21 @@
from bs4 import BeautifulSoup
class BatchcodeOptionsSetter:
def setBatchcodeOptions(self, html, options):
soup = self._setBatchcodeOptions(self._parse(html), self._parseOptions(options))
return str(soup)
def _setBatchcodeOptions(self, soup, options):
batchcodeSelect = soup.find(id = "batchCodeSelect")
batchcodeSelect.clear()
for option in options:
batchcodeSelect.append(option)
return soup
def _parseOptions(self, options):
return [self._parse(option).option for option in options]
def _parse(self, html):
return BeautifulSoup(html, 'lxml')

View File

@@ -0,0 +1,71 @@
import unittest
from BatchcodeOptionsSetter import BatchcodeOptionsSetter
class BatchcodeOptionsSetterTest(unittest.TestCase):
def test_setBatchcodeOptions(self):
# Given
batchcodeOptionsSetter = BatchcodeOptionsSetter()
# When
htmlActual = batchcodeOptionsSetter.setBatchcodeOptions(
html='''
<html>
<body>
<p>Test<p/>
<select id="batchCodeSelect" name="batchCode">
<option value="old1">old1</option>
<option value="old2">old2</option>
</select>
</body>
</html>
''',
options=[
'<option value="FE6208">FE6208</option>',
'<option value="039K20A">039K20A</option>',
'<option value="FF3318">FF3318</option>'])
# Then
assertEqualHTML(
htmlActual,
'''
<html>
<body>
<p>Test<p/>
<select id="batchCodeSelect" name="batchCode">
<option value="FE6208">FE6208</option>
<option value="039K20A">039K20A</option>
<option value="FF3318">FF3318</option>
</select>
</body>
</html>
''')
# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings
def assertEqualHTML(string1, string2, file1='', file2=''):
u'''
Compare two unicode strings containing HTML.
A human friendly diff goes to logging.error() if they
are not equal, and an exception gets raised.
'''
from bs4 import BeautifulSoup as bs
import difflib
def short(mystr):
max = 20
if len(mystr) > max:
return mystr[:max]
return mystr
p = []
for mystr, file in [(string1, file1), (string2, file2)]:
if not isinstance(mystr, str):
raise Exception(u'string ist not unicode: %r %s' %
(short(mystr), file))
soup = bs(mystr, 'lxml')
pretty = soup.prettify()
p.append(pretty)
if p[0] != p[1]:
for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2):
print(line)
print(p[0], ' != ', p[1])
raise Exception('Not equal %s %s' % (file1, file2))

View File

@@ -136,23 +136,24 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8d6507ca",
"id": "0915aa5a",
"metadata": {},
"outputs": [],
"source": [
"updateBatchCodeTableHtmlFile(batchCodeTableHtmlFile=\"../docs/batchCodeTable.html\")"
"batchCodeTable = createAndSaveGlobalBatchCodeTable(\n",
" minADRsForLethality = 100,\n",
" batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19))\n",
"batchCodeTable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0915aa5a",
"id": "89d57e0f",
"metadata": {},
"outputs": [],
"source": [
"createAndSaveGlobalBatchCodeTable(\n",
" minADRsForLethality = 100,\n",
" batchCodeTableFactory = BatchCodeTableFactory(internationalVaersCovid19))"
"updateBatchCodeTableHtmlFile(batchCodeTable, batchCodeTableHtmlFile=\"../docs/batchCodeTable.html\")"
]
},
{

10
src/HtmlUtils.py Normal file
View File

@@ -0,0 +1,10 @@
def getBatchcodes(batchCodeTable):
return batchCodeTable['Batch'].dropna().unique()
def getBatchcodeOptions(batchcodes):
return [_getBatchcodeOption(batchcode) for batchcode in batchcodes]
def _getBatchcodeOption(batchcode):
return '<option value="{batchcode}">{batchcode}</option>'.format(batchcode=batchcode)