-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfyr_kernel.py
95 lines (75 loc) · 3.05 KB
/
fyr_kernel.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os.path as op
import tempfile
import subprocess
from subprocess import run, PIPE
import magic
import base64
def exec_fyr(code):
"""Compile, execute Fyr code, and return the standard output."""
# Create temp folder which gets deleted upon leaving block
with tempfile.TemporaryDirectory() as tmpdir:
# Define source and executable filenames
source_path = op.join(tmpdir, "temp.fyr")
program_path = op.join(tmpdir, "temp")
# Write code to the Fyr file
with open(source_path, "w") as f:
f.write(code)
# Compile the Fyr code into an executable and save stdout/stderr
res = run(["fyrc", "-n", source_path], stdout=PIPE, stderr=subprocess.STDOUT)
# Execute program depending on returncode and return output or stdout+stderr
# Check if Image in Mimetype, if yes: convert to png (because Jupyter can display PNG)
# And return base64 encoded png
# 0 = text, 1 = error, 2 = image
if res.returncode == 0:
output = run([program_path], stdout=PIPE)
if ("image" in magic.from_buffer(output.stdout, mime=True)):
png = run(["convert", "-", "png:-"], input=output.stdout, stdout=PIPE)
return [base64.b64encode(png.stdout).decode("utf-8"), 2]
else:
return [output.stdout.decode("utf-8"), 0]
else:
return [res.stdout.decode("utf-8"), 1]
"""Fyr wrapper kernel"""
from ipykernel.kernelbase import Kernel
class FyrKernel(Kernel):
# Kernel information
implementation = "Fyr"
implementation_version = "1.1"
language = "fyr"
language_version = "0.1.4"
language_info = {
"name": "fyr",
"mimetype": "text/plain",
"file_extension": ".fyr",
"codemirror_mode": "go",
}
banner = "Fyr kernel"
def do_execute(
self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
):
"""Function which is run when a code cell is executed"""
# Run Fyr code and get result (output)
result = exec_fyr(code)
if not silent:
# Send back result to frontend depending on returnvalue of exec_fyr
# 0 = text, 1 = error, 2 = image
if result[1] == 0:
stream_content = {"name": "stdout", "text": result[0]}
msg_type = "stream"
elif result[1] == 2:
stream_content = {"data": {"image/png": result[0]}}
msg_type = "display_data"
else:
stream_content = {"name": "stderr", "text": result[0]}
msg_type = "stream"
self.send_response(self.iopub_socket, msg_type, stream_content)
return {
"status": "ok",
# Base class increments execution count
"execution_count": self.execution_count,
"payload": [],
"user_expressions": {},
}
if __name__ == "__main__":
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=FyrKernel)