Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --save-on-error flag, closes #626 #842

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def validate(self, obj, value):
"(the default behaviour is to abort conversion). This flag "
"is only relevant if '--execute' was specified, too.")
),
'save-on-error' : (
{'ExecutePreprocessor' : {'save_on_error' : True}},
("Saves the notebook even if one cell throws "
"an error. The error message is shown in the cell output."
"(the default behaviour is to not save the notebook on an error). This flag "
"is only relevant if '--execute' was specified, too.")
),
'stdin' : (
{'NbConvertApp' : {
'from_stdin' : True,
Expand Down
15 changes: 15 additions & 0 deletions nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ class ExecutePreprocessor(Preprocessor):
)
).tag(config=True)

save_on_error = Bool(False,
help=dedent(
"""
If `False` (default), when a cell raises an error no output is saved.
If `True`, the output of the notebook will be saved when an error occors.
"""
)
).tag(config=True)

extra_arguments = List(Unicode())

kernel_name = Unicode('',
Expand Down Expand Up @@ -276,6 +285,12 @@ def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs):

try:
nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
except CellExecutionError:
if self.save_on_error:
pass
else:
raise

finally:
self.kc.stop_channels()
self.km.shutdown_kernel(now=self.shutdown_kernel == 'immediate')
Expand Down
10 changes: 10 additions & 0 deletions nbconvert/preprocessors/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ def test_allow_errors(self):
else:
assert u"# üñîçø∂é".encode('utf8', 'replace') in str(exc.value)

def test_save_on_error(self):
"""
Check that conversion halts if ``allow_errors`` is False.
"""
current_dir = os.path.dirname(__file__)
filename = os.path.join(current_dir, 'files', 'Skip Exceptions.ipynb')
res = self.build_resources()
res['metadata']['path'] = os.path.dirname(filename)
self.run_notebook(filename, dict(save_on_error=True), res)

def test_force_raise_errors(self):
"""
Check that conversion halts if the ``force_raise_errors`` traitlet on
Expand Down