@@ -67,10 +67,10 @@ def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeo
6767 self ._run (f"set_matplotlib_formats('{ mpl_format } ')" )
6868
6969 def _run (self , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
70- stdout = True , stderr = True , display = True ):
70+ stdout = True , stderr = True , display = True , verbose = False ):
7171 # TODO what if there's a comment?
7272 semic = raw_cell .rstrip ().endswith (';' )
73- with capture_output (display = display , stdout = stdout , stderr = stderr ) as c :
73+ with capture_output (display = display , stdout = stdout and not verbose , stderr = stderr and not verbose ) as c :
7474 result = super ().run_cell (raw_cell , store_history , silent , shell_futures = shell_futures , cell_id = cell_id )
7575 return AttrDict (result = result , stdout = '' if semic else c .stdout , stderr = c .stderr ,
7676 display_objects = c .outputs , exception = result .error_in_exec , quiet = semic )
@@ -86,14 +86,14 @@ def enable_gui(self, gui=None): pass
8686# %% ../nbs/02_shell.ipynb
8787@patch
8888def run_cell (self :CaptureShell , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
89- stdout = True , stderr = True , display = True , timeout = None ):
89+ stdout = True , stderr = True , display = True , timeout = None , verbose = False ):
9090 if not timeout : timeout = self .timeout
9191 if timeout :
9292 def handler (* args ): raise TimeoutError ()
9393 signal .signal (signal .SIGALRM , handler )
9494 signal .alarm (timeout )
9595 try : return self ._run (raw_cell , store_history , silent , shell_futures , cell_id = cell_id ,
96- stdout = stdout , stderr = stderr , display = display )
96+ stdout = stdout , stderr = stderr , display = display , verbose = verbose )
9797 finally :
9898 if timeout : signal .alarm (0 )
9999
@@ -144,9 +144,10 @@ def run(self:CaptureShell,
144144 code :str , # Python/IPython code to run
145145 stdout = True , # Capture stdout and save as output?
146146 stderr = True , # Capture stderr and save as output?
147- timeout :Optional [int ]= None ): # Shell command will time out after {timeout} seconds
147+ timeout :Optional [int ]= None , # Shell command will time out after {timeout} seconds
148+ verbose :bool = False ): # Show stdout/stderr during execution
148149 "Run `code`, returning a list of all outputs in Jupyter notebook format"
149- res = self .run_cell (code , stdout = stdout , stderr = stderr , timeout = timeout )
150+ res = self .run_cell (code , stdout = stdout , stderr = stderr , timeout = timeout , verbose = verbose )
150151 self .result = res .result .result
151152 self .exc = res .exception
152153 return _out_nb (res , self .display_formatter )
@@ -157,8 +158,9 @@ async def run_async(self:CaptureShell,
157158 code : str , # Python/IPython code to run
158159 stdout = True , # Capture stdout and save as output?
159160 stderr = True , # Capture stderr and save as output?
160- timeout :Optional [int ]= None ): # Shell command will time out after {timeout} seconds
161- return self .run (code , stdout = stdout , stderr = stderr , timeout = timeout )
161+ timeout :Optional [int ]= None , # Shell command will time out after {timeout} seconds
162+ verbose :bool = False ): # Show stdout/stderr during execution
163+ return self .run (code , stdout = stdout , stderr = stderr , timeout = timeout , verbose = verbose )
162164
163165# %% ../nbs/02_shell.ipynb
164166def _pre (s , xtra = '' ): return f"<pre { xtra } ><code>{ escape (s )} </code></pre>"
@@ -197,11 +199,11 @@ def render_output(out):
197199
198200# %% ../nbs/02_shell.ipynb
199201@patch
200- def cell (self :CaptureShell , cell , stdout = True , stderr = True ):
202+ def cell (self :CaptureShell , cell , stdout = True , stderr = True , verbose = False ):
201203 "Run `cell`, skipping if not code, and store outputs back in cell"
202204 if cell .cell_type != 'code' : return
203205 self ._cell_idx = cell .idx_ + 1
204- outs = self .run (cell .source )
206+ outs = self .run (cell .source , verbose = verbose )
205207 if outs : cell .outputs = _dict2obj (outs )
206208
207209# %% ../nbs/02_shell.ipynb
@@ -239,13 +241,14 @@ def run_all(self:CaptureShell,
239241 preproc :callable = _false , # Called before each cell is executed
240242 postproc :callable = _false , # Called after each cell is executed
241243 inject_code :str | None = None , # Code to inject into a cell
242- inject_idx :int = 0 # Cell to replace with `inject_code`
244+ inject_idx :int = 0 , # Cell to replace with `inject_code`
245+ verbose :bool = False # Show stdout/stderr during execution
243246 ):
244247 "Run all cells in `nb`, stopping at first exception if `exc_stop`"
245248 if inject_code is not None : nb .cells [inject_idx ].source = inject_code
246249 for cell in nb .cells :
247250 if not preproc (cell ):
248- self .cell (cell )
251+ self .cell (cell , verbose = verbose )
249252 postproc (cell )
250253 if self .exc and exc_stop : raise self .exc from None
251254
@@ -259,15 +262,16 @@ def execute(self:CaptureShell,
259262 postproc :callable = _false , # Called after each cell is executed
260263 inject_code :str | None = None , # Code to inject into a cell
261264 inject_path :str | Path | None = None , # Path to file containing code to inject into a cell
262- inject_idx :int = 0 # Cell to replace with `inject_code`
265+ inject_idx :int = 0 , # Cell to replace with `inject_code`
266+ verbose :bool = False # Show stdout/stderr during execution
263267):
264268 "Execute notebook from `src` and save with outputs to `dest"
265269 nb = read_nb (src )
266270 self ._fname = src
267271 self .set_path (Path (src ).parent .resolve ())
268272 if inject_path is not None : inject_code = Path (inject_path ).read_text ()
269273 self .run_all (nb , exc_stop = exc_stop , preproc = preproc , postproc = postproc ,
270- inject_code = inject_code , inject_idx = inject_idx )
274+ inject_code = inject_code , inject_idx = inject_idx , verbose = verbose )
271275 if dest : write_nb (nb , dest )
272276
273277# %% ../nbs/02_shell.ipynb
@@ -290,11 +294,12 @@ def exec_nb(
290294 exc_stop :bool = False , # Stop on exceptions?
291295 inject_code :str = None , # Code to inject into a cell
292296 inject_path :str = None , # Path to file containing code to inject into a cell
293- inject_idx :int = 0 # Cell to replace with `inject_code`
297+ inject_idx :int = 0 , # Cell to replace with `inject_code`
298+ verbose :bool = False # Show stdout/stderr during execution
294299):
295300 "Execute notebook from `src` and save with outputs to `dest`"
296301 CaptureShell ().execute (src , dest , exc_stop = exc_stop , inject_code = inject_code ,
297- inject_path = inject_path , inject_idx = inject_idx )
302+ inject_path = inject_path , inject_idx = inject_idx , verbose = verbose )
298303
299304# %% ../nbs/02_shell.ipynb
300305class SmartCompleter (IPCompleter ):
0 commit comments