Skip to content

Commit

Permalink
Fix active cells in the py:percent format
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Apr 3, 2020
1 parent 6c0f027 commit 07fb597
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
4 changes: 4 additions & 0 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 4 additions & 4 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ['']:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_active_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 07fb597

Please sign in to comment.