This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconsole.html
80 lines (72 loc) · 2.35 KB
/
console.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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet"/>
<link href="renderedhtml.css" rel="stylesheet"/>
<script src="./pyodide_dev.js"></script>
</head>
<body>
<script>
languagePluginLoader.then(() => {
function pushCode(line) {
handleResult(c.push(line))
}
var term = $('body').terminal(
pushCode,
{
greetings: "Welcome to the Pyodide terminal emulator 🐍",
prompt: "[[;red;]>>> ]"
}
);
window.term = term;
pyodide.runPython(`
import io, code, sys
from js import term, pyodide
class Console(code.InteractiveConsole):
def runcode(self, code):
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
term.runPython("\\n".join(self.buffer))
_c = Console(locals=globals())
`)
var c = pyodide.pyimport('_c')
function handleResult(result) {
if (result) {
term.set_prompt('[[;gray;]... ]')
} else {
term.set_prompt('[[;red;]>>> ]')
var stderr = pyodide.runPython("sys.stderr.getvalue()").trim()
if (stderr) {
term.echo(`[[;red;]${stderr}]`)
} else {
var stdout = pyodide.runPython("sys.stdout.getvalue()")
if (stdout) {
term.echo(stdout.trim())
}
}
}
}
term.runPython = function(code) {
pyodide.runPythonAsync(code).then(
term.handlePythonResult, term.handlePythonError
)
}
term.handlePythonResult = function(result) {
if (result === undefined) {
return
} else if (result['_repr_html_'] !== undefined) {
term.echo(result['_repr_html_'], {raw: true})
} else {
term.echo(result.toString())
}
}
term.handlePythonError = function(result) {
term.error(result.toString())
}
});
</script>
</body>
</html>