@@ -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
8385def 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
8890def _out_stream (text , name ): return dict (name = name , output_type = 'stream' , text = text .splitlines (True ))
8991def _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
119121def 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
130132def 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
153155def 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
164166def 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
171173def 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
177179def 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
183185def 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
189191def _false (o ): return False
190192
191193@patch
192194def 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
210212def 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
231233def 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
243245def 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
256258class 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
277279def complete (self :CaptureShell , c ):
278280 if not hasattr (self , '_completer' ): self ._completer = SmartCompleter (self )
0 commit comments