Skip to content

Commit 8e64e95

Browse files
committed
🎨 refactor app.py and index.html, add main.py for improved functionality and loading process
1 parent 4297432 commit 8e64e95

4 files changed

Lines changed: 135 additions & 147 deletions

File tree

docs/app.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

docs/index.html

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
88

9-
<!-- Load error_handler.js script before other scripts -->
10-
<script src="js/error_handler.js"></script>
9+
<!-- PyScript runtime (version as requested) -->
10+
<link rel="stylesheet"
11+
href="https://pyscript.net/releases/2025.11.1/core.css" />
12+
<script type="module"
13+
src="https://pyscript.net/releases/2025.11.1/core.js"></script>
14+
15+
<link rel="stylesheet" href="css/table.css" />
16+
<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css" rel="stylesheet" />
17+
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.js"></script>
18+
19+
</head>
1120

12-
<!-- Load required PyScript packages -->
13-
<link rel="stylesheet" href="https://pyscript.net/releases/2025.11.2/core.css">
14-
<script type="module" src="https://pyscript.net/releases/2025.11.2/core.js"></script>
15-
16-
<link rel="stylesheet" href="css/table.css" />
17-
<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css" rel="stylesheet" />
18-
<script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.js"></script>
19-
20-
<!-- Load required Python packages -->
21-
<script type="py" src="./app.py" config="./pyscript.json"></script>
22-
</head>
23-
24-
<body>
21+
<body>
2522
<!-- Load the Python main application file -->
2623
<div class="max-w-md mx-auto">
2724
<div class="space-y-4">
@@ -41,50 +38,20 @@ <h1 class="text-2xl block text-gray-700 font-bold mb-2">
4138
<label for="collapse_file" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Collapse File</label>
4239
<input id="collapse_file" class="bg-gray-50 mb-2 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" value="https://raw.githubusercontent.com/MDU-PHL/pango-collapse/main/pango_collapse/collapse.txt" required>
4340
</div>
44-
<input class="py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="myfile" name="myfile" type="file">
41+
<input style="display: none;" class="py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="myfile" name="myfile" type="file">
4542
</div>
4643
</div>
47-
<ul id="loading" name="loading" style="display:none" class="max-w-md space-y-2 text-gray-500 list-inside dark:text-gray-400">
44+
<ul id="loading" name="loading" style="display:block" class="max-w-md space-y-2 text-gray-500 list-inside dark:text-gray-400">
4845
<li class="flex items-center">
4946
<div role="status">
5047
<svg aria-hidden="true" class="w-5 h-5 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
5148
<span class="sr-only">Loading...</span>
5249
</div>
53-
Collapsing your file...
50+
Loading...
5451
</li>
5552
</ul>
5653
</div>
54+
<!-- Load required Python packages -->
55+
<script type="py" src="./main.py" config="./pyscript.json"></script>
5756
</body>
58-
<script type="text/javascript">
59-
// Quick and simple export target #table_id into a csv
60-
function download_table_as_csv(table_id, separator = ',') {
61-
// Select rows from table_id
62-
var rows = document.querySelectorAll('table#' + table_id + ' tr');
63-
// Construct csv
64-
var csv = [];
65-
for (var i = 0; i < rows.length; i++) {
66-
var row = [], cols = rows[i].querySelectorAll('td, th');
67-
for (var j = 0; j < cols.length; j++) {
68-
// Clean innertext to remove multiple spaces and jumpline (break csv)
69-
var data = cols[j].innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ')
70-
// Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv)
71-
data = data.replace(/"/g, '""');
72-
// Push escaped string
73-
row.push('"' + data + '"');
74-
}
75-
csv.push(row.join(separator));
76-
}
77-
var csv_string = csv.join('\n');
78-
// Download it
79-
var filename = 'pango-collapse' + table_id + '_' + new Date().toLocaleDateString() + '.csv';
80-
var link = document.createElement('a');
81-
link.style.display = 'none';
82-
link.setAttribute('target', '_blank');
83-
link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string));
84-
link.setAttribute('download', filename);
85-
document.body.appendChild(link);
86-
link.click();
87-
document.body.removeChild(link);
88-
}
89-
</script>
90-
</html>
57+
</html>

docs/main.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import micropip
2+
3+
await micropip.install("pango-collapse", deps=False)
4+
5+
from pango_collapse import Collapsor
6+
from pango_collapse.utils import load_potential_parents_from_file
7+
from pyodide.http import open_url
8+
import csv
9+
from pyscript import when, document, window
10+
11+
print("✅ Required packages loaded.")
12+
btn = document.querySelector("#myfile")
13+
btn.style.display ='block'
14+
loading = document.querySelector("#loading")
15+
loading.style.display ='none'
16+
17+
# At this point PyScript has already applied pyscript.json configuration,
18+
btn.disabled = False
19+
20+
def build_table(data, headers):
21+
table = document.createElement('table')
22+
table.id = "table"
23+
header = table.createTHead()
24+
header_row = header.insertRow(0)
25+
for i, col_name in enumerate(headers):
26+
cell = header_row.insertCell(i)
27+
cell.innerHTML = f"<b>{col_name}</b>"
28+
tbody = table.createTBody()
29+
for row_data in data:
30+
row = tbody.insertRow()
31+
for i, col_name in enumerate(headers):
32+
cell = row.insertCell(i)
33+
cell.innerHTML = row_data.get(col_name, "")
34+
document.body.appendChild(table)
35+
36+
37+
def download_table_as_csv(table_id):
38+
table = document.getElementById(table_id)
39+
rows = table.getElementsByTagName('tr')
40+
csv_content = []
41+
for row in rows:
42+
cells = row.getElementsByTagName('td')
43+
if cells.length == 0:
44+
cells = row.getElementsByTagName('th')
45+
row_data = []
46+
for cell in cells:
47+
row_data.append(cell.innerText)
48+
csv_content.append(','.join(row_data))
49+
csv_string = '\n'.join(csv_content)
50+
blob = window.Blob.new([csv_string], { "type": "text/csv" })
51+
url = window.URL.createObjectURL(blob)
52+
a = document.createElement('a')
53+
a.href = url
54+
a.download = 'collapsed_lineages.csv'
55+
document.body.appendChild(a)
56+
a.click()
57+
document.body.removeChild(a)
58+
window.URL.revokeObjectURL(url)
59+
60+
def copy_file_to_local(url):
61+
filename = url.split('/')[-1]
62+
with open(filename, 'w') as file:
63+
file.writelines(open_url((url)).readlines())
64+
65+
def collapse(data, lineage_col, collapse_col, expanded_col='Lineage_expanded'):
66+
67+
copy_file_to_local('https://raw.githubusercontent.com/cov-lineages/pango-designation/master/pango_designation/alias_key.json')
68+
collapsor = Collapsor(alias_file="alias_key.json")
69+
collapse_file_url = document.getElementById("collapse_file").value
70+
copy_file_to_local(collapse_file_url)
71+
potential_parents = load_potential_parents_from_file("collapse.txt")
72+
collapsed_data = []
73+
for row in data:
74+
row[collapse_col] = collapsor.collapse(row[lineage_col], potential_parents)
75+
row[expanded_col] = collapsor.expand(row[lineage_col])
76+
collapsed_data.append(row)
77+
return collapsed_data
78+
79+
80+
@when("change", "#myfile")
81+
async def process_file(event):
82+
print("🚀 process_file called - file input detected!")
83+
table_el = document.getElementById("table")
84+
if table_el:
85+
print("📊 Removing existing table")
86+
table_el.remove()
87+
e = document.getElementById("loading")
88+
e.style.display ='block'
89+
print("⏳ Loading spinner displayed")
90+
91+
lineage_col = document.getElementById("lineage_col").value
92+
collapse_col = 'Lineage_family'
93+
expanded_col = 'Lineage_expanded'
94+
print(f"📝 Configuration - Lineage column: {lineage_col}")
95+
96+
fileList = event.target.files.to_py()
97+
print(f"📁 Number of files selected: {len(fileList)}")
98+
for f in fileList:
99+
print(f"📄 Processing file: {f.name}")
100+
delimiter = ','
101+
if f.name.endswith('.tsv'):
102+
delimiter = '\t'
103+
file = await f.text()
104+
lines = file.split("\n")
105+
data = csv.DictReader(lines, delimiter=delimiter)
106+
collapsed_data = collapse(data, lineage_col, collapse_col, expanded_col=expanded_col)
107+
headers = data.fieldnames
108+
if collapse_col not in headers:
109+
headers += [collapse_col]
110+
if expanded_col not in headers:
111+
headers += [expanded_col]
112+
build_table(collapsed_data, headers=headers)
113+
download_table_as_csv('table')
114+
e.style.display ='none'

docs/pyscript.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"packages": [
33
"numpy",
44
"pandas",
5-
"micropip"
6-
],
7-
"files": {
8-
"./error_handler.py": "./error_handler.py",
9-
"./jhanley_html.py": "./jhanley_html.py"
10-
},
11-
"experimental_create_proxy": "auto"
5+
"micropip",
6+
"pango_aliasor"
7+
]
128
}

0 commit comments

Comments
 (0)