Skip to content

Commit

Permalink
Mark cells as active only in ipynb with cell tags
Browse files Browse the repository at this point in the history
Fixes #226
  • Loading branch information
mwouts committed May 15, 2019
1 parent ea5d5e9 commit 59a54e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release History
**Improvements**

- Jupytext's content manager has a new option `notebook_extensions` (#224, #183)
- Cells can be made inactive in scripts with the `active-ipynb` cell tag (#226)

**BugFixes*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ In the animation below we propose a quick demo of Jupytext. While the example re
Jupytext allows to import code from other Jupyter notebooks in a very simple manner. Indeed, all you need to do is to pair the notebook that you wish to import with a script, and import the resulting script.

If the notebook contains demos and plots that you don't want to import, mark those cells as either
- _active_ only in the `ipynb` format, with the `{"active": "ipynb"}` cell metadata
- _active_ only in the `ipynb` format, with the `{"active": "ipynb"}` cell metadata, or with an `active-ipynb` tag (you may use the [jupyterlab-celltags](https://github.com/jupyterlab/jupyterlab-celltags) extension for this).
- _frozen_, using the [freeze extension](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/freeze/readme.html) for Jupyter notebook.

Inactive cells will be commented in the paired script, and consequently will not be executed when the script is imported.
Expand Down
5 changes: 4 additions & 1 deletion jupytext/cell_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def rmd_options_to_metadata(options):
for name in metadata:
try_eval_metadata(metadata, name)

if ('active' in metadata or metadata.get('run_control', {}).get('frozen') is True) and 'eval' in metadata:
if 'eval' in metadata and not is_active('.Rmd', metadata):
del metadata['eval']

return metadata.get('language') or language, metadata
Expand Down Expand Up @@ -351,6 +351,9 @@ def is_active(ext, metadata):
"""Is the cell active for the given file extension?"""
if metadata.get('run_control', {}).get('frozen') is True:
return False
for tag in metadata.get('tags', []):
if tag.startswith('active-'):
return ext.replace('.', '') in tag.split('-')
if 'active' not in metadata:
return True
return ext.replace('.', '') in re.split('\\.|,', metadata['active'])
Expand Down
31 changes: 31 additions & 0 deletions tests/test_active_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ def test_active_ipynb(ext):
compare(nb.cells[0], ACTIVE_IPYNB['.ipynb'])


ACTIVE_IPYNB_RMD_USING_TAG = {'.py': """# + {"tags": ["active-ipynb-Rmd"]}
# # This cell is active only in ipynb and Rmd
# %matplotlib inline
""",
'.Rmd': """```{python tags=c("active-ipynb-Rmd")}
# This cell is active only in ipynb and Rmd
# %matplotlib inline
```
""",
'.R': """# + {"tags": ["active-ipynb-Rmd"]}
# # This cell is active only in ipynb and Rmd
# %matplotlib inline
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active only in ipynb and Rmd\n'
'%matplotlib inline',
'metadata': {'tags': ['active-ipynb-Rmd']},
'execution_count': None,
'outputs': []}}


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_ipynb_rmd_using_tags(ext):
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', False):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB_RMD_USING_TAG[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB_RMD_USING_TAG[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_IPYNB_RMD_USING_TAG['.ipynb'])


ACTIVE_IPYNB_RSPIN = {'.R': """#+ active="ipynb", eval=FALSE
# # This cell is active only in ipynb
# 1 + 1
Expand Down

0 comments on commit 59a54e8

Please sign in to comment.