execnb 0.2.3__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.3/execnb.egg-info → execnb-0.2.4}/PKG-INFO +1 -1
- execnb-0.2.4/execnb/__init__.py +1 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb/_modidx.py +1 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb/shell.py +42 -41
- {execnb-0.2.3 → execnb-0.2.4/execnb.egg-info}/PKG-INFO +1 -1
- {execnb-0.2.3 → execnb-0.2.4}/pyproject.toml +3 -0
- execnb-0.2.3/execnb/__init__.py +0 -1
- {execnb-0.2.3 → execnb-0.2.4}/LICENSE +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/MANIFEST.in +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/README.md +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb.egg-info/SOURCES.txt +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb.egg-info/dependency_links.txt +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb.egg-info/entry_points.txt +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb.egg-info/requires.txt +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/execnb.egg-info/top_level.txt +0 -0
- {execnb-0.2.3 → execnb-0.2.4}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.4"
|
|
@@ -35,6 +35,7 @@ d = { 'settings': { 'branch': 'main',
|
|
|
35
35
|
'execnb.shell._false': ('shell.html#_false', 'execnb/shell.py'),
|
|
36
36
|
'execnb.shell._format_mimedata': ('shell.html#_format_mimedata', 'execnb/shell.py'),
|
|
37
37
|
'execnb.shell._is_exported': ('shell.html#_is_exported', 'execnb/shell.py'),
|
|
38
|
+
'execnb.shell._is_noeval': ('shell.html#_is_noeval', 'execnb/shell.py'),
|
|
38
39
|
'execnb.shell._mk_out': ('shell.html#_mk_out', 'execnb/shell.py'),
|
|
39
40
|
'execnb.shell._out_exc': ('shell.html#_out_exc', 'execnb/shell.py'),
|
|
40
41
|
'execnb.shell._out_nb': ('shell.html#_out_nb', 'execnb/shell.py'),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""A shell for running notebook code without a notebook server
|
|
2
2
|
|
|
3
|
-
Docs: https://AnswerDotAI.github.io/
|
|
3
|
+
Docs: https://AnswerDotAI.github.io/execnb/shell.html.md"""
|
|
4
4
|
|
|
5
5
|
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_shell.ipynb.
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ __all__ = ['CaptureShell', 'format_exc', 'NbResult', 'render_outputs', 'find_out
|
|
|
11
11
|
# %% ../nbs/00_shell.ipynb #535003cf
|
|
12
12
|
from fastcore.utils import *
|
|
13
13
|
from fastcore.script import call_parse
|
|
14
|
-
from fastcore.ansi import
|
|
14
|
+
from fastcore.ansi import strip_ansi
|
|
15
15
|
|
|
16
16
|
import multiprocessing,types,traceback,signal
|
|
17
17
|
try:
|
|
@@ -21,11 +21,10 @@ except RuntimeError: pass # if re-running cell
|
|
|
21
21
|
from IPython.core.interactiveshell import InteractiveShell, ExecutionInfo, ExecutionResult
|
|
22
22
|
from IPython.core.displayhook import DisplayHook
|
|
23
23
|
from IPython.utils.capture import capture_output
|
|
24
|
-
from IPython.core.completer import IPCompleter,provisionalcompleter
|
|
24
|
+
from IPython.core.completer import IPCompleter,provisionalcompleter
|
|
25
25
|
from IPython.core.hooks import CommandChainDispatcher
|
|
26
26
|
from IPython.core.completerlib import module_completer
|
|
27
27
|
from IPython.utils.strdispatch import StrDispatch
|
|
28
|
-
from IPython.display import display as disp, HTML
|
|
29
28
|
|
|
30
29
|
from base64 import b64encode
|
|
31
30
|
from html import escape
|
|
@@ -54,7 +53,7 @@ class CaptureShell(InteractiveShell):
|
|
|
54
53
|
super().__init__()
|
|
55
54
|
self.history_manager.enabled = history
|
|
56
55
|
self.timeout = timeout
|
|
57
|
-
self.result,self.exc = None,None
|
|
56
|
+
self.result,self.exc,self._cell_idx,self._fname = None,None,None,None
|
|
58
57
|
if path: self.set_path(path)
|
|
59
58
|
self.display_formatter.active = True
|
|
60
59
|
if not in_notebook(): InteractiveShell._instance = self
|
|
@@ -67,14 +66,13 @@ class CaptureShell(InteractiveShell):
|
|
|
67
66
|
self._run(f"set_matplotlib_formats('{mpl_format}')")
|
|
68
67
|
|
|
69
68
|
def _run(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
|
70
|
-
|
|
69
|
+
stdout=True, stderr=True, display=True, verbose=False):
|
|
71
70
|
# TODO what if there's a comment?
|
|
72
71
|
semic = raw_cell.rstrip().endswith(';')
|
|
73
72
|
with capture_output(display=display, stdout=stdout and not verbose, stderr=stderr and not verbose) as c:
|
|
74
73
|
result = super().run_cell(raw_cell, store_history, silent, shell_futures=shell_futures, cell_id=cell_id)
|
|
75
|
-
return AttrDict(result=result, stdout='' if semic else c.stdout, stderr=c.stderr,
|
|
76
|
-
|
|
77
|
-
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)
|
|
78
76
|
|
|
79
77
|
def set_path(self, path):
|
|
80
78
|
"Add `path` to python path, or `path.parent` if it's a file"
|
|
@@ -87,14 +85,14 @@ class CaptureShell(InteractiveShell):
|
|
|
87
85
|
# %% ../nbs/00_shell.ipynb #93adf867
|
|
88
86
|
@patch
|
|
89
87
|
def run_cell(self:CaptureShell, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
|
|
90
|
-
|
|
88
|
+
stdout=True, stderr=True, display=True, timeout=None, verbose=False):
|
|
91
89
|
if not timeout: timeout = self.timeout
|
|
92
90
|
if timeout:
|
|
93
91
|
def handler(*args): raise TimeoutError()
|
|
94
92
|
signal.signal(signal.SIGALRM, handler)
|
|
95
93
|
signal.alarm(timeout)
|
|
96
94
|
try: return self._run(raw_cell, store_history, silent, shell_futures, cell_id=cell_id,
|
|
97
|
-
|
|
95
|
+
stdout=stdout, stderr=stderr, display=display, verbose=verbose)
|
|
98
96
|
finally:
|
|
99
97
|
if timeout: signal.alarm(0)
|
|
100
98
|
|
|
@@ -132,8 +130,7 @@ def _out_nb(o, fmt):
|
|
|
132
130
|
if o.exception: res.append(_out_exc(o.exception))
|
|
133
131
|
res.result = o.result.result
|
|
134
132
|
for x in o.display_objects: res.append(_mk_out(x.data, x.metadata))
|
|
135
|
-
if res.result is not None and not o.quiet:
|
|
136
|
-
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'))
|
|
137
134
|
if 'execution_count' not in o: o['execution_count']=None
|
|
138
135
|
for p in res:
|
|
139
136
|
if p["output_type"]=="execute_result": p['execution_count'] = o['execution_count']
|
|
@@ -142,11 +139,11 @@ def _out_nb(o, fmt):
|
|
|
142
139
|
# %% ../nbs/00_shell.ipynb #242f732f
|
|
143
140
|
@patch
|
|
144
141
|
def run(self:CaptureShell,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
|
150
147
|
"Run `code`, returning a list of all outputs in Jupyter notebook format"
|
|
151
148
|
res = self.run_cell(code, stdout=stdout, stderr=stderr, timeout=timeout, verbose=verbose)
|
|
152
149
|
self.result = res.result.result
|
|
@@ -156,11 +153,11 @@ def run(self:CaptureShell,
|
|
|
156
153
|
# %% ../nbs/00_shell.ipynb #eaa11df9
|
|
157
154
|
@patch
|
|
158
155
|
async def run_async(self:CaptureShell,
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
|
164
161
|
return self.run(code, stdout=stdout, stderr=stderr, timeout=timeout, verbose=verbose)
|
|
165
162
|
|
|
166
163
|
# %% ../nbs/00_shell.ipynb #f698a432
|
|
@@ -209,7 +206,7 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True, verbose=False):
|
|
|
209
206
|
|
|
210
207
|
# %% ../nbs/00_shell.ipynb #008c0cef
|
|
211
208
|
def find_output(outp, # Output from `run`
|
|
212
|
-
|
|
209
|
+
ot='execute_result' # Output_type to find
|
|
213
210
|
):
|
|
214
211
|
"Find first output of type `ot` in `CaptureShell.run` output"
|
|
215
212
|
return first(o for o in outp if o['output_type']==ot)
|
|
@@ -257,15 +254,15 @@ def run_all(
|
|
|
257
254
|
# %% ../nbs/00_shell.ipynb #70808010
|
|
258
255
|
@patch
|
|
259
256
|
def execute(self:CaptureShell,
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
|
269
266
|
):
|
|
270
267
|
"Execute notebook from `src` and save with outputs to `dest"
|
|
271
268
|
nb = read_nb(src)
|
|
@@ -273,13 +270,13 @@ def execute(self:CaptureShell,
|
|
|
273
270
|
self.set_path(Path(src).parent.resolve())
|
|
274
271
|
if inject_path is not None: inject_code = Path(inject_path).read_text()
|
|
275
272
|
self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,
|
|
276
|
-
|
|
273
|
+
inject_code=inject_code, inject_idx=inject_idx, verbose=verbose)
|
|
277
274
|
if dest: write_nb(nb, dest)
|
|
278
275
|
|
|
279
276
|
# %% ../nbs/00_shell.ipynb #694161cb
|
|
280
277
|
@patch
|
|
281
278
|
def prettytb(self:CaptureShell,
|
|
282
|
-
|
|
279
|
+
fname:str|Path=None): # filename to print alongside the traceback
|
|
283
280
|
"Show a pretty traceback for notebooks, optionally printing `fname`."
|
|
284
281
|
fname = fname if fname else self._fname
|
|
285
282
|
_fence = '='*75
|
|
@@ -290,6 +287,8 @@ def prettytb(self:CaptureShell,
|
|
|
290
287
|
|
|
291
288
|
# %% ../nbs/00_shell.ipynb #de6bd9f6
|
|
292
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))
|
|
293
292
|
|
|
294
293
|
def select_cells(
|
|
295
294
|
nb, # A notebook read with `read_nb`
|
|
@@ -297,7 +296,8 @@ def select_cells(
|
|
|
297
296
|
above:bool=False, # Include the matched cell and all cells above it?
|
|
298
297
|
below:bool=False, # Include the matched cell and all cells below it?
|
|
299
298
|
all:bool=False, # Include all code cells (ignores `msgid`)?
|
|
300
|
-
exported:bool=False # Only cells with `#| export` or `#| exports`?
|
|
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
301
|
):
|
|
302
302
|
"Select code cells from `nb` by cell id prefix"
|
|
303
303
|
cells = [o for o in nb.cells if o.cell_type=='code']
|
|
@@ -309,6 +309,7 @@ def select_cells(
|
|
|
309
309
|
elif below: cells = cells[idx:]
|
|
310
310
|
else: cells = [cells[idx]]
|
|
311
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)]
|
|
312
313
|
return cells
|
|
313
314
|
|
|
314
315
|
# %% ../nbs/00_shell.ipynb #9c16e245
|
|
@@ -328,14 +329,15 @@ def nbrun(
|
|
|
328
329
|
above:bool=False, # Also run all cells above the match?
|
|
329
330
|
below:bool=False, # Also run all cells below the match?
|
|
330
331
|
all:bool=False, # Run all code cells?
|
|
331
|
-
exported:bool=False # Only cells with `#| export` or `#| exports`?
|
|
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`)?
|
|
332
334
|
):
|
|
333
335
|
"Run cell(s) from a notebook by id prefix, printing rendered outputs"
|
|
334
336
|
fname = ifnone(fname, getattr(self,'_nbrun_fname',None))
|
|
335
337
|
if not fname: raise ValueError('No `fname` passed and no notebook opened with `nbopen`')
|
|
336
338
|
self.nbopen(fname)
|
|
337
339
|
nb = read_nb(fname)
|
|
338
|
-
for cell in select_cells(nb, msgid, above=above, below=below, all=all, exported=exported):
|
|
340
|
+
for cell in select_cells(nb, msgid, above=above, below=below, all=all, exported=exported, skip_noeval=skip_noeval):
|
|
339
341
|
res = render_text(self.run(cell.source))
|
|
340
342
|
if res: print(f'--- {cell.id} ---\n{res}')
|
|
341
343
|
|
|
@@ -352,7 +354,7 @@ def exec_nb(
|
|
|
352
354
|
):
|
|
353
355
|
"Execute notebook from `src` and save with outputs to `dest`"
|
|
354
356
|
CaptureShell().execute(src, dest, exc_stop=exc_stop, inject_code=inject_code,
|
|
355
|
-
|
|
357
|
+
inject_path=inject_path, inject_idx=inject_idx, verbose=verbose)
|
|
356
358
|
|
|
357
359
|
# %% ../nbs/00_shell.ipynb #c44963a0
|
|
358
360
|
class SmartCompleter(IPCompleter):
|
|
@@ -369,8 +371,7 @@ class SmartCompleter(IPCompleter):
|
|
|
369
371
|
|
|
370
372
|
def __call__(self, c):
|
|
371
373
|
if not c: return []
|
|
372
|
-
with provisionalcompleter():
|
|
373
|
-
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']
|
|
374
375
|
if res and res[0][-1]=='=': res = [o for o in res if o[-1]=='=']
|
|
375
376
|
return res
|
|
376
377
|
|
execnb-0.2.3/execnb/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.3"
|
|
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
|