Skip to content

Commit d25e33a

Browse files
add remove_db option
1 parent d26c21c commit d25e33a

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

nbtools/run_notebook.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747

4848

4949
def run_notebook(path, inputs=None, outputs=None, inputs_pos=1, working_dir = './', execute_kwargs=None,
50-
out_path_db=None, out_path_ipynb=None, out_path_html=None, add_timestamp=True, hide_code_cells=False,
51-
display_links=True, raise_exception=False, return_notebook=False):
50+
out_path_db=None, out_path_ipynb=None, out_path_html=None, remove_db='always', add_timestamp=True,
51+
hide_code_cells=False, display_links=True, raise_exception=False, return_notebook=False):
5252
""" Execute a Jupyter Notebook programmatically.
5353
Heavily inspired by https://github.com/tritemio/nbrun.
5454
@@ -95,6 +95,15 @@ def run_notebook(path, inputs=None, outputs=None, inputs_pos=1, working_dir = '.
9595
Path to save the output ipynb file.
9696
out_path_html : str, optional
9797
Path to save the output html file.
98+
remove_db : str, optional
99+
Whether to remove shelve database after notebook execution.
100+
Possible options are: 'always', 'not_failed_case' or 'never'.
101+
If 'always', then remove the database after notebook execution.
102+
If 'not_failed_case', then remove the database if there wasn't any execution failure.
103+
If 'never', then don't remove the database after notebook execution.
104+
Running `:meth:run_notebook` with 'not_failed_case' or 'never' option helps to reproduce failures
105+
in the `out_path_ipynb` notebook: it will take passed inputs from the saved shelve database.
106+
Note, that database exists only if inputs and/or outputs are provided.
98107
execute_kwargs : dict, optional
99108
Parameters of `:class:ExecutePreprocessor`.
100109
add_timestamp : bool, optional
@@ -208,19 +217,20 @@ def run_notebook(path, inputs=None, outputs=None, inputs_pos=1, working_dir = '.
208217
with shelve.open(out_path_db) as notebook_db:
209218
outputs_values = notebook_db.get('outputs', {})
210219

211-
if out_path_db is not None:
212-
db_paths = glob(out_path_db + '*')
213-
214-
for path_ in db_paths:
215-
os.remove(path_)
216-
217220
# Check if something went wrong
218221
failed, error_cell_num, traceback_message = extract_traceback(notebook=notebook)
219222

220223
if exec_failed:
221224
failed = True
222225
traceback_message += '\nNotebook execution failed\n'
223226

227+
# Remove database
228+
if out_path_db is not None and (remove_db == 'always' or (remove_db == 'not_failed_case' and not failed)):
229+
db_paths = glob(out_path_db + '*')
230+
231+
for path_ in db_paths:
232+
os.remove(path_)
233+
224234
# Prepare execution results: execution state, notebook outputs and error info (if exists)
225235
if failed:
226236
exec_res = {'failed': failed, 'failed cell number': error_cell_num, 'traceback': traceback_message}

0 commit comments

Comments
 (0)