-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_kernel.py
232 lines (187 loc) · 6.49 KB
/
simple_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import queue
from jupyter_client.manager import start_new_kernel
from pprint import PrettyPrinter
class SimpleKernel():
"""
## Description
**SimpleKernel**:
A simplistic Jupyter kernel client wrapper.
Additional information in [this GitHub issue]
(
)
"""
def __init__(self):
"""
## Description
Initializes the `kernel_manager` and `client` objects
and starts the kernel. Also initializes the pretty printer
for displaying object properties and execution result
payloads.
## Parameters
None.
"""
### Initialize kernel and client
self.kernel_manager, self.client = start_new_kernel()
### Initialize pretty printer
self.pp = PrettyPrinter(indent=2)
### end __init__ ####
def execute(self, code, verbose=False, get_type=False):
"""
## Description
**execute**:
Executes a code string in the kernel. Can return either
the full execution response payload, or just `stdout`. Also,
there is a verbose mode that displays the execution process.
## Parameters
code : string
The code string to get passed to `stdin`.
verbose : bool (default=False)
Whether to display processing information.
get_type : bool (default=False) NOT IMPLEMENTED
When implemented, will return a dict including the output
and the type. E.g.
1+1 ==> {stdout: 2, type: int}
"hello" ==> {stdout: "hello", type: str}
print("hello") ==> {stdout: "hello", type: NoneType}
a=10 ==> {stdout: None, type: None}
## Returns
`stdout` or the full response payload.
"""
debug = lambda x: print(x if verbose else '', end='')
debug("----------------")
debug("executing code: " + code)
### Execute the code
msg_id = self.client.execute(code)
### Collect the response payload
reply = self.client.get_shell_msg(msg_id)
debug("reply content")
debug(reply['content'])
### Get the execution status
### When the execution state is "idle" it is complete
io_msg_content = self.client.get_iopub_msg(timeout=1)['content']
### We're going to catch this here before we start polling
if 'execution_state' in io_msg_content and io_msg_content['execution_state'] == 'idle':
return "no output"
### Continue polling for execution to complete
### which is indicated by having an execution state of "idle"
while True:
### Save the last message content. This will hold the solution.
### The next one has the idle execution state indicating the execution
###is complete, but not the stdout output
temp = io_msg_content
### Poll the message
try:
io_msg_content = self.client.get_iopub_msg(timeout=1)['content']
debug("io_msg content")
debug(io_msg_content)
if 'execution_state' in io_msg_content and io_msg_content['execution_state'] == 'idle':
break
except queue.Empty:
debug("timeout get_iopub_msg")
break
debug("temp")
debug(temp)
### Check the message for various possibilities
if 'data' in temp: # Indicates completed operation
debug('has data')
out = temp['data']['text/plain']
if get_type:
debug("code: " + code)
the_type = self.execute('type(' + code + ')')
debug(the_type)
out = {out: the_type}
elif 'name' in temp and temp['name'] == "stdout": # indicates output
debug('name is stdout')
out = temp['text']
if get_type:
debug("code: " + code)
if code.startswith('print('):
the_type = "string"
else:
the_type = self.execute('type(' + code + ')')
debug(the_type)
out = {out: the_type}
debug("out is " + str(out))
elif 'traceback' in temp: # Indicates error
print("ERROR")
out = '\n'.join(temp['traceback']) # Put error into nice format
else:
out = ''
debug("----------------\n\n")
debug("returning " + str(out))
return out
### end execute ####
def showManager(self):
"""
## Description
**showManager**:
Pretty Print kernel manager object.
"""
self.pp(self.kernel_manager)
def showClient(self):
"""
## Description
**showClient**:
Pretty Print client object.
"""
self.pp(self.client)
def prettyPrint(self, payload):
"""
## Description
**prettyPrint**:
A convenience method to pretty print the reply payload.
## example
```
>>> reply = my_kernel.execute("1+1")
>>> my_kernel.prettyPrint(reply)
```
"""
def __del__(self):
"""
## Description
Destructor. Shuts down kernel safely.
"""
self.kernel_manager.shutdown_kernel()
### end Simple Kernel ###
def test(verbose=False):
kernel = SimpleKernel()
commands = \
[
'1+1',
'a=5',
'b=0',
'b',
'print()',
'print("hello there")',
'10',
'a*b',
'a',
'a+b',
's = "this is s"',
'print(s)',
'type(s)',
'type(a)',
'type(1.0*a)',
'print(a+b)',
'print(a*10)',
'c=1/b',
'd = {"a":1,"b":"Two","c":[1,2,3]}',
'd',
'import json',
'j = json.loads(str(d).replace(\"\\\'\",\"\\"\"))',
'j',
'import pandas as pd',
'df = pd.DataFrame(dict(A=[1,2,3], B=["one", "two", "three"]))',
'df',
'df.describe()'
]
for command in commands:
print(">>>" + command)
out = kernel.execute(
command,
verbose=False,
get_type=False
)
if out: print(out)
if __name__=="__main__":
test()