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

Optionally speed up validation #1672

Merged
merged 1 commit into from Nov 14, 2021
Merged
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
22 changes: 16 additions & 6 deletions nbconvert/exporters/exporter.py
Expand Up @@ -61,6 +61,10 @@ class Exporter(LoggingConfigurable):
help="Extension of the file that should be written to disk"
).tag(config=True)

optimistic_validation = Bool(False,
help = "Reduces the number of validation steps so that it only occurs after all preprocesors have run."
).tag(config=True)

# MIME type of the result file, for HTTP response headers.
# This is *not* a traitlet, because we want to be able to access it from
# the class, not just on instances.
Expand Down Expand Up @@ -296,6 +300,13 @@ def _init_resources(self, resources):
resources['output_extension'] = self.file_extension
return resources

def _validate_preprocessor(self, nbc, preprocessor):
try:
nbformat.validate(nbc, relax_add_props=True)
except nbformat.ValidationError:
self.log.error('Notebook is invalid after preprocessor %s',
preprocessor)
raise

def _preprocess(self, nb, resources):
"""
Expand All @@ -321,11 +332,10 @@ def _preprocess(self, nb, resources):
# to each preprocessor
for preprocessor in self._preprocessors:
nbc, resc = preprocessor(nbc, resc)
try:
nbformat.validate(nbc, relax_add_props=True)
except nbformat.ValidationError:
self.log.error('Notebook is invalid after preprocessor %s',
preprocessor)
raise
if not self.optimistic_validation:
self._validate_preprocessor(nbc, preprocessor)

if self.optimistic_validation:
self._validate_preprocessor(nbc, preprocessor)

return nbc, resc