diff --git a/CHANGELOG.md b/CHANGELOG.md index a8667a1b8..5db3fc22d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Fixed the CLI example for not commenting out magic commands: `--opt comment_magics=false`. In addition, most of the `jupytext` commands in `using-cli.md` are now tested! (#465) - `jupytext.read` and `jupytext.write` now give more meaningful errors when the format information is incorrect (#462) - Multiline comments starting with quadruple quotes should not cause issues anymore (#460) +- Fixed active cells in the py:percent format (#477) 1.4.1 (2020-03-19) ------------------ diff --git a/jupytext/cell_reader.py b/jupytext/cell_reader.py index 7bb10ef03..831676a4c 100644 --- a/jupytext/cell_reader.py +++ b/jupytext/cell_reader.py @@ -689,6 +689,10 @@ def find_cell_end(self, lines): if self.metadata and 'cell_type' in self.metadata: self.cell_type = self.metadata.pop('cell_type') + elif not is_active('.ipynb', self.metadata): + if self.metadata.get('active') == '': + del self.metadata['active'] + self.cell_type = 'raw' else: self.cell_type = 'code' diff --git a/jupytext/cell_to_text.py b/jupytext/cell_to_text.py index 71a233546..5f02ad69a 100644 --- a/jupytext/cell_to_text.py +++ b/jupytext/cell_to_text.py @@ -377,20 +377,20 @@ def __init__(self, *args, **kwargs): def cell_to_text(self): """Return the text representation for the cell""" - if self.cell_type != 'code': - self.metadata['cell_type'] = self.cell_type - active = is_active(self.ext, self.metadata, same_language(self.language, self.default_language)) if self.cell_type == 'raw' and 'active' in self.metadata and self.metadata['active'] == '': del self.metadata['active'] + if not self.is_code(): + self.metadata['cell_type'] = self.cell_type + options = metadata_to_double_percent_options(self.metadata, self.cell_metadata_json) if options.startswith('%') or not options: lines = [self.comment + ' %%' + options] else: lines = [self.comment + ' %% ' + options] - if self.cell_type == 'code' and active: + if self.is_code() and active: source = copy(self.source) comment_magic(source, self.language, self.comment_magics) if source == ['']: diff --git a/tests/test_active_cells.py b/tests/test_active_cells.py index 712c9c3d5..b360cd7f5 100644 --- a/tests/test_active_cells.py +++ b/tests/test_active_cells.py @@ -243,3 +243,43 @@ def test_active_rmd(ext, no_jupytext_version_number): @pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R']) def test_active_not_include_rmd(ext, no_jupytext_version_number): check_active_cell(ext, ACTIVE_NOT_INCLUDE_RMD) + + +def test_active_cells_from_py_percent(text="""# %% active="py" +print('should only be displayed in py file') + +# %% tags=["active-py"] +print('should only be displayed in py file') + +# %% active="ipynb" +# print('only in jupyter') +"""): + """Example taken from https://github.com/mwouts/jupytext/issues/477""" + nb = jupytext.reads(text, 'py:percent') + assert nb.cells[0].cell_type == 'raw' + assert nb.cells[1].cell_type == 'raw' + assert nb.cells[2].cell_type == 'code' + assert nb.cells[2].source == "print('only in jupyter')" + + text2 = jupytext.writes(nb, 'py:percent') + compare(text2, text) + + +def test_active_cells_from_py_light(text="""# + active="py" +print('should only be displayed in py file') + +# + tags=["active-py"] +print('should only be displayed in py file') + +# + active="ipynb" +# print('only in jupyter') +"""): + """Example adapted from https://github.com/mwouts/jupytext/issues/477""" + nb = jupytext.reads(text, 'py') + assert nb.cells[0].cell_type == 'raw' + assert nb.cells[1].cell_type == 'raw' + assert nb.cells[2].cell_type == 'code' + assert nb.cells[2].source == "print('only in jupyter')" + + text2 = jupytext.writes(nb, 'py') + compare(text2, text)