Skip to content

Commit

Permalink
Update the notebook metadata filter to include existing entries
Browse files Browse the repository at this point in the history
Closes #376
  • Loading branch information
mwouts committed Nov 9, 2019
1 parent e5fcc36 commit ccf0d23
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
12 changes: 12 additions & 0 deletions jupytext/metadata_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ def update_metadata_filters(metadata, jupyter_md, cell_metadata):
cell_metadata = {'additional': cell_metadata, 'excluded': 'all'}
metadata.setdefault('jupytext', {})['notebook_metadata_filter'] = '-all'
metadata.setdefault('jupytext', {})['cell_metadata_filter'] = metadata_filter_as_string(cell_metadata)
else:
# Update the notebook metadata filter to include existing entries 376
nb_md_filter = metadata.get('jupytext', {}).get('notebook_metadata_filter', '').split(',')
nb_md_filter = [key for key in nb_md_filter if key]
if 'all' in nb_md_filter or '-all' in nb_md_filter:
return
for key in metadata:
if key in _DEFAULT_NOTEBOOK_METADATA or key in nb_md_filter or ('-' + key) in nb_md_filter:
continue
nb_md_filter.append(key)
if nb_md_filter:
metadata.setdefault('jupytext', {})['notebook_metadata_filter'] = ','.join(nb_md_filter)


def apply_metadata_filters(user_filter, default_filter, actual_keys):
Expand Down
62 changes: 59 additions & 3 deletions tests/test_read_simple_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,9 @@ def test_two_markdown_cell_with_no_language_code_works(nb=new_notebook(cells=[
def test_notebook_with_python3_magic(no_jupytext_version_number,
nb=new_notebook(metadata={
'kernelspec': {'display_name': 'Python 3', 'language': 'python',
'name': 'python3'}},
cells=[new_code_cell('%%python2\na = 1\nprint a'),
new_code_cell('%%python3\nb = 2\nprint(b)')]),
'name': 'python3'}}, cells=[
new_code_cell('%%python2\na = 1\nprint a'),
new_code_cell('%%python3\nb = 2\nprint(b)')]),
text="""---
jupyter:
kernelspec:
Expand All @@ -621,3 +621,59 @@ def test_notebook_with_python3_magic(no_jupytext_version_number,

nb2 = jupytext.reads(md, 'md')
compare_notebooks(nb2, nb)


def test_update_metadata_filter(
no_jupytext_version_number,
org="""---
jupyter:
kernelspec:
display_name: Python 3
language: python
name: python3
extra:
key: value
---
""", target="""---
jupyter:
extra:
key: value
jupytext:
notebook_metadata_filter: extra
kernelspec:
display_name: Python 3
language: python
name: python3
---
"""):
nb = jupytext.reads(org, 'md')
text = jupytext.writes(nb, 'md')
compare(text, target)


def test_update_metadata_filter_2(
no_jupytext_version_number,
org="""---
jupyter:
jupytext:
notebook_metadata_filter: -extra
kernelspec:
display_name: Python 3
language: python
name: python3
extra:
key: value
---
""", target="""---
jupyter:
jupytext:
notebook_metadata_filter: -extra
kernelspec:
display_name: Python 3
language: python
name: python3
---
"""):
nb = jupytext.reads(org, 'md')
text = jupytext.writes(nb, 'md')
compare(text, target)

0 comments on commit ccf0d23

Please sign in to comment.