-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnushellkernel.py
62 lines (55 loc) · 2 KB
/
nushellkernel.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
from ipykernel.kernelbase import Kernel
import subprocess
import json
import tempfile
class NushellKernel(Kernel):
implementation = 'Nushell'
implementation_version = '1.0'
language = 'no-op'
language_version = '0.1'
language_info = {
'name': 'Any text',
'mimetype': 'text/plain',
'file_extension': '.txt',
}
banner = "Nushell kernel - let's have fun"
def do_execute(self, code, silent, store_history=True, user_expressions=None,
allow_stdin=False):
if not silent:
temp = tempfile.NamedTemporaryFile(suffix=".nu")
for line in code.splitlines():
line = line + " | to html\n"
temp.write(line.encode('utf-8'))
temp.flush()
command = 'nu ' + temp.name
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
output = output.decode('utf-8')
err = err.decode()
if err:
display_data = {
'data': {
"text/plain": err,
},
'metadata': {},
}
self.send_response(self.iopub_socket, 'display_data', display_data)
else:
display_data = {
'data': {
"text/html": output,
},
'metadata': {},
}
self.send_response(self.iopub_socket, 'display_data', display_data)
temp.close()
return {'status': 'ok',
# The base class increments the execution count
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {},
}
if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=NushellKernel)