Skip to content

Commit

Permalink
feat: add option to keep output style (#177)
Browse files Browse the repository at this point in the history
with `nbstripout --keep-metadata-keys` or git config key `filter.nbstripout.keepmetadatakeys`.

closes #78
  • Loading branch information
davidxia authored Dec 9, 2022
1 parent cb47322 commit 1185a8d
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,22 @@ Additional metadata to be stripped can be configured via either
cell.metadata.tags
cell.metadata.init_cell'

* the ``--extra-keys`` flag, which takes a string as an argument, e.g. ::
* the ``--extra-keys`` flag, which takes a space-delimited string as an argument, e.g. ::

--extra-keys "metadata.celltoolbar cell.metadata.heading_collapsed"

You can keep certain metadata with either

* ``git config (--global/--system) filter.nbstripout.keepmetadatakeys``, e.g. ::

git config --global filter.nbstripout.keepmetadatakeys '
cell.metadata.collapsed
cell.metadata.scrolled'

* the ``--keep-metadata-keys`` flag, which takes a space-delimited string as an argument, e.g. ::

--keep-metadata-keys "cell.metadata.collapsed cell.metadata.scrolled"

Note: Previous versions of Jupyter used ``metadata.kernel_spec`` for kernel
metadata. Prefer stripping ``kernelspec`` entirely: only stripping some
attributes inside ``kernelspec`` may lead to errors when opening the notebook
Expand Down
13 changes: 13 additions & 0 deletions nbstripout/_nbstripout.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ def main():
parser.add_argument('--extra-keys', default='',
help='Space separated list of extra keys to strip '
'from metadata, e.g. metadata.foo cell.metadata.bar')
parser.add_argument('--keep-metadata-keys', default='',
help='Space separated list of metadata keys to keep'
', e.g. metadata.foo cell.metadata.bar')
parser.add_argument('--drop-empty-cells', action='store_true',
help='Remove cells where `source` is empty or contains only whitepace')
parser.add_argument('--drop-tagged-cells', default='',
Expand Down Expand Up @@ -446,6 +449,16 @@ def main():

extra_keys.extend(args.extra_keys.split())

try:
keep_metadata_keys = check_output(
(git_config if args._system or args._global else ['git', 'config']) + ['filter.nbstripout.keepmetadatakeys'],
universal_newlines=True
).strip().split()
except (CalledProcessError, FileNotFoundError):
keep_metadata_keys = []
keep_metadata_keys.extend(args.keep_metadata_keys.split())
extra_keys = [i for i in extra_keys if i not in keep_metadata_keys]

# Wrap input/output stream in UTF-8 encoded text wrapper
# https://stackoverflow.com/a/16549381
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') if sys.stdin else None
Expand Down
1 change: 0 additions & 1 deletion nbstripout/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def strip_output(nb, keep_output, keep_count, extra_keys=[], drop_empty_cells=Fa
if 'execution_count' in cell and not keep_count:
cell['execution_count'] = None

# Always remove some metadata
for field in keys['cell']:
pop_recursive(cell, field)
return nb
27 changes: 27 additions & 0 deletions tests/e2e_notebooks/test_keep_metadata_keys.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"scrolled": true
},
"source": [
"This notebook tests that using \"--keep-metadata-keys\" works as expected."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"1+1"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}
27 changes: 27 additions & 0 deletions tests/e2e_notebooks/test_keep_metadata_keys.ipynb.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"scrolled": true
},
"source": [
"This notebook tests that using \"--keep-metadata-keys\" works as expected."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"1+1"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}
3 changes: 2 additions & 1 deletion tests/test_diff.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
Expand Down
1 change: 1 addition & 0 deletions tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
("test_metadata.ipynb", "test_metadata_keep_output.ipynb.expected", ["--keep-output"]),
("test_metadata.ipynb", "test_metadata_keep_output_keep_count.ipynb.expected", ["--keep-output", "--keep-count"]),
("test_metadata_notebook.ipynb", "test_metadata_notebook.ipynb", []),
("test_keep_metadata_keys.ipynb", "test_keep_metadata_keys.ipynb.expected", ["--keep-metadata-keys", "cell.metadata.scrolled cell.metadata.collapsed metadata.a"]),
("test_metadata_period.ipynb", "test_metadata_period.ipynb.expected", ["--extra-keys", "cell.metadata.application/vnd.databricks.v1+cell metadata.application/vnd.databricks.v1+notebook"]),
("test_strip_init_cells.ipynb", "test_strip_init_cells.ipynb.expected", ["--strip-init-cells"]),
("test_nbformat2.ipynb", "test_nbformat2.ipynb.expected", []),
Expand Down
29 changes: 29 additions & 0 deletions tests/test_git_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,32 @@ def test_git_diff_extrakeys(pytester: pytest.Pytester):
],
""".splitlines())
assert len(r.outlines) == 13 # 12 lines + new line at end


def test_git_diff_keepmetadatakeys(pytester: pytest.Pytester):
pytester.run('git', 'init')
pytester.run('git', 'config', '--local', 'filter.nbstripout.keepmetadatakeys', 'cell.metadata.scrolled metadata.foo.bar')
pytester.run('nbstripout', '--install')

r = pytester.run('git', 'diff', '--no-index', NOTEBOOKS_FOLDER / "test_diff.ipynb", NOTEBOOKS_FOLDER / "test_diff_different_extrakeys.ipynb")
assert r.ret == 1
r.stdout.fnmatch_lines(r"""index*
--- *test_diff.ipynb*
+++ *test_diff_different_extrakeys.ipynb*
@@ -3,20 +3,17 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "scrolled": true
- },
+ "metadata": {},
"outputs": [],
"source": [
- "print(\"aou\")"
+ "print(\"aou now it is different\")"
]
}
],
""".splitlines())
assert len(r.outlines) == 28 # 12 lines + new line at end

0 comments on commit 1185a8d

Please sign in to comment.