Reputation:
I am trying download the code on this post.
download seems to be successful though, when i open the notebook, an error shows up.
Notebook validation failed
An invalid notebook may not function properly. The validation error was:
Notebook validation failed: Additional properties are not allowed ('execution_count', 'outputs' were unexpected):
why would opening a jupyter notebook get "'outputs' were unexpected" error?
is there a method i could use to examine which part of the notebook is invalid?
Upvotes: 4
Views: 2547
Reputation: 120
In addition to the previous reply, if the error verifiably stems from markdown cells with empty outputs properties, you can use a regex to strip those. It won't affect any other legit outputs.
Here's a python script to do just that:
"""
This script fixes the auto-corrupted jupyter notebooks, specifically removes empty outputs properties (which are not legal for markdown cells).
"""
import re
with open('MyNotebook.ipynb', 'r') as file:
# Read original notebook file to string
jupyter = file.read()
# Run a regex based search and replace, wipe all empty outputs properties.
outputs_tag_removed = re.sub(',\n\s+\"outputs\":\ \[\]', '', jupyter)
# Overwrite original notebook file
print(outputs_tag_removed, file=open('MyNotebook.ipynb', 'w'))
There seem to be similar issues, related to markdown cells with illegal execution count
and code cells without the mandatory execution count
property. See discussion on GitHub.
Upvotes: 1
Reputation: 19885
A Jupyter notebook is actually stored as a JSON file, so you can just open it in a text editor. Each cell is a JSON object that gets decoded to a Python dict
.
The reason you get this error is that cell_type
is markdown
, which signifies that this is a Markdown cell.
Since Markdown cells are rendered instead of executed, it doesn't make sense for them to have the outputs
and execution_count
keys, which apply only to code cells.
You could probably write a simple script to examine your Jupyter notebook; something like this:
import json
valid_keys = ['cell_type', 'metadata', 'source']
filename = ... # specify filename here
with open(filename) as f:
data = json.load(f)
for index, cell in enumerate(data['cells'], 1):
if cell['cell_type'] == 'markdown':
extra_keys = [key for key in cell.keys() if key not in valid_keys]
if extra_keys:
print(f'Cell {index} has the following keys which are invalid for a markdown cell: {extra_keys}')
Upvotes: 3