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

Throw a validation error if the worksheet conversion fails #239

Open
wants to merge 6 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nbformat/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def convert(nb, to_version):
if converted.get('nbformat', 1) == version:
raise ValueError("Failed to convert notebook from v%d to v%d." % (version, step_version))
except AttributeError as e:
raise ValidationError(f"Notebook could not be converted from version {version} to version {step_version} because it's missing a key: {e}")
raise ValidationError("Notebook could not be converted from version {version} to version {step_version} because it's missing a key: {e}".format(version=version, step_version=step_version, e=e))

# Recursively convert until target version is reached.
return convert(converted, to_version)
Expand Down
2 changes: 1 addition & 1 deletion nbformat/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def reads(s, **kwargs):
try:
return versions[major].to_notebook_json(nb_dict, minor=minor)
except AttributeError as e:
raise ValidationError(f"The notebook is invalid and is missing an expected key: {e}")
raise ValidationError("The notebook is invalid and is missing an expected key: %s" % e)
else:
raise NBFormatError('Unsupported nbformat version %s' % major)

Expand Down
21 changes: 12 additions & 9 deletions nbformat/v3/rwbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.


from .._compat import encodebytes, decodebytes
from .. import validator


def restore_bytes(nb):
Expand All @@ -15,14 +15,17 @@ def restore_bytes(nb):

Note: this is never used
"""
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
for output in cell.outputs:
if "png" in output:
output.png = output.png.encode("ascii", "replace")
if "jpeg" in output:
output.jpeg = output.jpeg.encode("ascii", "replace")
try:
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
for output in cell.outputs:
if "png" in output:
output.png = output.png.encode("ascii", "replace")
if "jpeg" in output:
output.jpeg = output.jpeg.encode("ascii", "replace")
except KeyError as e:
raise validator.ValidationError("The notebook was invalid missing the key: %s" % e.message)
return nb

# output keys that are likely to have multiline values
Expand Down