77
88from fastcore .utils import *
99from fastcore .script import call_parse
10+ from fastcore .ansi import ansi2html
1011
1112import multiprocessing ,types ,traceback
1213try :
@@ -125,11 +126,11 @@ def run(self:CaptureShell,
125126 return _out_nb (res , self .display_formatter )
126127
127128# %% ../nbs/02_shell.ipynb 30
128- def render_outputs (outputs ):
129+ def render_outputs (outputs , ansi_renderer = strip_ansi ):
129130 def render_output (out ):
130131 otype = out ['output_type' ]
131132 if otype == 'stream' :
132- txt = strip_ansi ('' .join (out ['text' ]))
133+ txt = ansi_renderer ('' .join (out ['text' ]))
133134 return f"<pre>{ txt } </pre>" if out ['name' ]== 'stdout' else f"<pre class='stderr'>{ txt } </pre>"
134135 elif otype in ('display_data' ,'execute_result' ):
135136 data = out ['data' ]
@@ -146,7 +147,7 @@ def render_output(out):
146147
147148 return '\n ' .join (map (render_output , outputs ))
148149
149- # %% ../nbs/02_shell.ipynb 41
150+ # %% ../nbs/02_shell.ipynb 43
150151@patch
151152def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
152153 "Run `cell`, skipping if not code, and store outputs back in cell"
@@ -158,32 +159,32 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
158159 for o in outs :
159160 if 'execution_count' in o : cell ['execution_count' ] = o ['execution_count' ]
160161
161- # %% ../nbs/02_shell.ipynb 44
162+ # %% ../nbs/02_shell.ipynb 46
162163def find_output (outp , # Output from `run`
163164 ot = 'execute_result' # Output_type to find
164165 ):
165166 "Find first output of type `ot` in `CaptureShell.run` output"
166167 return first (o for o in outp if o ['output_type' ]== ot )
167168
168- # %% ../nbs/02_shell.ipynb 47
169+ # %% ../nbs/02_shell.ipynb 49
169170def out_exec (outp ):
170171 "Get data from execution result in `outp`."
171172 out = find_output (outp )
172173 if out : return '\n ' .join (first (out ['data' ].values ()))
173174
174- # %% ../nbs/02_shell.ipynb 49
175+ # %% ../nbs/02_shell.ipynb 51
175176def out_stream (outp ):
176177 "Get text from stream in `outp`."
177178 out = find_output (outp , 'stream' )
178179 if out : return ('\n ' .join (out ['text' ])).strip ()
179180
180- # %% ../nbs/02_shell.ipynb 51
181+ # %% ../nbs/02_shell.ipynb 53
181182def out_error (outp ):
182183 "Get traceback from error in `outp`."
183184 out = find_output (outp , 'error' )
184185 if out : return '\n ' .join (out ['traceback' ])
185186
186- # %% ../nbs/02_shell.ipynb 53
187+ # %% ../nbs/02_shell.ipynb 55
187188def _false (o ): return False
188189
189190@patch
@@ -203,7 +204,7 @@ def run_all(self:CaptureShell,
203204 postproc (cell )
204205 if self .exc and exc_stop : raise self .exc from None
205206
206- # %% ../nbs/02_shell.ipynb 67
207+ # %% ../nbs/02_shell.ipynb 69
207208@patch
208209def execute (self :CaptureShell ,
209210 src :str | Path , # Notebook path to read from
@@ -224,7 +225,7 @@ def execute(self:CaptureShell,
224225 inject_code = inject_code , inject_idx = inject_idx )
225226 if dest : write_nb (nb , dest )
226227
227- # %% ../nbs/02_shell.ipynb 71
228+ # %% ../nbs/02_shell.ipynb 73
228229@patch
229230def prettytb (self :CaptureShell ,
230231 fname :str | Path = None ): # filename to print alongside the traceback
@@ -236,7 +237,7 @@ def prettytb(self:CaptureShell,
236237 fname_str = f' in { fname } ' if fname else ''
237238 return f"{ type (self .exc ).__name__ } { fname_str } :\n { _fence } \n { cell_str } \n "
238239
239- # %% ../nbs/02_shell.ipynb 90
240+ # %% ../nbs/02_shell.ipynb 92
240241@call_parse
241242def exec_nb (
242243 src :str , # Notebook path to read from
@@ -250,7 +251,7 @@ def exec_nb(
250251 CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
251252 inject_path = inject_path , inject_idx = inject_idx )
252253
253- # %% ../nbs/02_shell.ipynb 93
254+ # %% ../nbs/02_shell.ipynb 95
254255class SmartCompleter (IPCompleter ):
255256 def __init__ (self , shell , namespace = None , jedi = False ):
256257 if namespace is None : namespace = shell .user_ns
@@ -268,9 +269,9 @@ def __call__(self, c):
268269 with provisionalcompleter ():
269270 return [o .text .rpartition ('.' )[- 1 ]
270271 for o in self .completions (c , len (c ))
271- if o .type == '<unknown>' ]
272+ if o .type not in ( 'magic' , 'path' ) ]
272273
273- # %% ../nbs/02_shell.ipynb 95
274+ # %% ../nbs/02_shell.ipynb 97
274275@patch
275276def complete (self :CaptureShell , c ):
276277 if not hasattr (self , '_completer' ): self ._completer = SmartCompleter (self )
0 commit comments