From 0c01e2b7f8025fd60e4c9d2b37b01bfa68e69775 Mon Sep 17 00:00:00 2001 From: Mehmet Bektas Date: Wed, 3 Apr 2024 11:42:12 -0700 Subject: [PATCH 1/5] bring back strip_color and remove ANSI color codes from exception traceback --- papermill/exceptions.py | 21 ++++++++++++++++++++- papermill/tests/test_execute.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/papermill/exceptions.py b/papermill/exceptions.py index 95c2d1ef..b61eeb8b 100644 --- a/papermill/exceptions.py +++ b/papermill/exceptions.py @@ -1,3 +1,6 @@ +import re + + class AwsError(Exception): """Raised when an AWS Exception is encountered.""" @@ -35,7 +38,7 @@ def __str__(self): # provide the same result as was produced in the past. message = f"\n{75 * '-'}\n" message += f'Exception encountered at "In [{self.exec_count}]":\n' - message += "\n".join(self.traceback) + message += strip_color("\n".join(self.traceback)) message += "\n" return message @@ -75,3 +78,19 @@ def missing_dep(): ) return missing_dep + +# copied from https://github.com/jonathaneunice/ansiwrap/blob/master/ansiwrap/core.py +# papermill used to use strip_color from ansiwrap package, but removed due to dependency +# resolution issues (ref: https://github.com/nteract/papermill/pull/681) +def strip_color(s): + """ + Remove ANSI color/style sequences from a string. The set of all + possibly ANSI sequences is large, so does not try to strip every + possible one. But does strip some outliers seen not just in text + generated by this module, but by other ANSI colorizers in the wild. + Those include `\x1b[K` (aka EL or erase to end of line) and `\x1b[m` + a terse version of the more common `\x1b[0m`. + """ + ANSIRE = re.compile('\x1b\\[(K|.*?m)') + + return ANSIRE.sub('', s) diff --git a/papermill/tests/test_execute.py b/papermill/tests/test_execute.py index 705d418c..45b12454 100644 --- a/papermill/tests/test_execute.py +++ b/papermill/tests/test_execute.py @@ -11,7 +11,7 @@ from nbformat import validate from .. import engines, translators -from ..exceptions import PapermillExecutionError +from ..exceptions import PapermillExecutionError, strip_color from ..execute import execute_notebook from ..iorw import load_notebook_node from ..log import logger @@ -429,3 +429,33 @@ def test_notebook_node_input(self): execute_notebook(input_nb, self.result_path, {'msg': 'Hello'}) test_nb = nbformat.read(self.result_path, as_version=4) self.assertEqual(test_nb.metadata.papermill.parameters, {'msg': 'Hello'}) + +class TestOutputFormatting(unittest.TestCase): + def setUp(self): + self.test_dir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.test_dir) + + def test_output_formatting(self): + notebook_name = 'sysexit1.ipynb' + result_path = os.path.join(self.test_dir, f'output_{notebook_name}') + try: + execute_notebook(get_notebook_path(notebook_name), result_path) + # exception should be thrown by now + self.assertFalse(True) + except PapermillExecutionError as ex: + self.assertEqual(ex.traceback[1], "\x1b[0;31mSystemExit\x1b[0m\x1b[0;31m:\x1b[0m 1\n") + self.assertEqual(strip_color(ex.traceback[1]), "SystemExit: 1\n") + + nb = load_notebook_node(result_path) + self.assertEqual(nb.cells[0].cell_type, "markdown") + self.assertRegex(nb.cells[0].source, r'^$') + self.assertEqual(nb.cells[1].execution_count, 1) + + self.assertEqual(nb.cells[2].cell_type, "markdown") + self.assertRegex(nb.cells[2].source, '') + self.assertEqual(nb.cells[3].execution_count, 2) + self.assertEqual(nb.cells[3].outputs[0].output_type, 'error') + + self.assertEqual(nb.cells[4].execution_count, None) From 1af5ae58bafbd70b838a3904c9fb7452d63ea1ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:46:55 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- papermill/exceptions.py | 1 + papermill/tests/test_execute.py | 1 + 2 files changed, 2 insertions(+) diff --git a/papermill/exceptions.py b/papermill/exceptions.py index b61eeb8b..8cb96bcf 100644 --- a/papermill/exceptions.py +++ b/papermill/exceptions.py @@ -79,6 +79,7 @@ def missing_dep(): return missing_dep + # copied from https://github.com/jonathaneunice/ansiwrap/blob/master/ansiwrap/core.py # papermill used to use strip_color from ansiwrap package, but removed due to dependency # resolution issues (ref: https://github.com/nteract/papermill/pull/681) diff --git a/papermill/tests/test_execute.py b/papermill/tests/test_execute.py index 45b12454..f41232c2 100644 --- a/papermill/tests/test_execute.py +++ b/papermill/tests/test_execute.py @@ -430,6 +430,7 @@ def test_notebook_node_input(self): test_nb = nbformat.read(self.result_path, as_version=4) self.assertEqual(test_nb.metadata.papermill.parameters, {'msg': 'Hello'}) + class TestOutputFormatting(unittest.TestCase): def setUp(self): self.test_dir = tempfile.mkdtemp() From 0409031a5dc6a58533199ec3e392c6f696b608f8 Mon Sep 17 00:00:00 2001 From: Mehmet Bektas Date: Wed, 3 Apr 2024 19:02:08 -0700 Subject: [PATCH 3/5] use strip_color from ansicolors package --- papermill/exceptions.py | 19 +------------------ requirements.txt | 1 + 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/papermill/exceptions.py b/papermill/exceptions.py index 8cb96bcf..1e28819d 100644 --- a/papermill/exceptions.py +++ b/papermill/exceptions.py @@ -1,4 +1,4 @@ -import re +from colors import strip_color class AwsError(Exception): @@ -78,20 +78,3 @@ def missing_dep(): ) return missing_dep - - -# copied from https://github.com/jonathaneunice/ansiwrap/blob/master/ansiwrap/core.py -# papermill used to use strip_color from ansiwrap package, but removed due to dependency -# resolution issues (ref: https://github.com/nteract/papermill/pull/681) -def strip_color(s): - """ - Remove ANSI color/style sequences from a string. The set of all - possibly ANSI sequences is large, so does not try to strip every - possible one. But does strip some outliers seen not just in text - generated by this module, but by other ANSI colorizers in the wild. - Those include `\x1b[K` (aka EL or erase to end of line) and `\x1b[m` - a terse version of the more common `\x1b[0m`. - """ - ANSIRE = re.compile('\x1b\\[(K|.*?m)') - - return ANSIRE.sub('', s) diff --git a/requirements.txt b/requirements.txt index 6f8ebb8a..6eb127c2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ requests entrypoints tenacity >= 5.0.2 aiohttp >=3.9.0; python_version=="3.12" +ansicolors From 4d66f0a0071a4e66c03dcc081857806b83235b69 Mon Sep 17 00:00:00 2001 From: Mehmet Bektas Date: Wed, 3 Apr 2024 19:08:36 -0700 Subject: [PATCH 4/5] fix binder build, import strip_color directly --- binder/requirements.txt | 1 + papermill/tests/test_execute.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/binder/requirements.txt b/binder/requirements.txt index 6ebde05a..6b15e076 100644 --- a/binder/requirements.txt +++ b/binder/requirements.txt @@ -3,3 +3,4 @@ pandas matplotlib ipywidgets ipykernel +ansicolors diff --git a/papermill/tests/test_execute.py b/papermill/tests/test_execute.py index f41232c2..9d9b181e 100644 --- a/papermill/tests/test_execute.py +++ b/papermill/tests/test_execute.py @@ -6,12 +6,13 @@ from functools import partial from pathlib import Path from unittest.mock import ANY, patch +from colors import strip_color import nbformat from nbformat import validate from .. import engines, translators -from ..exceptions import PapermillExecutionError, strip_color +from ..exceptions import PapermillExecutionError from ..execute import execute_notebook from ..iorw import load_notebook_node from ..log import logger From 7a6dc45f6fd1b12a5e5a824edb155d8e6376a29f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 4 Apr 2024 02:08:50 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- papermill/tests/test_execute.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papermill/tests/test_execute.py b/papermill/tests/test_execute.py index 9d9b181e..87816cd3 100644 --- a/papermill/tests/test_execute.py +++ b/papermill/tests/test_execute.py @@ -6,9 +6,9 @@ from functools import partial from pathlib import Path from unittest.mock import ANY, patch -from colors import strip_color import nbformat +from colors import strip_color from nbformat import validate from .. import engines, translators