Skip to content

Commit

Permalink
Two separate options for preferred formats: read and save
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 22, 2018
1 parent 8a5e129 commit cd83906
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Note that the double percent scripts you have written outside of Jupytext will b

If you want to write the Python representation of your notebooks in that format per default, add the following to your `.jupyter/jupyter_notebook_config.py` file:
```python
c.ContentsManager.preferred_jupytext_formats = "py:percent"
c.ContentsManager.preferred_jupytext_formats_save = "py:percent"
```

### Sphinx-gallery scripts
Expand All @@ -208,6 +208,7 @@ If you want that the reStructuredText be converted to markdown for a nicer displ
# 1. Save the below as .jupyter/jupyter_notebook_config.py in your GitHub project, and
# 2. Create binder/requirements.txt
c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
c.ContentsManager.preferred_jupytext_formats_read = "py:sphinx"
c.ContentsManager.sphinx_convert_rst2md = True
```

Expand Down
27 changes: 20 additions & 7 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def _writes(nbk, version=nbformat.NO_CONVERT, **kwargs):
return _writes


def _jupytext_reads(ext, rst2md):
def _jupytext_reads(ext, format_name, rst2md):
def _reads(text, as_version, **kwargs):
return jupytext.reads(text, ext=ext, rst2md=rst2md,
return jupytext.reads(text, ext=ext,
format_name=format_name, rst2md=rst2md,
as_version=as_version, **kwargs)

return _reads
Expand Down Expand Up @@ -133,14 +134,21 @@ def all_nb_extensions(self):
'hydrogen/spyder/vscode compatible scripts',
config=True)

preferred_jupytext_formats = Unicode(
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 '
'only write "jupytext_formats": "py" in the notebook metadata.',
config=True)

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)

sphinx_convert_rst2md = Bool(
False,
help='When opening a Sphinx Gallery script, convert the '
Expand Down Expand Up @@ -182,10 +190,10 @@ def format_group(self, fmt, nbk=None):

return [fmt]

def preferred_format(self, ext):
def preferred_format(self, ext, preferred):
"""Returns the preferred format for that extension"""
for fmt_ext, format_name in \
parse_formats(self.preferred_jupytext_formats):
parse_formats(preferred):
if fmt_ext == ext:
return format_name
return None
Expand All @@ -194,8 +202,11 @@ def _read_notebook(self, os_path, as_version=4):
"""Read a notebook from an os path."""
_, ext = os.path.splitext(os_path)
if ext in self.nb_extensions:
format_name = self.preferred_format(
ext, self.preferred_jupytext_formats_read)
with mock.patch('nbformat.reads',
_jupytext_reads(ext, self.sphinx_convert_rst2md)):
_jupytext_reads(ext, format_name,
self.sphinx_convert_rst2md)):
return super(TextFileContentsManager, self) \
._read_notebook(os_path, as_version)
else:
Expand All @@ -212,7 +223,9 @@ def _save_notebook(self, os_path, nb):

if alt_ext in self.nb_extensions:
format_name = format_name_for_ext(nb.metadata, alt_ext) or \
self.preferred_format(alt_ext)
self.preferred_format(
alt_ext,
self.preferred_jupytext_formats_save)
with mock.patch('nbformat.writes',
_jupytext_writes(alt_ext, format_name)):
super(TextFileContentsManager, self) \
Expand Down
2 changes: 1 addition & 1 deletion tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_save_to_percent_format(nb_file, tmpdir):

cm = TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.preferred_jupytext_formats = 'jl:percent'
cm.preferred_jupytext_formats_save = 'jl:percent'

nb['metadata']['jupytext_formats'] = 'ipynb,jl'

Expand Down

0 comments on commit cd83906

Please sign in to comment.