16
16
from IPython .core .interactiveshell import InteractiveShell , ExecutionInfo , ExecutionResult
17
17
from IPython .core .displayhook import DisplayHook
18
18
from IPython .utils .capture import capture_output
19
+ from IPython .utils .text import strip_ansi
19
20
from IPython .core .completer import IPCompleter ,provisionalcompleter
20
21
from IPython .core .hooks import CommandChainDispatcher
21
22
from IPython .core .completerlib import module_completer
@@ -123,12 +124,12 @@ def run(self:CaptureShell,
123
124
self .exc = res .exception
124
125
return _out_nb (res , self .display_formatter )
125
126
126
- # %% ../nbs/02_shell.ipynb 29
127
+ # %% ../nbs/02_shell.ipynb 30
127
128
def render_outputs (outputs ):
128
129
def render_output (out ):
129
130
otype = out ['output_type' ]
130
131
if otype == 'stream' :
131
- txt = '' .join (out ['text' ])
132
+ txt = strip_ansi ( '' .join (out ['text' ]) )
132
133
return f"<pre>{ txt } </pre>" if out ['name' ]== 'stdout' else f"<pre class='stderr'>{ txt } </pre>"
133
134
elif otype in ('display_data' ,'execute_result' ):
134
135
data = out ['data' ]
@@ -141,13 +142,11 @@ def render_output(out):
141
142
if d := _g ('image/png' ): return f'<img src="data:image/png;base64,{ d } "/>'
142
143
if d := _g ('text/latex' ): return f'<div class="math">${ d } $</div>'
143
144
if d := _g ('text/plain' ): return f"<pre>{ escape (d )} </pre>"
144
- elif otype == 'error' :
145
- return f"<pre class='error'>{ out ['ename' ]} : { out ['evalue' ]} \n { '' .join (out ['traceback' ])} </pre>"
146
145
return ''
147
146
148
147
return '\n ' .join (map (render_output , outputs ))
149
148
150
- # %% ../nbs/02_shell.ipynb 40
149
+ # %% ../nbs/02_shell.ipynb 41
151
150
@patch
152
151
def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
153
152
"Run `cell`, skipping if not code, and store outputs back in cell"
@@ -159,32 +158,32 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
159
158
for o in outs :
160
159
if 'execution_count' in o : cell ['execution_count' ] = o ['execution_count' ]
161
160
162
- # %% ../nbs/02_shell.ipynb 43
161
+ # %% ../nbs/02_shell.ipynb 44
163
162
def find_output (outp , # Output from `run`
164
163
ot = 'execute_result' # Output_type to find
165
164
):
166
165
"Find first output of type `ot` in `CaptureShell.run` output"
167
166
return first (o for o in outp if o ['output_type' ]== ot )
168
167
169
- # %% ../nbs/02_shell.ipynb 46
168
+ # %% ../nbs/02_shell.ipynb 47
170
169
def out_exec (outp ):
171
170
"Get data from execution result in `outp`."
172
171
out = find_output (outp )
173
172
if out : return '\n ' .join (first (out ['data' ].values ()))
174
173
175
- # %% ../nbs/02_shell.ipynb 48
174
+ # %% ../nbs/02_shell.ipynb 49
176
175
def out_stream (outp ):
177
176
"Get text from stream in `outp`."
178
177
out = find_output (outp , 'stream' )
179
178
if out : return ('\n ' .join (out ['text' ])).strip ()
180
179
181
- # %% ../nbs/02_shell.ipynb 50
180
+ # %% ../nbs/02_shell.ipynb 51
182
181
def out_error (outp ):
183
182
"Get traceback from error in `outp`."
184
183
out = find_output (outp , 'error' )
185
184
if out : return '\n ' .join (out ['traceback' ])
186
185
187
- # %% ../nbs/02_shell.ipynb 52
186
+ # %% ../nbs/02_shell.ipynb 53
188
187
def _false (o ): return False
189
188
190
189
@patch
@@ -204,7 +203,7 @@ def run_all(self:CaptureShell,
204
203
postproc (cell )
205
204
if self .exc and exc_stop : raise self .exc from None
206
205
207
- # %% ../nbs/02_shell.ipynb 66
206
+ # %% ../nbs/02_shell.ipynb 67
208
207
@patch
209
208
def execute (self :CaptureShell ,
210
209
src :str | Path , # Notebook path to read from
@@ -225,7 +224,7 @@ def execute(self:CaptureShell,
225
224
inject_code = inject_code , inject_idx = inject_idx )
226
225
if dest : write_nb (nb , dest )
227
226
228
- # %% ../nbs/02_shell.ipynb 70
227
+ # %% ../nbs/02_shell.ipynb 71
229
228
@patch
230
229
def prettytb (self :CaptureShell ,
231
230
fname :str | Path = None ): # filename to print alongside the traceback
@@ -237,7 +236,7 @@ def prettytb(self:CaptureShell,
237
236
fname_str = f' in { fname } ' if fname else ''
238
237
return f"{ type (self .exc ).__name__ } { fname_str } :\n { _fence } \n { cell_str } \n "
239
238
240
- # %% ../nbs/02_shell.ipynb 89
239
+ # %% ../nbs/02_shell.ipynb 90
241
240
@call_parse
242
241
def exec_nb (
243
242
src :str , # Notebook path to read from
@@ -251,7 +250,7 @@ def exec_nb(
251
250
CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
252
251
inject_path = inject_path , inject_idx = inject_idx )
253
252
254
- # %% ../nbs/02_shell.ipynb 92
253
+ # %% ../nbs/02_shell.ipynb 93
255
254
class SmartCompleter (IPCompleter ):
256
255
def __init__ (self , shell , namespace = None , jedi = False ):
257
256
if namespace is None : namespace = shell .user_ns
@@ -271,7 +270,7 @@ def __call__(self, c):
271
270
for o in self .completions (c , len (c ))
272
271
if o .type == '<unknown>' ]
273
272
274
- # %% ../nbs/02_shell.ipynb 94
273
+ # %% ../nbs/02_shell.ipynb 95
275
274
@patch
276
275
def complete (self :CaptureShell , c ):
277
276
if not hasattr (self , '_completer' ): self ._completer = SmartCompleter (self )
0 commit comments