execnb 0.2.2__tar.gz → 0.2.4__tar.gz
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-0.2.2/execnb.egg-info → execnb-0.2.4}/PKG-INFO +1 -1
- execnb-0.2.4/execnb/__init__.py +1 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb/_modidx.py +6 -1
- {execnb-0.2.2 → execnb-0.2.4}/execnb/shell.py +104 -49
- {execnb-0.2.2 → execnb-0.2.4/execnb.egg-info}/PKG-INFO +1 -1
- {execnb-0.2.2 → execnb-0.2.4}/pyproject.toml +5 -2
- execnb-0.2.2/execnb/__init__.py +0 -1
- {execnb-0.2.2 → execnb-0.2.4}/LICENSE +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/MANIFEST.in +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/README.md +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb.egg-info/SOURCES.txt +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb.egg-info/dependency_links.txt +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb.egg-info/entry_points.txt +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb.egg-info/requires.txt +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/execnb.egg-info/top_level.txt +0 -0
- {execnb-0.2.2 → execnb-0.2.4}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.4"
|
|
@@ -12,6 +12,8 @@ d = { 'settings': { 'branch': 'main',
|
|
|
12
12
|
'execnb.shell.CaptureShell.complete': ('shell.html#captureshell.complete', 'execnb/shell.py'),
|
|
13
13
|
'execnb.shell.CaptureShell.enable_gui': ('shell.html#captureshell.enable_gui', 'execnb/shell.py'),
|
|
14
14
|
'execnb.shell.CaptureShell.execute': ('shell.html#captureshell.execute', 'execnb/shell.py'),
|
|
15
|
+
'execnb.shell.CaptureShell.nbopen': ('shell.html#captureshell.nbopen', 'execnb/shell.py'),
|
|
16
|
+
'execnb.shell.CaptureShell.nbrun': ('shell.html#captureshell.nbrun', 'execnb/shell.py'),
|
|
15
17
|
'execnb.shell.CaptureShell.prettytb': ('shell.html#captureshell.prettytb', 'execnb/shell.py'),
|
|
16
18
|
'execnb.shell.CaptureShell.run': ('shell.html#captureshell.run', 'execnb/shell.py'),
|
|
17
19
|
'execnb.shell.CaptureShell.run_all': ('shell.html#captureshell.run_all', 'execnb/shell.py'),
|
|
@@ -32,6 +34,8 @@ d = { 'settings': { 'branch': 'main',
|
|
|
32
34
|
'execnb/shell.py'),
|
|
33
35
|
'execnb.shell._false': ('shell.html#_false', 'execnb/shell.py'),
|
|
34
36
|
'execnb.shell._format_mimedata': ('shell.html#_format_mimedata', 'execnb/shell.py'),
|
|
37
|
+
'execnb.shell._is_exported': ('shell.html#_is_exported', 'execnb/shell.py'),
|
|
38
|
+
'execnb.shell._is_noeval': ('shell.html#_is_noeval', 'execnb/shell.py'),
|
|
35
39
|
'execnb.shell._mk_out': ('shell.html#_mk_out', 'execnb/shell.py'),
|
|
36
40
|
'execnb.shell._out_exc': ('shell.html#_out_exc', 'execnb/shell.py'),
|
|
37
41
|
'execnb.shell._out_nb': ('shell.html#_out_nb', 'execnb/shell.py'),
|
|
@@ -44,4 +48,5 @@ d = { 'settings': { 'branch': 'main',
|
|
|
44
48
|
'execnb.shell.out_error': ('shell.html#out_error', 'execnb/shell.py'),
|
|
45
49
|
'execnb.shell.out_exec': ('shell.html#out_exec', 'execnb/shell.py'),
|
|
46
50
|
'execnb.shell.out_stream': ('shell.html#out_stream', 'execnb/shell.py'),
|
|
47
|
-
'execnb.shell.render_outputs': ('shell.html#render_outputs', 'execnb/shell.py')
|
|
51
|
+
'execnb.shell.render_outputs': ('shell.html#render_outputs', 'execnb/shell.py'),
|
|
52
|
+
'execnb.shell.select_cells': ('shell.html#select_cells', 'execnb/shell.py')}}}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
"""A shell for running notebook code without a notebook server
|
|
1
|
+
"""A shell for running notebook code without a notebook server
|
|
2
|
+
|
|
3
|
+
Docs: https://AnswerDotAI.github.io/execnb/shell.html.md"""
|
|
2
4
|
|
|
3
5
|
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_shell.ipynb.
|
|
4
6
|
|
|
5
7
|
# %% auto #0
|
|
6
8
|
__all__ = ['CaptureShell', 'format_exc', 'NbResult', 'render_outputs', 'find_output', 'out_exec', 'out_stream', 'out_error',
|
|
7
|
-
'exec_nb', 'SmartCompleter']
|
|
9
|
+
'select_cells', 'exec_nb', 'SmartCompleter']
|
|
8
10
|
|
|
9
11
|
# %% ../nbs/00_shell.ipynb #535003cf
|
|
10
12
|
from fastcore.utils import *
|
|
11
13
|
from fastcore.script import call_parse
|
|
12
|
-
from fastcore.ansi import
|
|
14
|
+
from fastcore.ansi import strip_ansi
|
|
13
15
|
|
|
14
16
|
import multiprocessing,types,traceback,signal
|
|
15
17
|
try:
|
|
@@ -19,11 +21,10 @@ except RuntimeError: pass # if re-running cell
|
|
|
19
21
|
from IPython.core.interactiveshell import InteractiveShell, ExecutionInfo, ExecutionResult
|
|
20
22
|
from IPython.core.displayhook import DisplayHook
|
|
21
23
|
from IPython.utils.capture import capture_output
|
|
22
|
-
from IPython.core.completer import IPCompleter,provisionalcompleter
|
|
24
|
+
from IPython.core.completer import IPCompleter,provisionalcompleter
|
|
23
25
|
from IPython.core.hooks import CommandChainDispatcher
|
|
24
26
|
from IPython.core.completerlib import module_completer
|
|
25
27
|
from IPython.utils.strdispatch import StrDispatch
|
|
26
|
-
from IPython.display import display as disp, HTML
|
|
27
28
|
|
|
28
29
|
from base64 import b64encode
|
|
29
30
|
from html import escape
|
|
@@ -52,10 +53,10 @@ class CaptureShell(InteractiveShell):
|
|
|
52
53
|
super().__init__()
|
|
53
54
|
self.history_manager.enabled = history
|
|
54
55
|
self.timeout = timeout
|
|
55
|
-
self.result,self.exc = None,None
|
|
56
|
+
self.result,self.exc,self._cell_idx,self._fname = None,None,None,None
|
|
56
57
|
if path: self.set_path(path)
|
|
57
58
|
self.display_formatter.active = True
|
|
58
|
-
if not
|
|
59
|
+
if not in_notebook(): InteractiveShell._instance = self
|
|
59
60
|
if mpl_format:
|
|
60
61
|
try: from matplotlib_inline.backend_inline import set_matplotlib_formats
|
|
61
62
|
except ImportError: set_matplotlib_formats = None
|
|
@@ -65,14 +66,13 @@ class CaptureShell(InteractiveShell):
|
|
|
65
66
|
self._run(f"set_matplotlib_formats('{mpl_format}')")
|
|
66
67
|
|
|
67
68
|
def _run(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
|
68
|
-
|
|
69
|
+
stdout=True, stderr=True, display=True, verbose=False):
|
|
69
70
|
# TODO what if there's a comment?
|
|
70
71
|
semic = raw_cell.rstrip().endswith(';')
|
|
71
72
|
with capture_output(display=display, stdout=stdout and not verbose, stderr=stderr and not verbose) as c:
|
|
72
73
|
result = super().run_cell(raw_cell, store_history, silent, shell_futures=shell_futures, cell_id=cell_id)
|
|
73
|
-
return AttrDict(result=result, stdout='' if semic else c.stdout, stderr=c.stderr,
|
|
74
|
-
|
|
75
|
-
exception=result.error_in_exec or result.error_before_exec, quiet=semic)
|
|
74
|
+
return AttrDict(result=result, stdout='' if semic else c.stdout, stderr=c.stderr, display_objects=c.outputs,
|
|
75
|
+
exception=result.error_in_exec or result.error_before_exec, quiet=semic)
|
|
76
76
|
|
|
77
77
|
def set_path(self, path):
|
|
78
78
|
"Add `path` to python path, or `path.parent` if it's a file"
|
|
@@ -85,14 +85,14 @@ class CaptureShell(InteractiveShell):
|
|
|
85
85
|
# %% ../nbs/00_shell.ipynb #93adf867
|
|
86
86
|
@patch
|
|
87
87
|
def run_cell(self:CaptureShell, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
|
88
|
-
|
|
88
|
+
stdout=True, stderr=True, display=True, timeout=None, verbose=False):
|
|
89
89
|
if not timeout: timeout = self.timeout
|
|
90
90
|
if timeout:
|
|
91
91
|
def handler(*args): raise TimeoutError()
|
|
92
92
|
signal.signal(signal.SIGALRM, handler)
|
|
93
93
|
signal.alarm(timeout)
|
|
94
94
|
try: return self._run(raw_cell, store_history, silent, shell_futures, cell_id=cell_id,
|
|
95
|
-
|
|
95
|
+
stdout=stdout, stderr=stderr, display=display, verbose=verbose)
|
|
96
96
|
finally:
|
|
97
97
|
if timeout: signal.alarm(0)
|
|
98
98
|
|
|
@@ -130,8 +130,7 @@ def _out_nb(o, fmt):
|
|
|
130
130
|
if o.exception: res.append(_out_exc(o.exception))
|
|
131
131
|
res.result = o.result.result
|
|
132
132
|
for x in o.display_objects: res.append(_mk_out(x.data, x.metadata))
|
|
133
|
-
if res.result is not None and not o.quiet:
|
|
134
|
-
res.append(_mk_out(*fmt.format(res.result), 'execute_result'))
|
|
133
|
+
if res.result is not None and not o.quiet: res.append(_mk_out(*fmt.format(res.result), 'execute_result'))
|
|
135
134
|
if 'execution_count' not in o: o['execution_count']=None
|
|
136
135
|
for p in res:
|
|
137
136
|
if p["output_type"]=="execute_result": p['execution_count'] = o['execution_count']
|
|
@@ -140,11 +139,11 @@ def _out_nb(o, fmt):
|
|
|
140
139
|
# %% ../nbs/00_shell.ipynb #242f732f
|
|
141
140
|
@patch
|
|
142
141
|
def run(self:CaptureShell,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
142
|
+
code:str, # Python/IPython code to run
|
|
143
|
+
stdout=True, # Capture stdout and save as output?
|
|
144
|
+
stderr=True, # Capture stderr and save as output?
|
|
145
|
+
timeout:Optional[int]=None, # Shell command will time out after {timeout} seconds
|
|
146
|
+
verbose:bool=False): # Show stdout/stderr during execution
|
|
148
147
|
"Run `code`, returning a list of all outputs in Jupyter notebook format"
|
|
149
148
|
res = self.run_cell(code, stdout=stdout, stderr=stderr, timeout=timeout, verbose=verbose)
|
|
150
149
|
self.result = res.result.result
|
|
@@ -154,11 +153,11 @@ def run(self:CaptureShell,
|
|
|
154
153
|
# %% ../nbs/00_shell.ipynb #eaa11df9
|
|
155
154
|
@patch
|
|
156
155
|
async def run_async(self:CaptureShell,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
156
|
+
code: str, # Python/IPython code to run
|
|
157
|
+
stdout=True, # Capture stdout and save as output?
|
|
158
|
+
stderr=True, # Capture stderr and save as output?
|
|
159
|
+
timeout:Optional[int]=None, # Shell command will time out after {timeout} seconds
|
|
160
|
+
verbose:bool=False): # Show stdout/stderr during execution
|
|
162
161
|
return self.run(code, stdout=stdout, stderr=stderr, timeout=timeout, verbose=verbose)
|
|
163
162
|
|
|
164
163
|
# %% ../nbs/00_shell.ipynb #f698a432
|
|
@@ -207,7 +206,7 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True, verbose=False):
|
|
|
207
206
|
|
|
208
207
|
# %% ../nbs/00_shell.ipynb #008c0cef
|
|
209
208
|
def find_output(outp, # Output from `run`
|
|
210
|
-
|
|
209
|
+
ot='execute_result' # Output_type to find
|
|
211
210
|
):
|
|
212
211
|
"Find first output of type `ot` in `CaptureShell.run` output"
|
|
213
212
|
return first(o for o in outp if o['output_type']==ot)
|
|
@@ -234,15 +233,16 @@ def out_error(outp):
|
|
|
234
233
|
def _false(o): return False
|
|
235
234
|
|
|
236
235
|
@patch
|
|
237
|
-
def run_all(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
236
|
+
def run_all(
|
|
237
|
+
self:CaptureShell,
|
|
238
|
+
nb, # A notebook read with `nbclient` or `read_nb`
|
|
239
|
+
exc_stop:bool=False, # Stop on exceptions?
|
|
240
|
+
preproc:callable=_false, # Called before each cell is executed
|
|
241
|
+
postproc:callable=_false, # Called after each cell is executed
|
|
242
|
+
inject_code:str|None=None, # Code to inject into a cell
|
|
243
|
+
inject_idx:int=0, # Cell to replace with `inject_code`
|
|
244
|
+
verbose:bool=False # Show stdout/stderr during execution
|
|
245
|
+
):
|
|
246
246
|
"Run all cells in `nb`, stopping at first exception if `exc_stop`"
|
|
247
247
|
if inject_code is not None: nb.cells[inject_idx].source = inject_code
|
|
248
248
|
for cell in nb.cells:
|
|
@@ -254,15 +254,15 @@ def run_all(self:CaptureShell,
|
|
|
254
254
|
# %% ../nbs/00_shell.ipynb #70808010
|
|
255
255
|
@patch
|
|
256
256
|
def execute(self:CaptureShell,
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
257
|
+
src:str|Path, # Notebook path to read from
|
|
258
|
+
dest:str|None=None, # Notebook path to write to
|
|
259
|
+
exc_stop:bool=False, # Stop on exceptions?
|
|
260
|
+
preproc:callable=_false, # Called before each cell is executed
|
|
261
|
+
postproc:callable=_false, # Called after each cell is executed
|
|
262
|
+
inject_code:str|None=None, # Code to inject into a cell
|
|
263
|
+
inject_path:str|Path|None=None, # Path to file containing code to inject into a cell
|
|
264
|
+
inject_idx:int=0, # Cell to replace with `inject_code`
|
|
265
|
+
verbose:bool=False # Show stdout/stderr during execution
|
|
266
266
|
):
|
|
267
267
|
"Execute notebook from `src` and save with outputs to `dest"
|
|
268
268
|
nb = read_nb(src)
|
|
@@ -270,13 +270,13 @@ def execute(self:CaptureShell,
|
|
|
270
270
|
self.set_path(Path(src).parent.resolve())
|
|
271
271
|
if inject_path is not None: inject_code = Path(inject_path).read_text()
|
|
272
272
|
self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,
|
|
273
|
-
|
|
273
|
+
inject_code=inject_code, inject_idx=inject_idx, verbose=verbose)
|
|
274
274
|
if dest: write_nb(nb, dest)
|
|
275
275
|
|
|
276
276
|
# %% ../nbs/00_shell.ipynb #694161cb
|
|
277
277
|
@patch
|
|
278
278
|
def prettytb(self:CaptureShell,
|
|
279
|
-
|
|
279
|
+
fname:str|Path=None): # filename to print alongside the traceback
|
|
280
280
|
"Show a pretty traceback for notebooks, optionally printing `fname`."
|
|
281
281
|
fname = fname if fname else self._fname
|
|
282
282
|
_fence = '='*75
|
|
@@ -285,6 +285,62 @@ def prettytb(self:CaptureShell,
|
|
|
285
285
|
fname_str = f' in {fname}' if fname else ''
|
|
286
286
|
return f"{type(self.exc).__name__}{fname_str}:\n{_fence}\n{cell_str}\n"
|
|
287
287
|
|
|
288
|
+
# %% ../nbs/00_shell.ipynb #de6bd9f6
|
|
289
|
+
def _is_exported(src): return bool(re.search(r'^\s*#\|\s*exports?\b', src, flags=re.M))
|
|
290
|
+
def _is_noeval(src):
|
|
291
|
+
return 'nbdev_export'+'(' in src or bool(re.search(r'^\s*#\|\s*eval:\s*false\s*$', src, flags=re.M|re.I))
|
|
292
|
+
|
|
293
|
+
def select_cells(
|
|
294
|
+
nb, # A notebook read with `read_nb`
|
|
295
|
+
msgid:str=None, # Cell id prefix to match
|
|
296
|
+
above:bool=False, # Include the matched cell and all cells above it?
|
|
297
|
+
below:bool=False, # Include the matched cell and all cells below it?
|
|
298
|
+
all:bool=False, # Include all code cells (ignores `msgid`)?
|
|
299
|
+
exported:bool=False, # Only cells with `#| export` or `#| exports`?
|
|
300
|
+
skip_noeval:bool=False # Skip `#| eval: false` and `nbdev_export` cells (like `nbdev-test`)?
|
|
301
|
+
):
|
|
302
|
+
"Select code cells from `nb` by cell id prefix"
|
|
303
|
+
cells = [o for o in nb.cells if o.cell_type=='code']
|
|
304
|
+
if not all:
|
|
305
|
+
if not msgid: raise ValueError('`msgid` required unless `all=True`')
|
|
306
|
+
idx = first(i for i,o in enumerate(cells) if str(o.id).startswith(msgid))
|
|
307
|
+
if idx is None: raise ValueError(f'No code cell id starting with {msgid!r}')
|
|
308
|
+
if above: cells = cells[:idx+1]
|
|
309
|
+
elif below: cells = cells[idx:]
|
|
310
|
+
else: cells = [cells[idx]]
|
|
311
|
+
if exported: cells = [o for o in cells if _is_exported(o.source)]
|
|
312
|
+
if skip_noeval: cells = [o for o in cells if not _is_noeval(o.source)]
|
|
313
|
+
return cells
|
|
314
|
+
|
|
315
|
+
# %% ../nbs/00_shell.ipynb #9c16e245
|
|
316
|
+
from fastcore.nbio import render_text
|
|
317
|
+
|
|
318
|
+
# %% ../nbs/00_shell.ipynb #ec223d10
|
|
319
|
+
@patch
|
|
320
|
+
def nbopen(self:CaptureShell, fname:str|Path):
|
|
321
|
+
"Set the default notebook for `nbrun`"
|
|
322
|
+
self._nbrun_fname = Path(fname)
|
|
323
|
+
|
|
324
|
+
@patch
|
|
325
|
+
def nbrun(
|
|
326
|
+
self:CaptureShell,
|
|
327
|
+
msgid:str=None, # Cell id prefix to run
|
|
328
|
+
fname:str|Path=None, # Notebook path (defaults to last `nbopen`)
|
|
329
|
+
above:bool=False, # Also run all cells above the match?
|
|
330
|
+
below:bool=False, # Also run all cells below the match?
|
|
331
|
+
all:bool=False, # Run all code cells?
|
|
332
|
+
exported:bool=False, # Only cells with `#| export` or `#| exports`?
|
|
333
|
+
skip_noeval:bool=False # Skip `#| eval: false` and `nbdev_export` cells (like `nbdev-test`)?
|
|
334
|
+
):
|
|
335
|
+
"Run cell(s) from a notebook by id prefix, printing rendered outputs"
|
|
336
|
+
fname = ifnone(fname, getattr(self,'_nbrun_fname',None))
|
|
337
|
+
if not fname: raise ValueError('No `fname` passed and no notebook opened with `nbopen`')
|
|
338
|
+
self.nbopen(fname)
|
|
339
|
+
nb = read_nb(fname)
|
|
340
|
+
for cell in select_cells(nb, msgid, above=above, below=below, all=all, exported=exported, skip_noeval=skip_noeval):
|
|
341
|
+
res = render_text(self.run(cell.source))
|
|
342
|
+
if res: print(f'--- {cell.id} ---\n{res}')
|
|
343
|
+
|
|
288
344
|
# %% ../nbs/00_shell.ipynb #1227c8b1
|
|
289
345
|
@call_parse
|
|
290
346
|
def exec_nb(
|
|
@@ -298,7 +354,7 @@ def exec_nb(
|
|
|
298
354
|
):
|
|
299
355
|
"Execute notebook from `src` and save with outputs to `dest`"
|
|
300
356
|
CaptureShell().execute(src, dest, exc_stop=exc_stop, inject_code=inject_code,
|
|
301
|
-
|
|
357
|
+
inject_path=inject_path, inject_idx=inject_idx, verbose=verbose)
|
|
302
358
|
|
|
303
359
|
# %% ../nbs/00_shell.ipynb #c44963a0
|
|
304
360
|
class SmartCompleter(IPCompleter):
|
|
@@ -315,8 +371,7 @@ class SmartCompleter(IPCompleter):
|
|
|
315
371
|
|
|
316
372
|
def __call__(self, c):
|
|
317
373
|
if not c: return []
|
|
318
|
-
with provisionalcompleter():
|
|
319
|
-
res = [o.text.rpartition('.')[-1] for o in self.completions(c, len(c)) if o.type!='magic']
|
|
374
|
+
with provisionalcompleter(): res = [o.text.rpartition('.')[-1] for o in self.completions(c, len(c)) if o.type!='magic']
|
|
320
375
|
if res and res[0][-1]=='=': res = [o for o in res if o[-1]=='=']
|
|
321
376
|
return res
|
|
322
377
|
|
|
@@ -35,8 +35,11 @@ version = {attr = "execnb.__version__"}
|
|
|
35
35
|
include = ["execnb"]
|
|
36
36
|
|
|
37
37
|
[tool.nbdev]
|
|
38
|
-
allowed_metadata_keys = ['
|
|
39
|
-
allowed_cell_metadata_keys = [
|
|
38
|
+
allowed_metadata_keys = ['solveit']
|
|
39
|
+
allowed_cell_metadata_keys = ['solveit_ai']
|
|
40
40
|
recursive = false
|
|
41
41
|
jupyter_hooks = true
|
|
42
42
|
custom_sidebar = false
|
|
43
|
+
|
|
44
|
+
[tool.chkstyle]
|
|
45
|
+
skip_paths = ["_modidx.py"]
|
execnb-0.2.2/execnb/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.2"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|