From fed11e6554186d3a4184b76030f968ec157ea071 Mon Sep 17 00:00:00 2001 From: Cory Gwin Date: Thu, 18 Nov 2021 16:58:39 +0000 Subject: [PATCH 1/2] Add an invalid notebook error Motivation: - As we are now service hundreds of thousands of notebooks a day, we are seeing a number of errors from Notebook rendering that really stem from notebooks not being properly formated. These show up in a number of errors such as the error handled in this pr, an assertion error, we have also seen key errors and others. This pr is a bit of a proposal to instead introduced a common exception that indicates the notebook is missing some required data that is needed to render itself. --- nbconvert/filters/markdown_mistune.py | 5 ++++- nbconvert/nbconvertapp.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py index ff209f372..57dd39726 100644 --- a/nbconvert/filters/markdown_mistune.py +++ b/nbconvert/filters/markdown_mistune.py @@ -160,7 +160,10 @@ def image(self, src, title, text): attachment_prefix = 'attachment:' if src.startswith(attachment_prefix): name = src[len(attachment_prefix):] - assert name in attachments, "missing attachment: {}".format(name) + + if not name in attachments: + nbconvert.InvalidNotebook.new("missing attachment: {}".format(name)) + attachment = attachments[name] # we choose vector over raster, and lossless over lossy preferred_mime_types = ['image/svg+xml', 'image/png', 'image/jpeg'] diff --git a/nbconvert/nbconvertapp.py b/nbconvert/nbconvertapp.py index e137a173d..97ca798b2 100755 --- a/nbconvert/nbconvertapp.py +++ b/nbconvert/nbconvertapp.py @@ -595,6 +595,9 @@ def initialize(self, argv=None): def default_export_format(self): return 'html' +class InvalidNotebook(Exception): + pass + #----------------------------------------------------------------------------- # Main entry point #----------------------------------------------------------------------------- From 493aa18459fb9257e7214b9a964bf8a15819a540 Mon Sep 17 00:00:00 2001 From: Cory Gwin Date: Fri, 10 Dec 2021 12:56:33 -0500 Subject: [PATCH 2/2] Update nbconvert/filters/markdown_mistune.py Co-authored-by: Matthias Bussonnier --- nbconvert/filters/markdown_mistune.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py index 57dd39726..a46be513f 100644 --- a/nbconvert/filters/markdown_mistune.py +++ b/nbconvert/filters/markdown_mistune.py @@ -162,7 +162,7 @@ def image(self, src, title, text): name = src[len(attachment_prefix):] if not name in attachments: - nbconvert.InvalidNotebook.new("missing attachment: {}".format(name)) + raise InvalidNotebook("missing attachment: {}".format(name)) attachment = attachments[name] # we choose vector over raster, and lossless over lossy