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

🐛 Use nbformat for write_notebook #278

Open
wants to merge 1 commit 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
14 changes: 7 additions & 7 deletions nbproject/dev/_notebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import Union

import nbformat
import orjson
from pydantic import BaseModel

Expand All @@ -25,10 +26,11 @@
nb = orjson.loads(f.read())
except orjson.JSONDecodeError as e:
if "Input is a zero-length, empty document" in str(e):
raise ValueError("Notebook cannot be empty. It must have at least a title cell.")
raise ValueError(

Check warning on line 29 in nbproject/dev/_notebook.py

View check run for this annotation

Codecov / codecov/patch

nbproject/dev/_notebook.py#L29

Added line #L29 was not covered by tests
"Notebook cannot be empty. It must have at least a title cell."
) from e
else:
raise

raise e

Check warning on line 33 in nbproject/dev/_notebook.py

View check run for this annotation

Codecov / codecov/patch

nbproject/dev/_notebook.py#L33

Added line #L33 was not covered by tests

return Notebook(**nb)

Expand All @@ -40,7 +42,5 @@
nb: Notebook to write.
filepath: Path where to write the notebook.
"""
with open(filepath, "wb") as f:
# the formatting orjson dumps doesn't match jupyter lab
# maybe one can homogenize it at some point
f.write(orjson.dumps(nb.dict(), option=orjson.OPT_INDENT_2))
nb_node = nbformat.from_dict(nb.dict())
nbformat.write(nb_node, filepath, version=nbformat.NO_CONVERT)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"pyyaml",
"packaging",
"orjson",
"nbformat",
"ipylab",
"importlib-metadata",
"stdlib_list; python_version < '3.10'",
Expand Down
Loading