-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpython-console-worker.ts
More file actions
158 lines (145 loc) · 4.45 KB
/
python-console-worker.ts
File metadata and controls
158 lines (145 loc) · 4.45 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
importScripts('https://cdn.jsdelivr.net/pyodide/v0.22.0/full/pyodide.js')
interface Pyodide {
loadPackage: (packages: string[]) => Promise<void>
pyimport: (pkg: string) => micropip
// eslint-disable-next-line @typescript-eslint/no-explicit-any
runPythonAsync: (code: string, namespace?: any) => Promise<void>
version: string
FS: {
readFile: (name: string, options: unknown) => void
writeFile: (name: string, data: string, options: unknown) => void
mkdir: (name: string) => void
rmdir: (name: string) => void
}
globals: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get: (variable: string) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set: (variable: string, value: any) => void
}
isPyProxy: (value: unknown) => boolean
}
interface micropip {
install: (packages: string[]) => Promise<void>
}
declare global {
interface Window {
loadPyodide: ({
stdout
}: {
stdout?: (msg: string) => void
}) => Promise<Pyodide>
pyodide: Pyodide
}
}
// Monkey patch console.log to prevent the script from outputting logs
// eslint-disable-next-line @typescript-eslint/no-empty-function
console.log = () => {}
import { expose } from 'comlink'
const initConsoleCode = `
import sys
from pyodide.ffi import to_js
from pyodide.console import PyodideConsole, repr_shorten, BANNER
import __main__
BANNER = "Welcome to the Pyodide terminal emulator 🐍\\n" + BANNER
pyconsole = PyodideConsole(__main__.__dict__)
import builtins
async def await_fut(fut):
res = await fut
if res is not None:
builtins._ = res
return to_js([res], depth=1)
def clear_console():
pyconsole.buffer = []
`
let pythonConsole: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reprShorten: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
awaitFut: (fut: unknown) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
pyconsole: any
clearConsole: () => void
}
const python = {
async init(
stdout: (msg: string) => void,
onLoad: ({ version, banner }: { version: string; banner?: string }) => void,
packages: string[][]
) {
self.pyodide = await self.loadPyodide({})
if (packages[0].length > 0) {
await self.pyodide.loadPackage(packages[0])
}
if (packages[1].length > 0) {
await self.pyodide.loadPackage(['micropip'])
const micropip = self.pyodide.pyimport('micropip')
await micropip.install(packages[1])
}
const version = self.pyodide.version
const namespace = self.pyodide.globals.get('dict')()
await self.pyodide.runPythonAsync(initConsoleCode, { globals: namespace })
const reprShorten = namespace.get('repr_shorten')
const banner = namespace.get('BANNER')
const awaitFut = namespace.get('await_fut')
const pyconsole = namespace.get('pyconsole')
const clearConsole = namespace.get('clear_console')
namespace.destroy()
// eslint-disable-next-line camelcase
pyconsole.stdout_callback = stdout
pythonConsole = {
reprShorten,
awaitFut,
pyconsole,
clearConsole
}
onLoad({ version, banner })
},
async run(
code: string
): Promise<{ state: string; error?: string } | undefined> {
if (!pythonConsole) {
throw new Error('Console has not been initialised')
}
if (code === undefined) {
throw new Error('No code to push')
}
let state
for (const line of code.split('\n')) {
const fut = pythonConsole.pyconsole.push(line)
state = fut.syntax_check
const wrapped = pythonConsole.awaitFut(fut)
try {
const [value] = await wrapped
if (self.pyodide.isPyProxy(value)) {
value.destroy()
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.constructor.name === 'PythonError') {
const message = fut.formatted_error || error.message
return { state, error: message.trimEnd() }
} else {
throw error
}
} finally {
fut.destroy()
wrapped.destroy()
}
}
return { state }
},
readFile(name: string) {
return self.pyodide.FS.readFile(name, { encoding: 'utf8' })
},
writeFile(name: string, data: string) {
return self.pyodide.FS.writeFile(name, data, { encoding: 'utf8' })
},
mkdir(name: string) {
self.pyodide.FS.mkdir(name)
},
rmdir(name: string) {
self.pyodide.FS.rmdir(name)
}
}
expose(python)