-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
122 lines (106 loc) · 4.86 KB
/
index.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Black WASM</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
<style>
html,
body {
height: 100%;
}
.min-100 {
min-height: 100%;
}
*:focus {
box-shadow: none !important;
outline: none;
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<!-- and it's easy to individually load additional languages -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/python.min.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js"></script>
<script>
function setPageToLoading() {
document.getElementById("format_button").disabled = true;
// document.getElementById("format_button").innerText = "Loading";
document.querySelectorAll("#format_button span").forEach(span => span.style.display = "");
document.getElementById("floatingTextarea").disabled = true;
}
function setPageToReady() {
document.getElementById("format_button").disabled = false;
// document.getElementById("format_button").innerText = "Format";
document.querySelectorAll("#format_button span").forEach(span => span.style.display = "none");
document.getElementById("floatingTextarea").disabled = false;
}
</script>
</head>
<body>
<div class="container-fluid min-100 d-flex flex-column">
<nav class="navbar bg-body-tertiary flex-shrink-0">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">
Black WASM Formatter | No data transfered to server
<span class="ms-5">
<button id="format_button" type="button" class="btn btn-success" onclick="formatInput()">
Format
<span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
<span class="visually-hidden" role="status">Loading...</span>
</button>
</span>
</span>
<span>
<a href="https://github.com/eugene-prout/black-wasm" style="color: inherit;"><i class="bi bi-github"
style="font-size: 2rem"></i></a>
</span>
</div>
</nav>
<div class="row flex-grow-1">
<div class="col flex-grow-1 p-0 m-0">
<textarea class="form-control p-0 m-0" style="min-height: 100%; min-width: 50%; overflow-y: scroll;"
placeholder="Paste Python here to format it" id="floatingTextarea"></textarea>
</div>
<div class="col flex-grow-1 p-0 m-0">
<pre class="p-0 m-0" style="min-height: 100%; white-space: pre-wrap; min-width: 50%; overflow-y: scroll">
<code id="output" class="p-0 m-0">Formatted Python will appear here...</code>
</pre>
</div>
</div>
</div>
<script>
async function main() {
setPageToLoading()
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install('black');
pyodide.runPython(`print("Python loaded")`)
setPageToReady()
}
main();
async function formatInput() {
setPageToLoading()
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install('black');
let text = document.getElementById("floatingTextarea").value
pyodide.FS.writeFile("/data.txt", text, { encoding: "utf8" });
let ret = pyodide.runPython(`
from black import Mode, format_file_contents
with open("/data.txt", "r") as fh:
data = fh.read()
format_file_contents(data, fast=False, mode=Mode())
`);
document.getElementById("output").innerHTML = ret
hljs.highlightElement(document.getElementById("output"))
setPageToReady()
}
</script>
</body>
</html>