-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole_main.py
31 lines (25 loc) · 978 Bytes
/
console_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
from pyodide.console import BANNER, PyodideConsole, repr_shorten
from pyodide import to_js
import __main__
import time
__all__ = ["BANNER", "pycomplete", "exec_code"]
pyconsole = PyodideConsole(globals=__main__.__dict__, filename="<console>")
def pycomplete(source):
return pyconsole.complete(source)
async def exec_code(
code : str, syntax_check_passed, stdin_callback, stdout_callback, stderr_callback
):
pyconsole.stdin_callback = stdin_callback
pyconsole.stdout_callback = stdout_callback
pyconsole.stderr_callback = stderr_callback
for line in code.splitlines():
fut = pyconsole.push(line)
if fut.syntax_check == "syntax-error":
return to_js([-1, fut.formatted_error])
syntax_check_passed()
try:
result = await fut
repr_result = repr_shorten(result) if result is not None else None
return to_js([0, repr_result])
except Exception:
return to_js([-1, fut.formatted_error])