-
Notifications
You must be signed in to change notification settings - Fork 0
/
hs4Jupyter.py
75 lines (63 loc) · 3.27 KB
/
hs4Jupyter.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
from ipykernel.kernelbase import Kernel
import subprocess
import os
class Hs4Jupyter(Kernel):
# automate some of these in future
implementation = 'GHC'
implementation_version = '8.4'
language = 'Haskell'
language_version = '2010'
language_info = {
'name': 'Haskell',
'mimetype': 'text/plain',
'file_extension': '.hs',
}
banner = "GHC Haskell kernel for Jupyter notebook"
# this is called by IPKernelApp.launch_instance. initialize the interpreter
def __init__(self, **kwargs):
Kernel.__init__(self, **kwargs)
self._start_Haskell()
def _start_Haskell(self):
# find path to kernel in the same directory as hs4Jupyter.py
self.kernelpath = os.path.dirname(__file__)
self.kernelpath = os.path.join(self.kernelpath, "kernel")
# start kernel using Popen object
self.kernelInstance = subprocess.Popen(self.kernelpath, bufsize=-1, executable=None, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=None,
close_fds=True, shell=False, cwd=None, env=None,
universal_newlines=True, startupinfo=None,
creationflags=0, restore_signals=True,
start_new_session=False, pass_fds=(),
encoding=None, errors=None, text=True)
def do_execute(self, code, silent, store_history=True, user_expressions=None,allow_stdin=False):
# run code using the PIPE (inside Popen object self.kernelInstance)
if not silent:
#send_response is used for printing to the stdio (notebook)
#code = str.rstrip(str.rstrip(code)) $ strip string of useless whitespace. (necessary?)
stream_content = {'name': 'stdout', 'text': "path to kernel is "+self.kernelpath+"\n"}
self.send_response(self.iopub_socket, 'stream', stream_content)
#run code in kernel
stream_content = {'name': 'stdout', 'text': "writing to kernel\n"}
self.send_response(self.iopub_socket, 'stream', stream_content)
self.kernelInstance.stdin.write(code+'\n')
#read output
stream_content = {'name': 'stdout', 'text': "reading from kernel\n"}
self.send_response(self.iopub_socket, 'stream', stream_content)
#send the output to stdout
output = self.kernelInstance.stdout.readline()
stream_content = {'name': 'stdout', 'text': output}
self.send_response(self.iopub_socket, 'stream', stream_content)
return {'status': 'ok',
# The base class increments the execution count
'execution_count': self.execution_count,
'payload': [],
# user_expressions is a python dictionary containing the
# results of the computation
'user_expressions': {},
}
def do_shutdown(restart):
# orderly shutdown of the kernel
return
if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=Hs4Jupyter)