31
31
from .nbio import _dict2obj
32
32
33
33
# %% auto 0
34
- __all__ = ['CaptureShell' , 'find_output' , 'out_exec' , 'out_stream' , 'out_error' , 'exec_nb' , 'SmartCompleter' ]
34
+ __all__ = ['CaptureShell' , 'format_exc' , ' find_output' , 'out_exec' , 'out_stream' , 'out_error' , 'exec_nb' , 'SmartCompleter' ]
35
35
36
- # %% ../nbs/02_shell.ipynb 7
36
+ # %% ../nbs/02_shell.ipynb 5
37
37
class _CustDisplayHook (DisplayHook ):
38
38
def write_output_prompt (self ): pass
39
39
def write_format_data (self , data , md_dict ): pass
@@ -45,18 +45,17 @@ def __repr__(self: ExecutionInfo): return f'cell: {self.raw_cell}; id: {self.cel
45
45
@patch
46
46
def __repr__ (self : ExecutionResult ): return f'result: { self .result } ; err: { self .error_in_exec } ; info: <{ self .info } >'
47
47
48
- # %% ../nbs/02_shell.ipynb 8
48
+ # %% ../nbs/02_shell.ipynb 6
49
49
class CaptureShell (InteractiveShell ):
50
50
displayhook_class = _CustDisplayHook
51
51
52
- def __init__ (self ,
53
- path :str | Path = None ): # Add `path` to python path
54
- "Execute the IPython/Jupyter source code"
52
+ def __init__ (self , path :str | Path = None ):
55
53
super ().__init__ ()
56
54
self .result ,self .exc = None ,None
57
55
if path : self .set_path (path )
58
- self .display_formatter .active = False
59
- self .run_cell ('%matplotlib inline' ) # Enable inline plotting
56
+ self .display_formatter .active = True
57
+ if not IN_NOTEBOOK : InteractiveShell ._instance = self
58
+ self .run_cell ('%matplotlib inline' )
60
59
61
60
def run_cell (self , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
62
61
stdout = True , stderr = True , display = True ):
@@ -73,12 +72,17 @@ def set_path(self, path):
73
72
if path .is_file (): path = path .parent
74
73
self .run_cell (f"import sys; sys.path.insert(0, '{ path .as_posix ()} ')" )
75
74
76
- # %% ../nbs/02_shell.ipynb 24
75
+ # %% ../nbs/02_shell.ipynb 22
76
+ def format_exc (e ):
77
+ "Format exception `e` as a string"
78
+ return '' .join (traceback .format_exception (type (e ), e , e .__traceback__ ))
79
+
80
+ # %% ../nbs/02_shell.ipynb 23
77
81
def _out_stream (text , name ): return dict (name = name , output_type = 'stream' , text = text .splitlines (True ))
78
82
def _out_exc (e ):
79
83
ename = type (e ).__name__
80
84
tb = traceback .extract_tb (e .__traceback__ )#.format()
81
- return dict (ename = str (ename ), evalue = str (e ), output_type = 'error' , traceback = tb )
85
+ return dict (ename = str (ename ), evalue = str (e ), output_type = 'error' , traceback = format_exc ( e ) )
82
86
83
87
def _format_mimedata (k , v ):
84
88
"Format mime-type keyed data consistently with Jupyter"
@@ -103,7 +107,7 @@ def _out_nb(o, fmt):
103
107
res .append (_mk_out (* fmt .format (r ), 'execute_result' ))
104
108
return res
105
109
106
- # %% ../nbs/02_shell.ipynb 25
110
+ # %% ../nbs/02_shell.ipynb 24
107
111
@patch
108
112
def run (self :CaptureShell ,
109
113
code :str , # Python/IPython code to run
@@ -115,7 +119,7 @@ def run(self:CaptureShell,
115
119
self .exc = res .exception
116
120
return _out_nb (res , self .display_formatter )
117
121
118
- # %% ../nbs/02_shell.ipynb 39
122
+ # %% ../nbs/02_shell.ipynb 38
119
123
@patch
120
124
def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
121
125
"Run `cell`, skipping if not code, and store outputs back in cell"
@@ -127,32 +131,32 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
127
131
for o in outs :
128
132
if 'execution_count' in o : cell ['execution_count' ] = o ['execution_count' ]
129
133
130
- # %% ../nbs/02_shell.ipynb 42
134
+ # %% ../nbs/02_shell.ipynb 41
131
135
def find_output (outp , # Output from `run`
132
136
ot = 'execute_result' # Output_type to find
133
137
):
134
138
"Find first output of type `ot` in `CaptureShell.run` output"
135
139
return first (o for o in outp if o ['output_type' ]== ot )
136
140
137
- # %% ../nbs/02_shell.ipynb 45
141
+ # %% ../nbs/02_shell.ipynb 44
138
142
def out_exec (outp ):
139
143
"Get data from execution result in `outp`."
140
144
out = find_output (outp )
141
145
if out : return '\n ' .join (first (out ['data' ].values ()))
142
146
143
- # %% ../nbs/02_shell.ipynb 47
147
+ # %% ../nbs/02_shell.ipynb 46
144
148
def out_stream (outp ):
145
149
"Get text from stream in `outp`."
146
150
out = find_output (outp , 'stream' )
147
151
if out : return ('\n ' .join (out ['text' ])).strip ()
148
152
149
- # %% ../nbs/02_shell.ipynb 49
153
+ # %% ../nbs/02_shell.ipynb 48
150
154
def out_error (outp ):
151
155
"Get traceback from error in `outp`."
152
156
out = find_output (outp , 'error' )
153
157
if out : return '\n ' .join (out ['traceback' ])
154
158
155
- # %% ../nbs/02_shell.ipynb 51
159
+ # %% ../nbs/02_shell.ipynb 50
156
160
def _false (o ): return False
157
161
158
162
@patch
@@ -172,7 +176,7 @@ def run_all(self:CaptureShell,
172
176
postproc (cell )
173
177
if self .exc and exc_stop : raise self .exc from None
174
178
175
- # %% ../nbs/02_shell.ipynb 65
179
+ # %% ../nbs/02_shell.ipynb 64
176
180
@patch
177
181
def execute (self :CaptureShell ,
178
182
src :str | Path , # Notebook path to read from
@@ -193,19 +197,19 @@ def execute(self:CaptureShell,
193
197
inject_code = inject_code , inject_idx = inject_idx )
194
198
if dest : write_nb (nb , dest )
195
199
196
- # %% ../nbs/02_shell.ipynb 69
200
+ # %% ../nbs/02_shell.ipynb 68
197
201
@patch
198
202
def prettytb (self :CaptureShell ,
199
203
fname :str | Path = None ): # filename to print alongside the traceback
200
204
"Show a pretty traceback for notebooks, optionally printing `fname`."
201
205
fname = fname if fname else self ._fname
202
206
_fence = '=' * 75
203
207
cell_intro_str = f"While Executing Cell #{ self ._cell_idx } :" if self ._cell_idx else "While Executing:"
204
- cell_str = f"\n { cell_intro_str } \n { self .exc } "
208
+ cell_str = f"\n { cell_intro_str } \n { format_exc ( self .exc ) } "
205
209
fname_str = f' in { fname } ' if fname else ''
206
210
return f"{ type (self .exc ).__name__ } { fname_str } :\n { _fence } \n { cell_str } \n "
207
211
208
- # %% ../nbs/02_shell.ipynb 88
212
+ # %% ../nbs/02_shell.ipynb 87
209
213
@call_parse
210
214
def exec_nb (
211
215
src :str , # Notebook path to read from
@@ -219,13 +223,12 @@ def exec_nb(
219
223
CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
220
224
inject_path = inject_path , inject_idx = inject_idx )
221
225
222
- # %% ../nbs/02_shell.ipynb 91
226
+ # %% ../nbs/02_shell.ipynb 90
223
227
class SmartCompleter (IPCompleter ):
224
- def __init__ (self , shell , namespace = None ):
228
+ def __init__ (self , shell , namespace = None , jedi = False ):
225
229
if namespace is None : namespace = shell .user_ns
226
230
super ().__init__ (shell , namespace )
227
- self .use_jedi = False
228
-
231
+ self .use_jedi = jedi
229
232
sdisp = StrDispatch ()
230
233
self .custom_completers = sdisp
231
234
import_disp = CommandChainDispatcher ()
0 commit comments