77
88from fastcore .utils import *
99from fastcore .script import call_parse
10-
10+ from fastcore . ansi import ansi2html
1111import multiprocessing ,types ,traceback
1212try :
1313 if sys .platform == 'darwin' : multiprocessing .set_start_method ("fork" )
@@ -125,11 +125,11 @@ def run(self:CaptureShell,
125125 return _out_nb (res , self .display_formatter )
126126
127127# %% ../nbs/02_shell.ipynb 30
128- def render_outputs (outputs ):
128+ def render_outputs (outputs , ansi_renderer = strip_ansi ):
129129 def render_output (out ):
130130 otype = out ['output_type' ]
131131 if otype == 'stream' :
132- txt = strip_ansi ('' .join (out ['text' ]))
132+ txt = ansi_renderer ('' .join (out ['text' ]))
133133 return f"<pre>{ txt } </pre>" if out ['name' ]== 'stdout' else f"<pre class='stderr'>{ txt } </pre>"
134134 elif otype in ('display_data' ,'execute_result' ):
135135 data = out ['data' ]
@@ -146,7 +146,7 @@ def render_output(out):
146146
147147 return '\n ' .join (map (render_output , outputs ))
148148
149- # %% ../nbs/02_shell.ipynb 41
149+ # %% ../nbs/02_shell.ipynb 43
150150@patch
151151def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
152152 "Run `cell`, skipping if not code, and store outputs back in cell"
@@ -158,32 +158,32 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
158158 for o in outs :
159159 if 'execution_count' in o : cell ['execution_count' ] = o ['execution_count' ]
160160
161- # %% ../nbs/02_shell.ipynb 44
161+ # %% ../nbs/02_shell.ipynb 46
162162def find_output (outp , # Output from `run`
163163 ot = 'execute_result' # Output_type to find
164164 ):
165165 "Find first output of type `ot` in `CaptureShell.run` output"
166166 return first (o for o in outp if o ['output_type' ]== ot )
167167
168- # %% ../nbs/02_shell.ipynb 47
168+ # %% ../nbs/02_shell.ipynb 49
169169def out_exec (outp ):
170170 "Get data from execution result in `outp`."
171171 out = find_output (outp )
172172 if out : return '\n ' .join (first (out ['data' ].values ()))
173173
174- # %% ../nbs/02_shell.ipynb 49
174+ # %% ../nbs/02_shell.ipynb 51
175175def out_stream (outp ):
176176 "Get text from stream in `outp`."
177177 out = find_output (outp , 'stream' )
178178 if out : return ('\n ' .join (out ['text' ])).strip ()
179179
180- # %% ../nbs/02_shell.ipynb 51
180+ # %% ../nbs/02_shell.ipynb 53
181181def out_error (outp ):
182182 "Get traceback from error in `outp`."
183183 out = find_output (outp , 'error' )
184184 if out : return '\n ' .join (out ['traceback' ])
185185
186- # %% ../nbs/02_shell.ipynb 53
186+ # %% ../nbs/02_shell.ipynb 55
187187def _false (o ): return False
188188
189189@patch
@@ -203,7 +203,7 @@ def run_all(self:CaptureShell,
203203 postproc (cell )
204204 if self .exc and exc_stop : raise self .exc from None
205205
206- # %% ../nbs/02_shell.ipynb 67
206+ # %% ../nbs/02_shell.ipynb 69
207207@patch
208208def execute (self :CaptureShell ,
209209 src :str | Path , # Notebook path to read from
@@ -224,7 +224,7 @@ def execute(self:CaptureShell,
224224 inject_code = inject_code , inject_idx = inject_idx )
225225 if dest : write_nb (nb , dest )
226226
227- # %% ../nbs/02_shell.ipynb 71
227+ # %% ../nbs/02_shell.ipynb 73
228228@patch
229229def prettytb (self :CaptureShell ,
230230 fname :str | Path = None ): # filename to print alongside the traceback
@@ -236,7 +236,7 @@ def prettytb(self:CaptureShell,
236236 fname_str = f' in { fname } ' if fname else ''
237237 return f"{ type (self .exc ).__name__ } { fname_str } :\n { _fence } \n { cell_str } \n "
238238
239- # %% ../nbs/02_shell.ipynb 90
239+ # %% ../nbs/02_shell.ipynb 92
240240@call_parse
241241def exec_nb (
242242 src :str , # Notebook path to read from
@@ -250,7 +250,7 @@ def exec_nb(
250250 CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
251251 inject_path = inject_path , inject_idx = inject_idx )
252252
253- # %% ../nbs/02_shell.ipynb 93
253+ # %% ../nbs/02_shell.ipynb 95
254254class SmartCompleter (IPCompleter ):
255255 def __init__ (self , shell , namespace = None , jedi = False ):
256256 if namespace is None : namespace = shell .user_ns
@@ -270,7 +270,7 @@ def __call__(self, c):
270270 for o in self .completions (c , len (c ))
271271 if o .type == '<unknown>' ]
272272
273- # %% ../nbs/02_shell.ipynb 95
273+ # %% ../nbs/02_shell.ipynb 97
274274@patch
275275def complete (self :CaptureShell , c ):
276276 if not hasattr (self , '_completer' ): self ._completer = SmartCompleter (self )
0 commit comments