forked from michaelfenner/michaelfenner.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (57 loc) · 2.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Demystify-lite Pyscript front-end.
The form for Demystify is cleared and a user can then provide the
page with a File handle to a file-format analysis report on their own
file-system. The file is processed and the results returned to the page.
"""
import tempfile
from js import document
from pyodide.ffi import create_proxy, to_js
from demystify.demystify import analysis_from_csv_lite
from demystify.libs.outputhandlers.htmloutputclass import FormatAnalysisHTMLOutput
def clear_data():
"""Clear the metadata fields associated with the file input and
output.
"""
document.getElementById("filename").innerHTML = ""
document.getElementById("filesize").innerHTML = ""
document.getElementById("filetype").innerHTML = ""
document.getElementById("filedate").innerHTML = ""
document.getElementById("results").innerHTML = ""
async def file_select(event):
"""Handle file select and follow-on actions from HTML/Pyscript."""
clear_data()
event.stopPropagation()
event.preventDefault()
files = event.target.files
for file in files:
document.getElementById("filename").innerHTML = f"<b>File Name:</b> {file.name}"
document.getElementById("filesize").innerHTML = f"<b>File Size:</b> {file.size}"
if file.type:
document.getElementById(
"filetype"
).innerHTML = f"<b>File Type:</b> {file.type}"
document.getElementById(
"filedate"
).innerHTML = f"<b>File date:</b> {file.lastModified}"
content = await file.text()
with tempfile.NamedTemporaryFile("w", encoding="UTF8") as temp_file:
temp_file.write(content)
analysis = analysis_from_csv_lite(temp_file.name, analyze=True)
try:
out = FormatAnalysisHTMLOutput(
analysis.analysis_results
).printHTMLResults()
except AttributeError:
# TODO: Consider a more idiomatic approach. We'll supply a
# string to the function if analysis_results do not exist.
out = f"<b>{analysis}</b> Press F12 on your keyboard to open developer tools, then select the console tab to view additional debug output."
document.getElementById("results").innerHTML = out
def setup_button():
"""Create a Python proxy for the callback function."""
file_select_proxy = create_proxy(file_select)
document.querySelector("#file_select input[type='file']").addEventListener(
"change", file_select_proxy, False
)
# TODO: Add a second button to run the tests
# Add a third button to use custom configs
setup_button()