execnb 0.1.7__tar.gz → 0.1.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,8 +1,8 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: execnb
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: A description of your project
5
- Home-page: https://github.com/fastai/execnb/
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 @@
1
+ __version__ = "0.1.9"
@@ -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'),
@@ -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 mistletoe,multiprocessing,types,traceback,signal
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.run_cell("from matplotlib_inline.backend_inline import set_matplotlib_formats")
67
- self.run_cell(f"set_matplotlib_formats('{mpl_format}')")
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 run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
70
- stdout=True, stderr=True, display=True, timeout=None):
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.run_cell(f"import sys; sys.path.insert(0, '{path.as_posix()}')")
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): # Capture stderr and save as output?
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
- def render_outputs(outputs, ansi_renderer=strip_ansi):
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
- return f"<pre>{txt}</pre>" if out['name']=='stdout' else f"<pre class='stderr'>{txt}</pre>"
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 mistletoe.markdown(d)
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 d := _g('text/plain'): return f"<pre>{escape(d)}</pre>"
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.7
3
+ Version: 0.1.9
4
4
  Summary: A description of your project
5
- Home-page: https://github.com/fastai/execnb/
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
 
@@ -5,14 +5,14 @@ repo = execnb
5
5
  description = A description of your project
6
6
  copyright = Put your copyright info here
7
7
  keywords = some keywords
8
- user = fastai
8
+ user = AnswerDotAI
9
9
  author = Jeremy Howard
10
10
  author_email = j@fast.ai
11
11
  branch = master
12
- version = 0.1.7
12
+ version = 0.1.9
13
13
  min_python = 3.7
14
- requirements = fastcore>=1.5.5 ipython mistletoe
15
- dev_requirements = matplotlib Pillow
14
+ requirements = fastcore>=1.5.5 ipython
15
+ dev_requirements = matplotlib Pillow mistletoe
16
16
  console_scripts = exec_nb=execnb.shell:exec_nb
17
17
  audience = Developers
18
18
  language = English
@@ -23,9 +23,9 @@ nbs_path = nbs
23
23
  doc_path = _docs
24
24
  recursive = False
25
25
  tst_flags = notest
26
- doc_host = https://fastai.github.io
26
+ doc_host = https://AnswerDotAI.github.io
27
27
  doc_baseurl = /execnb/
28
- git_url = https://github.com/fastai/execnb/
28
+ git_url = https://github.com/AnswerDotAI/execnb/
29
29
  lib_path = execnb
30
30
  title = execnb
31
31
  black_formatting = False
@@ -1 +0,0 @@
1
- __version__ = "0.1.7"
File without changes
File without changes
File without changes
File without changes
@@ -1,7 +1,7 @@
1
1
  fastcore>=1.5.5
2
2
  ipython
3
- mistletoe
4
3
 
5
4
  [dev]
6
5
  matplotlib
7
6
  Pillow
7
+ mistletoe
File without changes
File without changes
File without changes