Skip to content

Commit

Permalink
Clear widgets metadata in clear (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored Nov 9, 2024
1 parent d888b51 commit aebf660
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/juv/_clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,37 @@

def clear(path: Path) -> None:
nb = nbformat.read(path, nbformat.NO_CONVERT)
# clear cells
for cell in nb.cells:
if cell.cell_type == "code":
cell.outputs = []
cell.execution_count = None
# clear widgets metadata
nb.metadata.pop("widgets", None)
nbformat.write(nb, path)


def is_cleared(path: Path) -> bool:
"""Check if a notebook has been cleared."""
"""Check if a notebook has been cleared.
A notebook is considered cleared if:
- It does not have any outputs or execution counts in code cells.
- It does not have any widgets metadata.
Parameters
----------
path : Path
Path to the notebook file.
Returns
-------
bool
True if the notebook is cleared, False otherwise
"""
nb = nbformat.read(path, nbformat.NO_CONVERT)
if "widgets" in nb.metadata:
return False
for cell in filter(lambda cell: cell.cell_type == "code", nb.cells):
if cell.outputs or cell.execution_count is not None:
return False
Expand Down

0 comments on commit aebf660

Please sign in to comment.