Skip to content

Commit

Permalink
Settings from the config file have precedence over those in the noteb…
Browse files Browse the repository at this point in the history
…ook at save time
  • Loading branch information
mwouts committed Mar 15, 2021
1 parent 88725af commit 4a474ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
19 changes: 6 additions & 13 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,14 @@ def __init__(self, fmt, config):
def update_fmt_with_notebook_options(self, metadata, read=False):
"""Update format options with the values in the notebook metadata, and record those
options in the notebook metadata"""
# The settings in the Jupytext configuration file have precedence over the metadata in the notebook
# when the notebook is saved. This is because the metadata in the notebook might not be visible
# in the text representation when e.g. notebook_metadata_filter="-all", which makes them hard to edit.
if not read and self.config is not None:
self.config.set_default_format_options(self.fmt, read)

# format options in notebook have precedence over that in fmt, and precedence over the config
for opt in _VALID_FORMAT_OPTIONS:
if self.config is not None:
# We use the config filters if provided
if (
opt == "notebook_metadata_filter"
and self.config.default_notebook_metadata_filter
):
continue
if (
opt == "cell_metadata_filter"
and self.config.default_cell_metadata_filter
):
continue

if opt in metadata.get("jupytext", {}):
self.fmt.setdefault(opt, metadata["jupytext"][opt])

Expand Down
14 changes: 9 additions & 5 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,21 +1541,25 @@ def test_save_file_with_default_cell_markers(tmpdir):
nb = cm.get("nb.py")["content"]
assert len(nb.cells) == 1

nb.metadata["jupytext"]["cell_markers"] = "+,-"
del nb.metadata["jupytext"]["notebook_metadata_filter"]
cm.save(model=notebook_model(nb), path="nb.py")

with open(tmp_py) as fp:
text2 = fp.read()

compare(
"\n".join(text2.splitlines()[-len(text.splitlines()) :]),
"\n".join(text.splitlines()),
text2,
"""# region
# this is a unique code cell
1 + 1
2 + 2
# endregion
""",
)

nb2 = cm.get("nb.py")["content"]
compare_notebooks(nb2, nb)
assert nb2.metadata["jupytext"]["cell_markers"] == "+,-"
assert nb2.metadata["jupytext"]["cell_markers"] == "region,endregion"


def test_notebook_extensions(tmpdir):
Expand Down

0 comments on commit 4a474ea

Please sign in to comment.