From 5c32977db3af8fc7d05e036a3fac8176524473b6 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Wed, 19 Aug 2020 16:32:29 +0200 Subject: [PATCH] fix: stop executing cells when an error is encountered (replaces #530) Co-authored-by: martinRenou --- tests/app/syntax_error_test.py | 4 +++- tests/notebooks/syntax_error.ipynb | 9 +++++++++ voila/execute.py | 3 --- voila/handler.py | 9 ++++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/app/syntax_error_test.py b/tests/app/syntax_error_test.py index e395f7a74..77da5a84b 100644 --- a/tests/app/syntax_error_test.py +++ b/tests/app/syntax_error_test.py @@ -14,4 +14,6 @@ def voila_args(notebook_directory, voila_args_extra): async def test_syntax_error(capsys, http_server_client, syntax_error_notebook): response = await http_server_client.fetch(syntax_error_notebook) assert response.code == 200 - assert 'There was an error when executing cell' in response.body.decode('utf-8') + output = response.body.decode('utf-8') + assert 'There was an error when executing cell' in output + assert 'This should not be executed' not in output diff --git a/tests/notebooks/syntax_error.ipynb b/tests/notebooks/syntax_error.ipynb index a26cb8a30..042971835 100644 --- a/tests/notebooks/syntax_error.ipynb +++ b/tests/notebooks/syntax_error.ipynb @@ -8,6 +8,15 @@ "source": [ "this is a syntax error" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print('This should ' + 'not be executed')" + ] } ], "metadata": { diff --git a/voila/execute.py b/voila/execute.py index c272c0603..773efb57c 100644 --- a/voila/execute.py +++ b/voila/execute.py @@ -69,9 +69,6 @@ async def execute_cell(self, cell, resources, cell_index, store_history=True): self.log.error(e) self.show_code_cell_timeout(cell) raise e - except CellExecutionError as e: - self.log.error(e) - result = cell # Strip errors and traceback if not in debug mode if self.should_strip_error(): diff --git a/voila/handler.py b/voila/handler.py index 16ebdbf3f..78609f0df 100644 --- a/voila/handler.py +++ b/voila/handler.py @@ -20,11 +20,12 @@ import nbformat from nbconvert.preprocessors import ClearOutputPreprocessor +from nbclient.exceptions import CellExecutionError from nbclient.util import ensure_async from tornado.httputil import split_host_and_port from ._version import __version__ -from .execute import VoilaExecutor +from .execute import VoilaExecutor, strip_code_cell_warnings from .exporter import VoilaExporter @@ -182,6 +183,12 @@ async def _jinja_cell_generator(self, nb, kernel_id): except TimeoutError: output_cell = input_cell break + except CellExecutionError as e: + if self.executor.should_strip_error(): + strip_code_cell_warnings(input_cell) + self.executor.strip_code_cell_errors(input_cell) + output_cell = input_cell + break except Exception as e: self.log.exception('Error at server while executing cell: %r', input_cell) output_cell = nbformat.v4.new_code_cell()