Skip to content

Commit

Permalink
tests now test correct behaviour, tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacer committed Aug 7, 2017
1 parent 51e9d02 commit aa79f8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 7 additions & 7 deletions nbconvert/preprocessors/tagremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def check_cell_conditions(self, cell, resources, index):
"""

# Return true if any of the tags in the cell are removable.
return not any([tag in cell.get('tags', [])
return not any([tag in cell.get('metadata', {}).get('tags', [])
for tag in self.remove_cell_tags])

def preprocess(self, nb, resources):
Expand All @@ -63,15 +63,16 @@ def preprocess_cell(self, cell, resources, cell_index):
Apply a transformation on each cell. See base.py for details.
"""

if any([tag in cell.get('tags', [])
for tag in self.remove_all_output_tags]):
if (any([tag in cell.get('metadata', {}).get('tags',[])
for tag in self.remove_all_output_tags])
and cell.cell_type == 'code'):
cell.outputs = []
cell.execution_count = None
# Remove metadata associated with output
if 'metadata' in cell:
for field in self.remove_metadata_fields:
cell.metadata.pop(field, None)
if cell.outputs:
if cell.get('outputs', {}):
cell.outputs = [self.preprocess_output(output,
resources,
cell_index,
Expand All @@ -84,18 +85,17 @@ def preprocess_cell(self, cell, resources, cell_index):
]
return cell, resources

def check_output_conditions(self, output, resources,
def check_output_conditions(self, output, resources,
cell_index, output_index):
"""
Checks that an output has a tag that indicates removal.
Returns: Boolean.
True means output should *not* be removed.
"""
return not any([tag in output.metadata.get('tags', [])
return not any([tag in output.get('metadata', {}).get('tags', [])
for tag in self.remove_single_output_tags])


def preprocess_output(self, output, resources,
cell_index, output_index):
return output, resources
10 changes: 4 additions & 6 deletions nbconvert/preprocessors/tests/test_tagremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def build_notebook(self):
])
outputs_to_be_removed = [
nbformat.new_output("display_data",
data={'text/plain': "remove_my_output"},
metadata={
"tags": ["hide_all_outputs"]
}),
data={'text/plain': "remove_my_output"}),
]
outputs_to_be_kept = [
nbformat.new_output("stream",
Expand All @@ -39,7 +36,8 @@ def build_notebook(self):
notebook.cells.extend(
[nbformat.new_code_cell(source="display('remove_my_output')",
execution_count=2,
outputs=outputs_to_be_removed),
outputs=outputs_to_be_removed,
metadata={"tags": ["hide_all_outputs"]}),

nbformat.new_code_cell(source="print('remove this cell')",
execution_count=3,
Expand Down Expand Up @@ -74,5 +72,5 @@ def test_output(self):
nb, res = preprocessor(nb, res)

self.assertEqual(len(nb.cells), 3)
self.assertEqaul(len(nb.cells[-1].outputs), 0)
self.assertEqual(len(nb.cells[-1].outputs), 0)
self.assertEqual(len(nb.cells[0].outputs), 8)

0 comments on commit aa79f8d

Please sign in to comment.