Skip to content

Commit

Permalink
New option to classify only specific extensions as notebooks
Browse files Browse the repository at this point in the history
Fixes #224 #183
  • Loading branch information
mwouts committed May 15, 2019
1 parent 440fc8b commit ea5d5e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Release History
1.1.2 (2019-05-15)
++++++++++++++++++++++

**Improvements**

- Jupytext's content manager has a new option `notebook_extensions` (#224, #183)

**BugFixes*
- Directories ending in .jl (or .ipynb) are not notebooks (#228)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ In JupyterLab this is slightly different. Scripts and Markdown document also hav

![](https://gist.githubusercontent.com/mwouts/13de42d8bb514e4acf6481c580feffd0/raw/403b53ac5097446a15ea664579ba44cd1badcc57/ContextMenuLab.png)

If do not want to classify scripts or Markdown documents as notebooks, please use the `notebook_extension` option. For instance, if you want to get the notebook icon only for `.ipynb` and `.Rmd` files, set

```python
c.ContentsManager.notebook_extensions = "ipynb,Rmd"
```

Please note that, with the above setting, Jupyter will not let you open scripts as notebooks. If you still want to do so, use Jupytext command line (see below) to first convert or pair the script to an `.ipynb` notebook.

## Scripting Jupytext

### Command line conversion
Expand Down
14 changes: 7 additions & 7 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,12 @@ class TextFileContentsManager(LargeFileManager, Configurable):
Python (.py) or R scripts (.R)
"""

nb_extensions = [ext for ext in NOTEBOOK_EXTENSIONS if ext != '.ipynb']

# Dictionary: notebook path => (fmt, formats) where fmt is the current format, and formats the paired formats.
paired_notebooks = dict()

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

default_jupytext_formats = Unicode(
u'',
Expand Down Expand Up @@ -159,6 +154,11 @@ def all_nb_extensions(self):
'as foldable regions in Vim, and "region,endregion" to mark cells as Vscode/PyCharm regions',
config=True)

notebook_extensions = Unicode(
u','.join(NOTEBOOK_EXTENSIONS),
help='A comma separated list of notebook extensions',
config=True)

def drop_paired_notebook(self, path):
"""Remove the current notebook from the list of paired notebooks"""
if path not in self.paired_notebooks:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,27 @@ def test_save_file_with_default_cell_markers(tmpdir):
nb2 = cm.get('nb.py')['content']
compare_notebooks(nb, nb2)
assert nb2.metadata['jupytext']['cell_markers'] == '+,-'


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'))

nb = new_notebook()
writef(nb, tmp_py)
writef(nb, tmp_rmd)
writef(nb, tmp_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'

0 comments on commit ea5d5e9

Please sign in to comment.