execnb 0.2.7__tar.gz → 0.2.9__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.7
3
+ Version: 0.2.9
4
4
  Summary: A description of your project
5
5
  Author-email: Jeremy Howard <j@fast.ai>
6
6
  License: Apache-2.0
@@ -15,7 +15,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
15
15
  Requires-Python: >=3.7
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
- Requires-Dist: fastcore>=1.12.27
18
+ Requires-Dist: fastcore>=2.1.2
19
19
  Requires-Dist: ipython
20
20
  Provides-Extra: dev
21
21
  Requires-Dist: matplotlib; extra == "dev"
@@ -0,0 +1 @@
1
+ __version__ = "0.2.9"
@@ -47,6 +47,7 @@ d = { 'settings': { 'branch': 'main',
47
47
  'execnb.shell.exec_nb': ('shell.html#exec_nb', 'execnb/shell.py'),
48
48
  'execnb.shell.find_output': ('shell.html#find_output', 'execnb/shell.py'),
49
49
  'execnb.shell.format_exc': ('shell.html#format_exc', 'execnb/shell.py'),
50
+ 'execnb.shell.leading_comment_lines': ('shell.html#leading_comment_lines', 'execnb/shell.py'),
50
51
  'execnb.shell.out_error': ('shell.html#out_error', 'execnb/shell.py'),
51
52
  'execnb.shell.out_exec': ('shell.html#out_exec', 'execnb/shell.py'),
52
53
  'execnb.shell.out_stream': ('shell.html#out_stream', 'execnb/shell.py'),
@@ -5,8 +5,8 @@ Docs: https://AnswerDotAI.github.io/execnb/shell.html.md"""
5
5
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_shell.ipynb.
6
6
 
7
7
  # %% auto #0
8
- __all__ = ['CaptureShell', 'format_exc', 'NbResult', 'render_outputs', 'find_output', 'out_exec', 'out_stream', 'out_error',
9
- 'select_cells', 'exec_nb', 'SmartCompleter']
8
+ __all__ = ['leading_comment_lines', 'CaptureShell', 'format_exc', 'NbResult', 'render_outputs', 'find_output', 'out_exec',
9
+ 'out_stream', 'out_error', 'select_cells', 'exec_nb', 'SmartCompleter']
10
10
 
11
11
  # %% ../nbs/00_shell.ipynb #535003cf
12
12
  from fastcore.utils import *
@@ -22,6 +22,7 @@ from traitlets.config import Config
22
22
  from traitlets.config.loader import PyFileConfigLoader
23
23
  from IPython.paths import get_ipython_dir
24
24
  from IPython.core.interactiveshell import InteractiveShell, ExecutionInfo, ExecutionResult
25
+ from IPython.core.error import UsageError
25
26
  from IPython.core.displayhook import DisplayHook
26
27
  from IPython.utils.capture import capture_output
27
28
  from IPython.core.completer import IPCompleter,provisionalcompleter
@@ -60,6 +61,12 @@ def _profile_cfg(name='default'):
60
61
  cfg.merge(c)
61
62
  return cfg,exts,pd
62
63
 
64
+ # %% ../nbs/00_shell.ipynb #62ece372
65
+ def leading_comment_lines(lines):
66
+ "Input cleanup transform: drop leading blank/comment lines when the first code line is a cell magic, which IPython otherwise misparses as a line magic"
67
+ i = next((i for i,l in enumerate(lines) if l.strip() and not l.lstrip().startswith('#')), 0)
68
+ return lines[i:] if i and lines[i].startswith('%%') else lines
69
+
63
70
  # %% ../nbs/00_shell.ipynb #e3fb2bee
64
71
  class CaptureShell(InteractiveShell):
65
72
  displayhook_class = _CustDisplayHook
@@ -68,6 +75,7 @@ class CaptureShell(InteractiveShell):
68
75
  cfg = _profile_cfg()[0] if profile else Config()
69
76
  if not history: cfg.HistoryManager.hist_file = ':memory:'
70
77
  super().__init__(config=cfg)
78
+ self.input_transformers_cleanup.append(leading_comment_lines)
71
79
  self.history_manager.enabled = history
72
80
  self.timeout = timeout
73
81
  self.result,self.exc,self._cell_idx,self._fname,self._nested = None,None,None,None,False
@@ -100,8 +108,11 @@ class CaptureShell(InteractiveShell):
100
108
  finally: self._nested = old
101
109
  finally:
102
110
  if timeout: signal.alarm(0)
111
+ e = result.error_in_exec or result.error_before_exec
112
+ if isinstance(e, UsageError) and str(e).startswith('Line magic function `%%'):
113
+ e.args = (f"{e} A cell magic (%%) must start the cell; remove or move the lines above it.",)
103
114
  return AttrDict(result=result, stdout='' if semic else c.stdout, stderr=c.stderr, display_objects=c.outputs,
104
- exception=result.error_in_exec or result.error_before_exec, quiet=semic)
115
+ exception=e, quiet=semic)
105
116
 
106
117
  def set_path(self, path):
107
118
  "Add `path` to python path, or `path.parent` if it's a file"
@@ -334,9 +345,9 @@ def prettytb(self:CaptureShell,
334
345
  return f"{type(self.exc).__name__}{fname_str}:\n{_fence}\n{cell_str}\n"
335
346
 
336
347
  # %% ../nbs/00_shell.ipynb #de6bd9f6
337
- def _is_exported(src): return bool(re.search(r'^\s*#\|\s*exports?\b', src, flags=re.M))
338
- def _is_noeval(src):
339
- return 'nbdev_export'+'(' in src or bool(re.search(r'^\s*#\|\s*eval:\s*false\s*$', src, flags=re.M|re.I))
348
+ def _is_exported(cell): return cell.has_directive('export') or cell.has_directive('exports')
349
+ def _is_noeval(cell):
350
+ return 'nbdev_export'+'(' in cell.source or (cell.directive('eval') or '').lower()=='false'
340
351
 
341
352
  def select_cells(
342
353
  nb, # A notebook read with `read_nb`
@@ -358,8 +369,8 @@ def select_cells(
358
369
  if above: cells = cells[:idx+1]
359
370
  elif below: cells = cells[idx:]
360
371
  else: cells = [cells[idx]]
361
- if exported: cells = [o for o in cells if _is_exported(o.source)]
362
- if skip_noeval: cells = [o for o in cells if not _is_noeval(o.source)]
372
+ if exported: cells = [o for o in cells if _is_exported(o)]
373
+ if skip_noeval: cells = [o for o in cells if not _is_noeval(o)]
363
374
  return cells
364
375
 
365
376
  # %% ../nbs/00_shell.ipynb #9c16e245
@@ -382,7 +393,8 @@ def nbrun(
382
393
  below:bool=False, # Also run all cells below the match?
383
394
  all:bool=False, # Run all code cells?
384
395
  exported:bool=False, # Only cells with `#| export` or `#| exports`?
385
- skip_noeval:bool=False # Skip `#| eval: false` and `nbdev_export` cells (like `nbdev-test`)?
396
+ skip_noeval:bool=False, # Skip `#| eval: false` and `nbdev_export` cells (like `nbdev-test`)?
397
+ stop_on_error:bool=True, # Stop after the first cell whose run errors?
386
398
  ):
387
399
  "Run cell(s) from a notebook by id prefix, printing rendered outputs"
388
400
  fname = ifnone(fname, getattr(self,'_nbrun_fname',None))
@@ -392,8 +404,9 @@ def nbrun(
392
404
  if all or not msgids: msgids = (None,)
393
405
  for msgid in msgids:
394
406
  for cell in select_cells(nb, msgid, above=above, below=below, all=all, exported=exported, skip_noeval=skip_noeval):
395
- res = render_text(self.run(cell.source))
396
- if res: print(f'--- {cell.id} ---\n{res}')
407
+ outs = self.run(cell.source)
408
+ if res := render_text(outs): print(f'--- {cell.id} ---\n{res}')
409
+ if stop_on_error and any(o.get('output_type')=='error' for o in outs): return
397
410
 
398
411
  # %% ../nbs/00_shell.ipynb #1227c8b1
399
412
  @call_parse
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: execnb
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Summary: A description of your project
5
5
  Author-email: Jeremy Howard <j@fast.ai>
6
6
  License: Apache-2.0
@@ -15,7 +15,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
15
15
  Requires-Python: >=3.7
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
- Requires-Dist: fastcore>=1.12.27
18
+ Requires-Dist: fastcore>=2.1.2
19
19
  Requires-Dist: ipython
20
20
  Provides-Extra: dev
21
21
  Requires-Dist: matplotlib; extra == "dev"
@@ -1,4 +1,4 @@
1
- fastcore>=1.12.27
1
+ fastcore>=2.1.2
2
2
  ipython
3
3
 
4
4
  [dev]
@@ -12,7 +12,7 @@ license = {text = "Apache-2.0"}
12
12
  authors = [{name = "Jeremy Howard", email = "j@fast.ai"}]
13
13
  keywords = ['some', 'keywords']
14
14
  classifiers = ["Natural Language :: English", "Intended Audience :: Developers", "Development Status :: 2 - Pre-Alpha", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only"]
15
- dependencies = ['fastcore>=1.12.27', 'ipython']
15
+ dependencies = ['fastcore>=2.1.2', 'ipython']
16
16
 
17
17
  [project.urls]
18
18
  Repository = "https://github.com/AnswerDotAI/execnb/"
@@ -1 +0,0 @@
1
- __version__ = "0.2.7"
File without changes
File without changes
File without changes
File without changes