Skip to content

Commit 1e8b7ea

Browse files
authored
Throw a validation error if the worksheet conversion fails
Motivation: - We are seeing a lot of errors in this worksheets process where keys are not present that are needed, this pr adds a validation error when this process cannot be completed because the key is missing.
1 parent f101cd2 commit 1e8b7ea

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

nbformat/v3/rwbase.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Distributed under the terms of the Modified BSD License.
55

66
from ipython_genutils.py3compat import str_to_bytes
7-
7+
from .. import validator
88
from .._compat import encodebytes, decodebytes
99

1010

@@ -16,14 +16,17 @@ def restore_bytes(nb):
1616
1717
Note: this is never used
1818
"""
19-
for ws in nb.worksheets:
20-
for cell in ws.cells:
21-
if cell.cell_type == 'code':
22-
for output in cell.outputs:
23-
if 'png' in output:
24-
output.png = str_to_bytes(output.png, 'ascii')
25-
if 'jpeg' in output:
26-
output.jpeg = str_to_bytes(output.jpeg, 'ascii')
19+
try:
20+
for ws in nb.worksheets:
21+
for cell in ws.cells:
22+
if cell.cell_type == 'code':
23+
for output in cell.outputs:
24+
if 'png' in output:
25+
output.png = str_to_bytes(output.png, 'ascii')
26+
if 'jpeg' in output:
27+
output.jpeg = str_to_bytes(output.jpeg, 'ascii')
28+
except KeyError as e:
29+
validator.ValidationError(f"The notebook was invalid missing the key: {e.message}")
2730
return nb
2831

2932
# output keys that are likely to have multiline values

0 commit comments

Comments
 (0)