|
47 | 47 |
|
48 | 48 |
|
49 | 49 | 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): |
52 | 52 | """ Execute a Jupyter Notebook programmatically. |
53 | 53 | Heavily inspired by https://github.com/tritemio/nbrun. |
54 | 54 |
|
@@ -95,6 +95,15 @@ def run_notebook(path, inputs=None, outputs=None, inputs_pos=1, working_dir = '. |
95 | 95 | Path to save the output ipynb file. |
96 | 96 | out_path_html : str, optional |
97 | 97 | 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. |
98 | 107 | execute_kwargs : dict, optional |
99 | 108 | Parameters of `:class:ExecutePreprocessor`. |
100 | 109 | add_timestamp : bool, optional |
@@ -208,19 +217,20 @@ def run_notebook(path, inputs=None, outputs=None, inputs_pos=1, working_dir = '. |
208 | 217 | with shelve.open(out_path_db) as notebook_db: |
209 | 218 | outputs_values = notebook_db.get('outputs', {}) |
210 | 219 |
|
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 | | - |
217 | 220 | # Check if something went wrong |
218 | 221 | failed, error_cell_num, traceback_message = extract_traceback(notebook=notebook) |
219 | 222 |
|
220 | 223 | if exec_failed: |
221 | 224 | failed = True |
222 | 225 | traceback_message += '\nNotebook execution failed\n' |
223 | 226 |
|
| 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 | + |
224 | 234 | # Prepare execution results: execution state, notebook outputs and error info (if exists) |
225 | 235 | if failed: |
226 | 236 | exec_res = {'failed': failed, 'failed cell number': error_cell_num, 'traceback': traceback_message} |
|
0 commit comments