Skip to content

Commit bf76fb4

Browse files
committed
fixes #59
1 parent 6d2c7c3 commit bf76fb4

File tree

5 files changed

+77
-63
lines changed

5 files changed

+77
-63
lines changed

execnb/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'execnb.shell.CaptureShell.__init__': ('shell.html#captureshell.__init__', 'execnb/shell.py'),
2525
'execnb.shell.CaptureShell.cell': ('shell.html#captureshell.cell', 'execnb/shell.py'),
2626
'execnb.shell.CaptureShell.complete': ('shell.html#captureshell.complete', 'execnb/shell.py'),
27+
'execnb.shell.CaptureShell.enable_gui': ('shell.html#captureshell.enable_gui', 'execnb/shell.py'),
2728
'execnb.shell.CaptureShell.execute': ('shell.html#captureshell.execute', 'execnb/shell.py'),
2829
'execnb.shell.CaptureShell.prettytb': ('shell.html#captureshell.prettytb', 'execnb/shell.py'),
2930
'execnb.shell.CaptureShell.run': ('shell.html#captureshell.run', 'execnb/shell.py'),

execnb/shell.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def __init__(self, path:str|Path=None, mpl_format='retina'):
6060
self.display_formatter.active = True
6161
if not IN_NOTEBOOK: InteractiveShell._instance = self
6262
if set_matplotlib_formats:
63+
self.enable_matplotlib("inline")
6364
self.run_cell("from matplotlib_inline.backend_inline import set_matplotlib_formats")
6465
self.run_cell(f"set_matplotlib_formats('{mpl_format}')")
65-
self.run_cell('%matplotlib inline')
6666

6767
def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
6868
stdout=True, stderr=True, display=True):
@@ -78,13 +78,15 @@ def set_path(self, path):
7878
path = Path(path)
7979
if path.is_file(): path = path.parent
8080
self.run_cell(f"import sys; sys.path.insert(0, '{path.as_posix()}')")
81+
82+
def enable_gui(self, gui=None): pass
8183

82-
# %% ../nbs/02_shell.ipynb 22
84+
# %% ../nbs/02_shell.ipynb 23
8385
def format_exc(e):
8486
"Format exception `e` as a string"
8587
return ''.join(traceback.format_exception(type(e), e, e.__traceback__))
8688

87-
# %% ../nbs/02_shell.ipynb 23
89+
# %% ../nbs/02_shell.ipynb 24
8890
def _out_stream(text, name): return dict(name=name, output_type='stream', text=text.splitlines(True))
8991
def _out_exc(e):
9092
ename = type(e).__name__
@@ -114,7 +116,7 @@ def _out_nb(o, fmt):
114116
res.append(_mk_out(*fmt.format(r), 'execute_result'))
115117
return res
116118

117-
# %% ../nbs/02_shell.ipynb 24
119+
# %% ../nbs/02_shell.ipynb 25
118120
@patch
119121
def run(self:CaptureShell,
120122
code:str, # Python/IPython code to run
@@ -126,7 +128,7 @@ def run(self:CaptureShell,
126128
self.exc = res.exception
127129
return _out_nb(res, self.display_formatter)
128130

129-
# %% ../nbs/02_shell.ipynb 30
131+
# %% ../nbs/02_shell.ipynb 31
130132
def render_outputs(outputs, ansi_renderer=strip_ansi):
131133
def render_output(out):
132134
otype = out['output_type']
@@ -148,7 +150,7 @@ def render_output(out):
148150

149151
return '\n'.join(map(render_output, outputs))
150152

151-
# %% ../nbs/02_shell.ipynb 43
153+
# %% ../nbs/02_shell.ipynb 44
152154
@patch
153155
def cell(self:CaptureShell, cell, stdout=True, stderr=True):
154156
"Run `cell`, skipping if not code, and store outputs back in cell"
@@ -160,40 +162,40 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
160162
for o in outs:
161163
if 'execution_count' in o: cell['execution_count'] = o['execution_count']
162164

163-
# %% ../nbs/02_shell.ipynb 46
165+
# %% ../nbs/02_shell.ipynb 47
164166
def find_output(outp, # Output from `run`
165167
ot='execute_result' # Output_type to find
166168
):
167169
"Find first output of type `ot` in `CaptureShell.run` output"
168170
return first(o for o in outp if o['output_type']==ot)
169171

170-
# %% ../nbs/02_shell.ipynb 49
172+
# %% ../nbs/02_shell.ipynb 50
171173
def out_exec(outp):
172174
"Get data from execution result in `outp`."
173175
out = find_output(outp)
174176
if out: return '\n'.join(first(out['data'].values()))
175177

176-
# %% ../nbs/02_shell.ipynb 51
178+
# %% ../nbs/02_shell.ipynb 52
177179
def out_stream(outp):
178180
"Get text from stream in `outp`."
179181
out = find_output(outp, 'stream')
180182
if out: return ('\n'.join(out['text'])).strip()
181183

182-
# %% ../nbs/02_shell.ipynb 53
184+
# %% ../nbs/02_shell.ipynb 54
183185
def out_error(outp):
184186
"Get traceback from error in `outp`."
185187
out = find_output(outp, 'error')
186188
if out: return '\n'.join(out['traceback'])
187189

188-
# %% ../nbs/02_shell.ipynb 55
190+
# %% ../nbs/02_shell.ipynb 56
189191
def _false(o): return False
190192

191193
@patch
192194
def run_all(self:CaptureShell,
193195
nb, # A notebook read with `nbclient` or `read_nb`
194196
exc_stop:bool=False, # Stop on exceptions?
195-
preproc:Callable=_false, # Called before each cell is executed
196-
postproc:Callable=_false, # Called after each cell is executed
197+
preproc:callable=_false, # Called before each cell is executed
198+
postproc:callable=_false, # Called after each cell is executed
197199
inject_code:str|None=None, # Code to inject into a cell
198200
inject_idx:int=0 # Cell to replace with `inject_code`
199201
):
@@ -205,14 +207,14 @@ def run_all(self:CaptureShell,
205207
postproc(cell)
206208
if self.exc and exc_stop: raise self.exc from None
207209

208-
# %% ../nbs/02_shell.ipynb 69
210+
# %% ../nbs/02_shell.ipynb 70
209211
@patch
210212
def execute(self:CaptureShell,
211213
src:str|Path, # Notebook path to read from
212214
dest:str|None=None, # Notebook path to write to
213215
exc_stop:bool=False, # Stop on exceptions?
214-
preproc:Callable=_false, # Called before each cell is executed
215-
postproc:Callable=_false, # Called after each cell is executed
216+
preproc:callable=_false, # Called before each cell is executed
217+
postproc:callable=_false, # Called after each cell is executed
216218
inject_code:str|None=None, # Code to inject into a cell
217219
inject_path:str|Path|None=None, # Path to file containing code to inject into a cell
218220
inject_idx:int=0 # Cell to replace with `inject_code`
@@ -226,7 +228,7 @@ def execute(self:CaptureShell,
226228
inject_code=inject_code, inject_idx=inject_idx)
227229
if dest: write_nb(nb, dest)
228230

229-
# %% ../nbs/02_shell.ipynb 73
231+
# %% ../nbs/02_shell.ipynb 74
230232
@patch
231233
def prettytb(self:CaptureShell,
232234
fname:str|Path=None): # filename to print alongside the traceback
@@ -238,7 +240,7 @@ def prettytb(self:CaptureShell,
238240
fname_str = f' in {fname}' if fname else ''
239241
return f"{type(self.exc).__name__}{fname_str}:\n{_fence}\n{cell_str}\n"
240242

241-
# %% ../nbs/02_shell.ipynb 92
243+
# %% ../nbs/02_shell.ipynb 93
242244
@call_parse
243245
def exec_nb(
244246
src:str, # Notebook path to read from
@@ -252,7 +254,7 @@ def exec_nb(
252254
CaptureShell().execute(src, dest, exc_stop=exc_stop, inject_code=inject_code,
253255
inject_path=inject_path, inject_idx=inject_idx)
254256

255-
# %% ../nbs/02_shell.ipynb 95
257+
# %% ../nbs/02_shell.ipynb 96
256258
class SmartCompleter(IPCompleter):
257259
def __init__(self, shell, namespace=None, jedi=False):
258260
if namespace is None: namespace = shell.user_ns
@@ -272,7 +274,7 @@ def __call__(self, c):
272274
for o in self.completions(c, len(c))
273275
if o.type not in ('magic', 'path')]
274276

275-
# %% ../nbs/02_shell.ipynb 97
277+
# %% ../nbs/02_shell.ipynb 98
276278
@patch
277279
def complete(self:CaptureShell, c):
278280
if not hasattr(self, '_completer'): self._completer = SmartCompleter(self)

nbs/02_shell.ipynb

Lines changed: 52 additions & 41 deletions
Large diffs are not rendered by default.

nbs/nbdev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ website:
66
site-url: "https://fastai.github.io/execnb/"
77
description: "A description of your project"
88
repo-branch: master
9-
repo-url: "https://github.com/fastai/execnb/tree/master/"
9+
repo-url: "https://github.com/fastai/execnb/"

nbs/sidebar.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ website:
33
contents:
44
- index.ipynb
55
- 01_nbio.ipynb
6-
- 02_shell.ipynb
6+
- 02_shell.ipynb

0 commit comments

Comments
 (0)