execnb 0.2.2__tar.gz → 0.2.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: execnb
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: A description of your project
5
5
  Author-email: Jeremy Howard <j@fast.ai>
6
6
  License: Apache-2.0
@@ -0,0 +1 @@
1
+ __version__ = "0.2.3"
@@ -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,7 @@ 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'),
35
38
  'execnb.shell._mk_out': ('shell.html#_mk_out', 'execnb/shell.py'),
36
39
  'execnb.shell._out_exc': ('shell.html#_out_exc', 'execnb/shell.py'),
37
40
  'execnb.shell._out_nb': ('shell.html#_out_nb', 'execnb/shell.py'),
@@ -44,4 +47,5 @@ d = { 'settings': { 'branch': 'main',
44
47
  'execnb.shell.out_error': ('shell.html#out_error', 'execnb/shell.py'),
45
48
  'execnb.shell.out_exec': ('shell.html#out_exec', 'execnb/shell.py'),
46
49
  'execnb.shell.out_stream': ('shell.html#out_stream', 'execnb/shell.py'),
47
- 'execnb.shell.render_outputs': ('shell.html#render_outputs', 'execnb/shell.py')}}}
50
+ 'execnb.shell.render_outputs': ('shell.html#render_outputs', 'execnb/shell.py'),
51
+ 'execnb.shell.select_cells': ('shell.html#select_cells', 'execnb/shell.py')}}}
@@ -1,10 +1,12 @@
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/execnbshell.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 *
@@ -55,7 +57,7 @@ class CaptureShell(InteractiveShell):
55
57
  self.result,self.exc = None,None
56
58
  if path: self.set_path(path)
57
59
  self.display_formatter.active = True
58
- if not IN_NOTEBOOK: InteractiveShell._instance = self
60
+ if not in_notebook(): InteractiveShell._instance = self
59
61
  if mpl_format:
60
62
  try: from matplotlib_inline.backend_inline import set_matplotlib_formats
61
63
  except ImportError: set_matplotlib_formats = None
@@ -234,15 +236,16 @@ def out_error(outp):
234
236
  def _false(o): return False
235
237
 
236
238
  @patch
237
- def run_all(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
- ):
239
+ def run_all(
240
+ self:CaptureShell,
241
+ nb, # A notebook read with `nbclient` or `read_nb`
242
+ exc_stop:bool=False, # Stop on exceptions?
243
+ preproc:callable=_false, # Called before each cell is executed
244
+ postproc:callable=_false, # Called after each cell is executed
245
+ inject_code:str|None=None, # Code to inject into a cell
246
+ inject_idx:int=0, # Cell to replace with `inject_code`
247
+ verbose:bool=False # Show stdout/stderr during execution
248
+ ):
246
249
  "Run all cells in `nb`, stopping at first exception if `exc_stop`"
247
250
  if inject_code is not None: nb.cells[inject_idx].source = inject_code
248
251
  for cell in nb.cells:
@@ -285,6 +288,57 @@ def prettytb(self:CaptureShell,
285
288
  fname_str = f' in {fname}' if fname else ''
286
289
  return f"{type(self.exc).__name__}{fname_str}:\n{_fence}\n{cell_str}\n"
287
290
 
291
+ # %% ../nbs/00_shell.ipynb #de6bd9f6
292
+ def _is_exported(src): return bool(re.search(r'^\s*#\|\s*exports?\b', src, flags=re.M))
293
+
294
+ def select_cells(
295
+ nb, # A notebook read with `read_nb`
296
+ msgid:str=None, # Cell id prefix to match
297
+ above:bool=False, # Include the matched cell and all cells above it?
298
+ below:bool=False, # Include the matched cell and all cells below it?
299
+ all:bool=False, # Include all code cells (ignores `msgid`)?
300
+ exported:bool=False # Only cells with `#| export` or `#| exports`?
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
+ return cells
313
+
314
+ # %% ../nbs/00_shell.ipynb #9c16e245
315
+ from fastcore.nbio import render_text
316
+
317
+ # %% ../nbs/00_shell.ipynb #ec223d10
318
+ @patch
319
+ def nbopen(self:CaptureShell, fname:str|Path):
320
+ "Set the default notebook for `nbrun`"
321
+ self._nbrun_fname = Path(fname)
322
+
323
+ @patch
324
+ def nbrun(
325
+ self:CaptureShell,
326
+ msgid:str=None, # Cell id prefix to run
327
+ fname:str|Path=None, # Notebook path (defaults to last `nbopen`)
328
+ above:bool=False, # Also run all cells above the match?
329
+ below:bool=False, # Also run all cells below the match?
330
+ all:bool=False, # Run all code cells?
331
+ exported:bool=False # Only cells with `#| export` or `#| exports`?
332
+ ):
333
+ "Run cell(s) from a notebook by id prefix, printing rendered outputs"
334
+ fname = ifnone(fname, getattr(self,'_nbrun_fname',None))
335
+ if not fname: raise ValueError('No `fname` passed and no notebook opened with `nbopen`')
336
+ self.nbopen(fname)
337
+ nb = read_nb(fname)
338
+ for cell in select_cells(nb, msgid, above=above, below=below, all=all, exported=exported):
339
+ res = render_text(self.run(cell.source))
340
+ if res: print(f'--- {cell.id} ---\n{res}')
341
+
288
342
  # %% ../nbs/00_shell.ipynb #1227c8b1
289
343
  @call_parse
290
344
  def exec_nb(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: execnb
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: A description of your project
5
5
  Author-email: Jeremy Howard <j@fast.ai>
6
6
  License: Apache-2.0
@@ -35,8 +35,8 @@ version = {attr = "execnb.__version__"}
35
35
  include = ["execnb"]
36
36
 
37
37
  [tool.nbdev]
38
- allowed_metadata_keys = ['solveit_dialog_mode', 'solveit_ver']
39
- allowed_cell_metadata_keys = ["solveit_ai"]
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
@@ -1 +0,0 @@
1
- __version__ = "0.2.2"
File without changes
File without changes
File without changes
File without changes