Skip to content

Commit

Permalink
Map the R Markdown visibility options to the runtools ones
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 29, 2019
1 parent b88ea4d commit e86bf89
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
35 changes: 19 additions & 16 deletions jupytext/cell_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
except NameError:
unicode = str # Python 3

# Map R Markdown's "echo" and "include" to "hide_input" and "hide_output", that are understood by the `runtools`
# extension for Jupyter notebook, and by nbconvert (use the `hide_input_output.tpl` template).
# Map R Markdown's "echo", "results" and "include" to "hide_input" and "hide_output", that are understood by the
# `runtools` extension for Jupyter notebook, and by nbconvert (use the `hide_input_output.tpl` template).
# See http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/runtools/readme.html
_BOOLEAN_OPTIONS_DICTIONARY = [('hide_input', 'echo', True),
('hide_output', 'include', True)]
_RMARKDOWN_TO_RUNTOOLS_OPTION_MAP = [
(('include', 'FALSE'), [('hide_input', True), ('hide_output', True)]),
(('echo', 'FALSE'), [('hide_input', True)]),
(('results', "'hide'"), [('hide_output', True)]),
(('results', '"hide"'), [('hide_output', True)])
]

_JUPYTEXT_CELL_METADATA = [
# Pre-jupytext metadata
'skipline', 'noskipline',
Expand Down Expand Up @@ -71,11 +76,11 @@ def metadata_to_rmd_options(language, metadata):
if 'name' in metadata:
options += ' ' + metadata['name'] + ','
del metadata['name']
for jupyter_option, rmd_option, rev in _BOOLEAN_OPTIONS_DICTIONARY:
if jupyter_option in metadata:
options += ' {}={},'.format(
rmd_option, _r_logical_values(metadata[jupyter_option] != rev))
del metadata[jupyter_option]
for rmd_option, jupyter_options in _RMARKDOWN_TO_RUNTOOLS_OPTION_MAP:
if all([metadata.get(opt_name) == opt_value for opt_name, opt_value in jupyter_options]):
options += ' {}={},'.format(rmd_option[0], 'FALSE' if rmd_option[1] is False else rmd_option[1])
for opt_name, _ in jupyter_options:
metadata.pop(opt_name)
for opt_name in metadata:
opt_value = metadata[opt_name]
opt_name = opt_name.strip()
Expand Down Expand Up @@ -103,13 +108,11 @@ def update_metadata_from_rmd_options(name, value, metadata):
:param metadata:
:return:
"""
for jupyter_option, rmd_option, rev in _BOOLEAN_OPTIONS_DICTIONARY:
if name == rmd_option:
try:
metadata[jupyter_option] = _py_logical_values(value) != rev
return True
except RLogicalValueError:
pass
for rmd_option, jupyter_options in _RMARKDOWN_TO_RUNTOOLS_OPTION_MAP:
if name == rmd_option[0] and value == rmd_option[1]:
for opt_name, opt_value in jupyter_options:
metadata[opt_name] = opt_value
return True
return False


Expand Down
34 changes: 34 additions & 0 deletions tests/test_hide_remove_input_outputs_rmarkdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
import jupytext
from jupytext.compare import compare, compare_notebooks
from .utils import skip_if_dict_is_not_ordered


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('md,rmd', [('hide_input=true hide_output=true', 'include=FALSE'),
('hide_output=true', "results='hide'"),
('hide_output=true', 'results="hide"'),
('hide_input=true', 'echo=FALSE')])
def test_runtools_options_to_rmarkdown(md, rmd):
"""Options set by the runtools extension are mapped to the corresponding R Markdown options
https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/runtools/readme.html"""
md = '```python ' + md + """
1 + 1
```
"""

rmd = '```{python ' + rmd + """}
1 + 1
```
"""

nb_md = jupytext.reads(md, 'md')
nb_rmd = jupytext.reads(rmd, 'Rmd')
compare_notebooks(nb_rmd, nb_md)

md2 = jupytext.writes(nb_rmd, 'md')
compare(md2, md)

rmd = rmd.replace('"hide"', "'hide'")
rmd2 = jupytext.writes(nb_md, 'Rmd')
compare(rmd2, rmd)

0 comments on commit e86bf89

Please sign in to comment.