forked from ross-spencer/demystify-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demystify-lite.py
79 lines (60 loc) · 2.61 KB
/
demystify-lite.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
77
78
79
"""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 Object, document, window
from pyodide import create_proxy, to_js
from demystify.demystify import analysis_from_csv, handle_output
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()
try:
options = {"multiple": False, "startIn": "documents"}
fileHandles = await window.showOpenFilePicker(
Object.fromEntries(to_js(options))
)
except Exception as err:
console.log(f"Exception: {err}")
return
for fileHandle in fileHandles:
file = await fileHandle.getFile()
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.lastModifiedDate}"
content = await file.text()
a = tempfile.NamedTemporaryFile("w", encoding="UTF8")
a.write(content)
analysis = analysis_from_csv(a.name, True, label=file.name)
try:
out = handle_output(analysis.analysis_results)
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.getElementById("file_select").addEventListener(
"click", file_select_proxy, False
)
setup_button()