Skip to content

Commit

Permalink
Use notebook_extensions from the config file if set #746
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Mar 16, 2021
1 parent b15d467 commit 681d377
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Jupytext ChangeLog
-------------------

**Fixed**
- The `jupytext.toml` can now be used together with the `jupytext` pre-commit hook ([#752](https://github.com/mwouts/jupytext/issues/752))
- The `jupytext.toml` config file can now be used together with the `jupytext` pre-commit hook ([#752](https://github.com/mwouts/jupytext/issues/752))
- The `notebook_extensions` option of the `jupytext.toml` file now works ([#746](https://github.com/mwouts/jupytext/issues/746))

**Changed**
- The options in `jupytext.toml` where renamed to match the `jupytext` metadata in the text notebooks. One should now use `formats` rather than `default_jupytext_formats` and `notebook_metadata_filter` rather than `default_notebook_metadata_filter` ([#753](https://github.com/mwouts/jupytext/issues/753))
Expand Down
18 changes: 7 additions & 11 deletions jupytext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ class JupytextConfiguration(Configurable):
config=True,
)
default_jupytext_formats = Unicode(
"", help="Deprecated. Use 'formats' instead", config=True
help="Deprecated. Use 'formats' instead", config=True
)

preferred_jupytext_formats_save = Unicode(
u"",
help="Preferred format when saving notebooks as text, per extension. "
'Use "jl:percent,py:percent,R:percent" if you want to save '
"Julia, Python and R scripts in the double percent format and "
Expand All @@ -66,15 +65,13 @@ class JupytextConfiguration(Configurable):
)

preferred_jupytext_formats_read = Unicode(
u"",
help="Preferred format when reading notebooks from text, per "
'extension. Use "py:sphinx" if you want to read all python '
"scripts as Sphinx gallery scripts.",
config=True,
)

notebook_metadata_filter = Unicode(
u"",
help="Notebook metadata that should be save in the text representations. "
"Examples: 'all', '-all', 'widgets,nteract', 'kernelspec,jupytext-all'",
config=True,
Expand All @@ -100,7 +97,6 @@ class JupytextConfiguration(Configurable):
)

cell_metadata_filter = Unicode(
u"",
help="Cell metadata that should be saved in the text representations. "
"Examples: 'all', 'hide_input,hide_output'",
config=True,
Expand Down Expand Up @@ -145,24 +141,22 @@ class JupytextConfiguration(Configurable):
)

cell_markers = Unicode(
u"",
help='Start and end cell markers for the light format, comma separated. Use "{{{,}}}" to mark cells'
'as foldable regions in Vim, and "region,endregion" to mark cells as Vscode/PyCharm regions',
config=True,
)

default_cell_markers = Unicode(
"", help="Deprecated. Use 'cell_markers' instead", config=True
help="Deprecated. Use 'cell_markers' instead", config=True
)

notebook_extensions = Unicode(
u",".join(NOTEBOOK_EXTENSIONS),
help="A comma separated list of notebook extensions",
notebook_extensions = Union(
[List(Unicode(), NOTEBOOK_EXTENSIONS), Unicode()],
help="A list of notebook extensions",
config=True,
)

custom_cell_magics = Unicode(
"",
help='A comma separated list of cell magics. Use e.g. custom_cell_magics = "configure,local" '
'if you want code cells starting with the Spark magic cell commands "configure" and "local" '
"to be commented out when converted to scripts.",
Expand Down Expand Up @@ -377,6 +371,8 @@ def load_jupytext_configuration_file(config_file, stream=None):
for prefix, fmt in config.formats.items()
]
config.formats = short_form_multiple_formats(config.formats)
if isinstance(config.notebook_extensions, str):
config.notebook_extensions = config.notebook_extensions.split(",")
return config


Expand Down
18 changes: 12 additions & 6 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def __init__(self, *args, **kwargs):
self.super = super(JupytextContentsManager, self)
self.super.__init__(*args, **kwargs)

def all_nb_extensions(self):
def all_nb_extensions(self, config):
"""All extensions that should be classified as notebooks"""
return [
ext if ext.startswith(".") else "." + ext
for ext in self.notebook_extensions.split(",")
for ext in config.notebook_extensions
]

def drop_paired_notebook(self, path):
Expand Down Expand Up @@ -192,11 +192,14 @@ def get(
if (
not self.file_exists(path)
or self.dir_exists(path)
or (type != "notebook" if type else ext not in self.all_nb_extensions())
or (type is not None and type != "notebook")
):
return self.super.get(path, content, type, format)

config = self.get_config(path, use_cache=content is False)
if ext not in self.all_nb_extensions(config):
return self.super.get(path, content, type, format)

fmt = preferred_format(ext, config.preferred_jupytext_formats_read)
if ext == ".ipynb":
model = self.super.get(path, content, type="notebook", format=format)
Expand Down Expand Up @@ -371,7 +374,8 @@ def new_untitled(self, path="", type="", ext=""):
raise HTTPError(404, "No such directory: %s" % path)

untitled = self.untitled_notebook
name = self.increment_notebook_filename(untitled + ext, path)
config = self.get_config(path)
name = self.increment_notebook_filename(config, untitled + ext, path)
path = u"{0}/{1}".format(path, name)

model = {"type": "notebook"}
Expand All @@ -383,7 +387,7 @@ def new_untitled(self, path="", type="", ext=""):

return self.new(model, path)

def increment_notebook_filename(self, filename, path=""):
def increment_notebook_filename(self, config, filename, path=""):
"""Increment a notebook filename until it is unique, regardless of extension"""
# Extract the full suffix from the filename (e.g. .tar.gz)
path = path.strip("/")
Expand All @@ -399,7 +403,7 @@ def increment_notebook_filename(self, filename, path=""):
name = basename_i + ext
if not any(
self.exists(u"{}/{}{}".format(path, basename_i, nb_ext))
for nb_ext in self.notebook_extensions.split(",")
for nb_ext in config.notebook_extensions
):
break
return name
Expand Down Expand Up @@ -533,6 +537,8 @@ def get_config(self, path, use_cache=False):
self.cached_config.config_file,
)
return self.cached_config.config
if isinstance(self.notebook_extensions, str):
self.notebook_extensions = self.notebook_extensions.split(",")
return self

return JupytextContentsManager
Expand Down
32 changes: 24 additions & 8 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,20 +1562,36 @@ def test_save_file_with_cell_markers(tmpdir):
assert nb2.metadata["jupytext"]["cell_markers"] == "region,endregion"


def test_notebook_extensions(tmpdir):
tmp_py = str(tmpdir.join("script.py"))
tmp_rmd = str(tmpdir.join("notebook.Rmd"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
def test_notebook_extensions(tmpdir, cwd_tmpdir):
nb = new_notebook()
write(nb, "script.py")
write(nb, "notebook.Rmd")
write(nb, "notebook.ipynb")

cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.notebook_extensions = "ipynb,Rmd"

model = cm.get("notebook.ipynb")
assert model["type"] == "notebook"

model = cm.get("notebook.Rmd")
assert model["type"] == "notebook"

model = cm.get("script.py")
assert model["type"] == "file"


def test_notebook_extensions_in_config(tmpdir, cwd_tmpdir):
nb = new_notebook()
write(nb, tmp_py)
write(nb, tmp_rmd)
write(nb, tmp_ipynb)
write(nb, "script.py")
write(nb, "notebook.Rmd")
write(nb, "notebook.ipynb")
tmpdir.join("jupytext.toml").write("""notebook_extensions = ["ipynb", "Rmd"]""")

cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)

cm.notebook_extensions = "ipynb,Rmd"
model = cm.get("notebook.ipynb")
assert model["type"] == "notebook"

Expand Down

0 comments on commit 681d377

Please sign in to comment.