execnb 0.1.7__py3-none-any.whl → 0.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- execnb/__init__.py +1 -1
- execnb/_modidx.py +4 -0
- execnb/shell.py +54 -26
- {execnb-0.1.7.dist-info → execnb-0.1.9.dist-info}/METADATA +3 -3
- execnb-0.1.9.dist-info/RECORD +10 -0
- execnb-0.1.7.dist-info/RECORD +0 -10
- {execnb-0.1.7.dist-info → execnb-0.1.9.dist-info}/LICENSE +0 -0
- {execnb-0.1.7.dist-info → execnb-0.1.9.dist-info}/WHEEL +0 -0
- {execnb-0.1.7.dist-info → execnb-0.1.9.dist-info}/entry_points.txt +0 -0
- {execnb-0.1.7.dist-info → execnb-0.1.9.dist-info}/top_level.txt +0 -0
execnb/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = "0.1.
|
1
|
+
__version__ = "0.1.9"
|
execnb/_modidx.py
CHANGED
@@ -22,6 +22,7 @@ d = { 'settings': { 'branch': 'master',
|
|
22
22
|
'execnb.nbio.write_nb': ('nbio.html#write_nb', 'execnb/nbio.py')},
|
23
23
|
'execnb.shell': { 'execnb.shell.CaptureShell': ('shell.html#captureshell', 'execnb/shell.py'),
|
24
24
|
'execnb.shell.CaptureShell.__init__': ('shell.html#captureshell.__init__', 'execnb/shell.py'),
|
25
|
+
'execnb.shell.CaptureShell._run': ('shell.html#captureshell._run', 'execnb/shell.py'),
|
25
26
|
'execnb.shell.CaptureShell.cell': ('shell.html#captureshell.cell', 'execnb/shell.py'),
|
26
27
|
'execnb.shell.CaptureShell.complete': ('shell.html#captureshell.complete', 'execnb/shell.py'),
|
27
28
|
'execnb.shell.CaptureShell.enable_gui': ('shell.html#captureshell.enable_gui', 'execnb/shell.py'),
|
@@ -29,6 +30,7 @@ d = { 'settings': { 'branch': 'master',
|
|
29
30
|
'execnb.shell.CaptureShell.prettytb': ('shell.html#captureshell.prettytb', 'execnb/shell.py'),
|
30
31
|
'execnb.shell.CaptureShell.run': ('shell.html#captureshell.run', 'execnb/shell.py'),
|
31
32
|
'execnb.shell.CaptureShell.run_all': ('shell.html#captureshell.run_all', 'execnb/shell.py'),
|
33
|
+
'execnb.shell.CaptureShell.run_async': ('shell.html#captureshell.run_async', 'execnb/shell.py'),
|
32
34
|
'execnb.shell.CaptureShell.run_cell': ('shell.html#captureshell.run_cell', 'execnb/shell.py'),
|
33
35
|
'execnb.shell.CaptureShell.set_path': ('shell.html#captureshell.set_path', 'execnb/shell.py'),
|
34
36
|
'execnb.shell.ExecutionInfo.__repr__': ('shell.html#executioninfo.__repr__', 'execnb/shell.py'),
|
@@ -48,6 +50,8 @@ d = { 'settings': { 'branch': 'master',
|
|
48
50
|
'execnb.shell._out_exc': ('shell.html#_out_exc', 'execnb/shell.py'),
|
49
51
|
'execnb.shell._out_nb': ('shell.html#_out_nb', 'execnb/shell.py'),
|
50
52
|
'execnb.shell._out_stream': ('shell.html#_out_stream', 'execnb/shell.py'),
|
53
|
+
'execnb.shell._pre': ('shell.html#_pre', 'execnb/shell.py'),
|
54
|
+
'execnb.shell._strip': ('shell.html#_strip', 'execnb/shell.py'),
|
51
55
|
'execnb.shell.exec_nb': ('shell.html#exec_nb', 'execnb/shell.py'),
|
52
56
|
'execnb.shell.find_output': ('shell.html#find_output', 'execnb/shell.py'),
|
53
57
|
'execnb.shell.format_exc': ('shell.html#format_exc', 'execnb/shell.py'),
|
execnb/shell.py
CHANGED
@@ -9,7 +9,7 @@ from fastcore.utils import *
|
|
9
9
|
from fastcore.script import call_parse
|
10
10
|
from fastcore.ansi import ansi2html
|
11
11
|
|
12
|
-
import
|
12
|
+
import multiprocessing,types,traceback,signal
|
13
13
|
try:
|
14
14
|
if sys.platform == 'darwin': multiprocessing.set_start_method("fork")
|
15
15
|
except RuntimeError: pass # if re-running cell
|
@@ -53,7 +53,7 @@ def __repr__(self: ExecutionResult): return f'result: {self.result}; err: {self.
|
|
53
53
|
class CaptureShell(InteractiveShell):
|
54
54
|
displayhook_class = _CustDisplayHook
|
55
55
|
|
56
|
-
def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeout=None):
|
56
|
+
def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeout:Optional[int]=None):
|
57
57
|
super().__init__()
|
58
58
|
self.history_manager.enabled = history
|
59
59
|
self.timeout = timeout
|
@@ -63,21 +63,15 @@ class CaptureShell(InteractiveShell):
|
|
63
63
|
if not IN_NOTEBOOK: InteractiveShell._instance = self
|
64
64
|
if set_matplotlib_formats:
|
65
65
|
self.enable_matplotlib("inline")
|
66
|
-
self.
|
67
|
-
self.
|
66
|
+
self._run("from matplotlib_inline.backend_inline import set_matplotlib_formats")
|
67
|
+
self._run(f"set_matplotlib_formats('{mpl_format}')")
|
68
68
|
|
69
|
-
def
|
70
|
-
stdout=True, stderr=True, display=True
|
71
|
-
if not timeout: timeout = self.timeout
|
69
|
+
def _run(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
70
|
+
stdout=True, stderr=True, display=True):
|
72
71
|
# TODO what if there's a comment?
|
73
72
|
semic = raw_cell.rstrip().endswith(';')
|
74
|
-
if timeout:
|
75
|
-
def handler(*args): raise TimeoutError()
|
76
|
-
signal.signal(signal.SIGALRM, handler)
|
77
|
-
signal.alarm(timeout)
|
78
73
|
with capture_output(display=display, stdout=stdout, stderr=stderr) as c:
|
79
74
|
result = super().run_cell(raw_cell, store_history, silent, shell_futures=shell_futures, cell_id=cell_id)
|
80
|
-
if timeout: signal.alarm(0)
|
81
75
|
return AttrDict(result=result, stdout='' if semic else c.stdout, stderr=c.stderr,
|
82
76
|
display_objects=c.outputs, exception=result.error_in_exec, quiet=semic)
|
83
77
|
|
@@ -85,10 +79,24 @@ class CaptureShell(InteractiveShell):
|
|
85
79
|
"Add `path` to python path, or `path.parent` if it's a file"
|
86
80
|
path = Path(path)
|
87
81
|
if path.is_file(): path = path.parent
|
88
|
-
self.
|
82
|
+
self._run(f"import sys; sys.path.insert(0, '{path.as_posix()}')")
|
89
83
|
|
90
84
|
def enable_gui(self, gui=None): pass
|
91
85
|
|
86
|
+
# %% ../nbs/02_shell.ipynb
|
87
|
+
@patch
|
88
|
+
def run_cell(self:CaptureShell, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
89
|
+
stdout=True, stderr=True, display=True, timeout=None):
|
90
|
+
if not timeout: timeout = self.timeout
|
91
|
+
if timeout:
|
92
|
+
def handler(*args): raise TimeoutError()
|
93
|
+
signal.signal(signal.SIGALRM, handler)
|
94
|
+
signal.alarm(timeout)
|
95
|
+
try: return self._run(raw_cell, store_history, silent, shell_futures, cell_id=cell_id,
|
96
|
+
stdout=stdout, stderr=stderr, display=display)
|
97
|
+
finally:
|
98
|
+
if timeout: signal.alarm(0)
|
99
|
+
|
92
100
|
# %% ../nbs/02_shell.ipynb
|
93
101
|
def format_exc(e):
|
94
102
|
"Format exception `e` as a string"
|
@@ -122,6 +130,8 @@ def _out_nb(o, fmt):
|
|
122
130
|
for x in o.display_objects: res.append(_mk_out(x.data, x.metadata))
|
123
131
|
if r is not None and not o.quiet:
|
124
132
|
res.append(_mk_out(*fmt.format(r), 'execute_result'))
|
133
|
+
if 'execution_count' not in o: o['execution_count']=None
|
134
|
+
for p in res: p['execution_count'] = o['execution_count']
|
125
135
|
return res
|
126
136
|
|
127
137
|
# %% ../nbs/02_shell.ipynb
|
@@ -129,31 +139,52 @@ def _out_nb(o, fmt):
|
|
129
139
|
def run(self:CaptureShell,
|
130
140
|
code:str, # Python/IPython code to run
|
131
141
|
stdout=True, # Capture stdout and save as output?
|
132
|
-
stderr=True
|
142
|
+
stderr=True, # Capture stderr and save as output?
|
143
|
+
timeout:Optional[int]=None): # Shell command will time out after {timeout} seconds
|
133
144
|
"Run `code`, returning a list of all outputs in Jupyter notebook format"
|
134
|
-
res = self.run_cell(code, stdout=stdout, stderr=stderr)
|
145
|
+
res = self.run_cell(code, stdout=stdout, stderr=stderr, timeout=timeout)
|
135
146
|
self.result = res.result.result
|
136
147
|
self.exc = res.exception
|
137
148
|
return _out_nb(res, self.display_formatter)
|
138
149
|
|
139
150
|
# %% ../nbs/02_shell.ipynb
|
140
|
-
|
151
|
+
@patch
|
152
|
+
async def run_async(self:CaptureShell,
|
153
|
+
code: str, # Python/IPython code to run
|
154
|
+
stdout=True, # Capture stdout and save as output?
|
155
|
+
stderr=True, # Capture stderr and save as output?
|
156
|
+
timeout:Optional[int]=None): # Shell command will time out after {timeout} seconds
|
157
|
+
return self.run(code, stdout=stdout, stderr=stderr, timeout=timeout)
|
158
|
+
|
159
|
+
# %% ../nbs/02_shell.ipynb
|
160
|
+
def _pre(s, xtra=''): return f"<pre {xtra}><code>{escape(s)}</code></pre>"
|
161
|
+
def _strip(s): return strip_ansi(escape(s))
|
162
|
+
|
163
|
+
def render_outputs(outputs, ansi_renderer=_strip, include_imgs=True, pygments=False):
|
164
|
+
try:
|
165
|
+
from mistletoe import markdown, HTMLRenderer
|
166
|
+
from mistletoe.contrib.pygments_renderer import PygmentsRenderer
|
167
|
+
except ImportError: return print('mistletoe not found -- please install it')
|
168
|
+
renderer = PygmentsRenderer if pygments else HTMLRenderer
|
141
169
|
def render_output(out):
|
142
170
|
otype = out['output_type']
|
143
171
|
if otype == 'stream':
|
144
172
|
txt = ansi_renderer(''.join(out['text']))
|
145
|
-
|
173
|
+
xtra = '' if out['name']=='stdout' else "class='stderr'"
|
174
|
+
return f"<pre {xtra}><code>{txt}</code></pre>"
|
146
175
|
elif otype in ('display_data','execute_result'):
|
147
176
|
data = out['data']
|
148
177
|
_g = lambda t: ''.join(data[t]) if t in data else None
|
149
178
|
if d := _g('text/html'): return d
|
150
179
|
if d := _g('application/javascript'): return f'<script>{d}</script>'
|
151
|
-
if d := _g('text/markdown'): return
|
152
|
-
if d := _g('image/svg+xml'): return d
|
153
|
-
if d := _g('image/jpeg'): return f'<img src="data:image/jpeg;base64,{d}"/>'
|
154
|
-
if d := _g('image/png'): return f'<img src="data:image/png;base64,{d}"/>'
|
180
|
+
if d := _g('text/markdown'): return markdown(d, renderer=renderer)
|
155
181
|
if d := _g('text/latex'): return f'<div class="math">${d}$</div>'
|
156
|
-
if
|
182
|
+
if include_imgs:
|
183
|
+
if d := _g('image/jpeg'): return f'<img src="data:image/jpeg;base64,{d}"/>'
|
184
|
+
if d := _g('image/png'): return f'<img src="data:image/png;base64,{d}"/>'
|
185
|
+
if d := _g('text/plain'): return _pre(d)
|
186
|
+
if d := _g('image/svg+xml'): return d
|
187
|
+
|
157
188
|
return ''
|
158
189
|
|
159
190
|
return '\n'.join(map(render_output, outputs))
|
@@ -165,10 +196,7 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
|
|
165
196
|
if cell.cell_type!='code': return
|
166
197
|
self._cell_idx = cell.idx_ + 1
|
167
198
|
outs = self.run(cell.source)
|
168
|
-
if outs:
|
169
|
-
cell.outputs = _dict2obj(outs)
|
170
|
-
for o in outs:
|
171
|
-
if 'execution_count' in o: cell['execution_count'] = o['execution_count']
|
199
|
+
if outs: cell.outputs = _dict2obj(outs)
|
172
200
|
|
173
201
|
# %% ../nbs/02_shell.ipynb
|
174
202
|
def find_output(outp, # Output from `run`
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: execnb
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.9
|
4
4
|
Summary: A description of your project
|
5
|
-
Home-page: https://github.com/
|
5
|
+
Home-page: https://github.com/AnswerDotAI/execnb/
|
6
6
|
Author: Jeremy Howard
|
7
7
|
Author-email: j@fast.ai
|
8
8
|
License: Apache Software License 2.0
|
@@ -18,10 +18,10 @@ Description-Content-Type: text/markdown
|
|
18
18
|
License-File: LICENSE
|
19
19
|
Requires-Dist: fastcore >=1.5.5
|
20
20
|
Requires-Dist: ipython
|
21
|
-
Requires-Dist: mistletoe
|
22
21
|
Provides-Extra: dev
|
23
22
|
Requires-Dist: matplotlib ; extra == 'dev'
|
24
23
|
Requires-Dist: Pillow ; extra == 'dev'
|
24
|
+
Requires-Dist: mistletoe ; extra == 'dev'
|
25
25
|
|
26
26
|
# execnb
|
27
27
|
|
@@ -0,0 +1,10 @@
|
|
1
|
+
execnb/__init__.py,sha256=XIaxbMbyiP-L3kguR1GhxirFblTXiHR1lMfDVITvHUI,22
|
2
|
+
execnb/_modidx.py,sha256=lGlGXiq_K9mthW2PvSJHZmzp2E__vtlAdaIM_XuBJes,6201
|
3
|
+
execnb/nbio.py,sha256=gs3EGN0sP2j47XUH31rTn2KT57b3wWnBZsn9AoZm2JQ,3677
|
4
|
+
execnb/shell.py,sha256=vhfFiZuFAX7glVSKV97j7zVuUYZb8roGfV2tLlDoOVk,13314
|
5
|
+
execnb-0.1.9.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
6
|
+
execnb-0.1.9.dist-info/METADATA,sha256=qFTOO-ZUF3Z7ayFeBGrnWD6FRxsliLHEyM5fd2Sn1fc,3297
|
7
|
+
execnb-0.1.9.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
8
|
+
execnb-0.1.9.dist-info/entry_points.txt,sha256=jZ8LPZCqnu4hXN_AgQpm05hVnTE-C25YK_4aiVX4i78,84
|
9
|
+
execnb-0.1.9.dist-info/top_level.txt,sha256=VGWmzsw8FOlT1r5TdOxaTWIAyRdclcxNdJ2r1hojf5U,7
|
10
|
+
execnb-0.1.9.dist-info/RECORD,,
|
execnb-0.1.7.dist-info/RECORD
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
execnb/__init__.py,sha256=YpKDcdV7CqL8n45u267wKtyloM13FSVbOdrqgNZnSLM,22
|
2
|
-
execnb/_modidx.py,sha256=3wJwFT95tiaCPmj7VMQEIim5qLTcOatC2od-ze5WJcc,5771
|
3
|
-
execnb/nbio.py,sha256=gs3EGN0sP2j47XUH31rTn2KT57b3wWnBZsn9AoZm2JQ,3677
|
4
|
-
execnb/shell.py,sha256=4wuuyphJ0M0NCvjQ2RbJlstDHUXFmnWNJExRdveiLew,11958
|
5
|
-
execnb-0.1.7.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
6
|
-
execnb-0.1.7.dist-info/METADATA,sha256=BqAfeem0tFfd_BxveYlY1_kxWuW35KGZc4AHAaSOSSw,3275
|
7
|
-
execnb-0.1.7.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
8
|
-
execnb-0.1.7.dist-info/entry_points.txt,sha256=jZ8LPZCqnu4hXN_AgQpm05hVnTE-C25YK_4aiVX4i78,84
|
9
|
-
execnb-0.1.7.dist-info/top_level.txt,sha256=VGWmzsw8FOlT1r5TdOxaTWIAyRdclcxNdJ2r1hojf5U,7
|
10
|
-
execnb-0.1.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|