-
Notifications
You must be signed in to change notification settings - Fork 95
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
flag to remove empty cell (with no data) #131
Comments
There's no way to do this right now, but I'll add this feature in the upcoming 0.4.0 release. |
The quick and dirty solution would be to just read the notebook as """A little tool to remove empty cells from notebooks.
Since ``nbstripout`` doesn't have this feature yet, we do it ourselves.
See: https://github.com/kynan/nbstripout/issues/131
"""
import json
from pathlib import Path
from typing import List
from typing import Optional
SCRIPT_ROOT_PATH = Path(__file__).parent
NOTEBOOK_BASE_PATH = SCRIPT_ROOT_PATH / "source" / "notebooks"
def strip_empty_cells_from_notebooks(args: Optional[List[str]] = None) -> int:
"""Strips empty cells from notebooks in NOTEBOOK_BASE_PATH."""
if args is None:
notebook_paths = NOTEBOOK_BASE_PATH.rglob("*.ipynb")
else:
notebook_paths = [Path(arg) for arg in args]
for notebook_path in notebook_paths:
notebook = json.loads(notebook_path.read_text())
originale_nr_of_cells = len(notebook["cells"])
notebook["cells"] = [cell for cell in notebook["cells"] if cell.get("source", []) != []]
if originale_nr_of_cells != len(notebook["cells"]):
print(f"Fixing: {notebook_path}")
# to ensure an `lf` newline on windows we need to use `.open` instead of `write_text`
with notebook_path.open(mode="w", encoding="utf8", newline="\n") as f:
f.write(json.dumps(notebook, indent=1) + "\n")
return 0
if __name__ == "__main__":
import sys
exit(strip_empty_cells_from_notebooks(sys.argv[1:])) Used as - repo: local
hooks:
- id: strip-empty-notebook-cells
name: Strip empty notebook cells
language: system
entry: python docs/strip_empty_notebook_cells.py
types: [jupyter] To run it on all notebooks you can use I might make it a standalone hook since I don't want to copy-paste files across projects, but this is my hotfix for now. |
This is now available in nbstripout 0.4.0 |
This is great. How do I make it so this option is applied as part of the git filter? |
@devmcp If you use - repo: https://github.com/kynan/nbstripout
rev: 0.4.0
hooks:
- id: nbstripout
args: [--strip-empty-cells] |
Thanks @s-weigand. I much prefer to use it in a git filter rather than |
@devmcp To use this option with the git filter, just edit your |
Hello, is there any possibility to add option to remove cells which not have any data (or only spaces/tabs/newlines) / tags / unfiltered metadata from ipynb?
I often create cells with no data, which will be deleted is some time. Of cource some people in such way separate code, but this option could be optional for people who want clear ipynb files in git.
Or is there any way for this with current functionality?
The text was updated successfully, but these errors were encountered: