3131from .nbio import _dict2obj
3232
3333# %% 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' ]
3535
36- # %% ../nbs/02_shell.ipynb 7
36+ # %% ../nbs/02_shell.ipynb 5
3737class _CustDisplayHook (DisplayHook ):
3838 def write_output_prompt (self ): pass
3939 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
4545@patch
4646def __repr__ (self : ExecutionResult ): return f'result: { self .result } ; err: { self .error_in_exec } ; info: <{ self .info } >'
4747
48- # %% ../nbs/02_shell.ipynb 8
48+ # %% ../nbs/02_shell.ipynb 6
4949class CaptureShell (InteractiveShell ):
5050 displayhook_class = _CustDisplayHook
5151
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 ):
5553 super ().__init__ ()
5654 self .result ,self .exc = None ,None
5755 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' )
6059
6160 def run_cell (self , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
6261 stdout = True , stderr = True , display = True ):
@@ -73,12 +72,17 @@ def set_path(self, path):
7372 if path .is_file (): path = path .parent
7473 self .run_cell (f"import sys; sys.path.insert(0, '{ path .as_posix ()} ')" )
7574
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
7781def _out_stream (text , name ): return dict (name = name , output_type = 'stream' , text = text .splitlines (True ))
7882def _out_exc (e ):
7983 ename = type (e ).__name__
8084 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 ) )
8286
8387def _format_mimedata (k , v ):
8488 "Format mime-type keyed data consistently with Jupyter"
@@ -103,7 +107,7 @@ def _out_nb(o, fmt):
103107 res .append (_mk_out (* fmt .format (r ), 'execute_result' ))
104108 return res
105109
106- # %% ../nbs/02_shell.ipynb 25
110+ # %% ../nbs/02_shell.ipynb 24
107111@patch
108112def run (self :CaptureShell ,
109113 code :str , # Python/IPython code to run
@@ -115,7 +119,7 @@ def run(self:CaptureShell,
115119 self .exc = res .exception
116120 return _out_nb (res , self .display_formatter )
117121
118- # %% ../nbs/02_shell.ipynb 39
122+ # %% ../nbs/02_shell.ipynb 38
119123@patch
120124def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
121125 "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):
127131 for o in outs :
128132 if 'execution_count' in o : cell ['execution_count' ] = o ['execution_count' ]
129133
130- # %% ../nbs/02_shell.ipynb 42
134+ # %% ../nbs/02_shell.ipynb 41
131135def find_output (outp , # Output from `run`
132136 ot = 'execute_result' # Output_type to find
133137 ):
134138 "Find first output of type `ot` in `CaptureShell.run` output"
135139 return first (o for o in outp if o ['output_type' ]== ot )
136140
137- # %% ../nbs/02_shell.ipynb 45
141+ # %% ../nbs/02_shell.ipynb 44
138142def out_exec (outp ):
139143 "Get data from execution result in `outp`."
140144 out = find_output (outp )
141145 if out : return '\n ' .join (first (out ['data' ].values ()))
142146
143- # %% ../nbs/02_shell.ipynb 47
147+ # %% ../nbs/02_shell.ipynb 46
144148def out_stream (outp ):
145149 "Get text from stream in `outp`."
146150 out = find_output (outp , 'stream' )
147151 if out : return ('\n ' .join (out ['text' ])).strip ()
148152
149- # %% ../nbs/02_shell.ipynb 49
153+ # %% ../nbs/02_shell.ipynb 48
150154def out_error (outp ):
151155 "Get traceback from error in `outp`."
152156 out = find_output (outp , 'error' )
153157 if out : return '\n ' .join (out ['traceback' ])
154158
155- # %% ../nbs/02_shell.ipynb 51
159+ # %% ../nbs/02_shell.ipynb 50
156160def _false (o ): return False
157161
158162@patch
@@ -172,7 +176,7 @@ def run_all(self:CaptureShell,
172176 postproc (cell )
173177 if self .exc and exc_stop : raise self .exc from None
174178
175- # %% ../nbs/02_shell.ipynb 65
179+ # %% ../nbs/02_shell.ipynb 64
176180@patch
177181def execute (self :CaptureShell ,
178182 src :str | Path , # Notebook path to read from
@@ -193,19 +197,19 @@ def execute(self:CaptureShell,
193197 inject_code = inject_code , inject_idx = inject_idx )
194198 if dest : write_nb (nb , dest )
195199
196- # %% ../nbs/02_shell.ipynb 69
200+ # %% ../nbs/02_shell.ipynb 68
197201@patch
198202def prettytb (self :CaptureShell ,
199203 fname :str | Path = None ): # filename to print alongside the traceback
200204 "Show a pretty traceback for notebooks, optionally printing `fname`."
201205 fname = fname if fname else self ._fname
202206 _fence = '=' * 75
203207 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 ) } "
205209 fname_str = f' in { fname } ' if fname else ''
206210 return f"{ type (self .exc ).__name__ } { fname_str } :\n { _fence } \n { cell_str } \n "
207211
208- # %% ../nbs/02_shell.ipynb 88
212+ # %% ../nbs/02_shell.ipynb 87
209213@call_parse
210214def exec_nb (
211215 src :str , # Notebook path to read from
@@ -219,13 +223,12 @@ def exec_nb(
219223 CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
220224 inject_path = inject_path , inject_idx = inject_idx )
221225
222- # %% ../nbs/02_shell.ipynb 91
226+ # %% ../nbs/02_shell.ipynb 90
223227class SmartCompleter (IPCompleter ):
224- def __init__ (self , shell , namespace = None ):
228+ def __init__ (self , shell , namespace = None , jedi = False ):
225229 if namespace is None : namespace = shell .user_ns
226230 super ().__init__ (shell , namespace )
227- self .use_jedi = False
228-
231+ self .use_jedi = jedi
229232 sdisp = StrDispatch ()
230233 self .custom_completers = sdisp
231234 import_disp = CommandChainDispatcher ()
0 commit comments