From aa79f8d7e6f4c4245db81c0aa6edfb9f4b09900b Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sun, 6 Aug 2017 21:56:01 -0700 Subject: [PATCH] tests now test correct behaviour, tests pass --- nbconvert/preprocessors/tagremove.py | 14 +++++++------- nbconvert/preprocessors/tests/test_tagremove.py | 10 ++++------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/nbconvert/preprocessors/tagremove.py b/nbconvert/preprocessors/tagremove.py index 3de511ddb..59e73fbea 100644 --- a/nbconvert/preprocessors/tagremove.py +++ b/nbconvert/preprocessors/tagremove.py @@ -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): @@ -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, @@ -84,7 +85,7 @@ 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. @@ -92,10 +93,9 @@ def check_output_conditions(self, output, resources, 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 diff --git a/nbconvert/preprocessors/tests/test_tagremove.py b/nbconvert/preprocessors/tests/test_tagremove.py index 75f6b2936..dd85922d0 100644 --- a/nbconvert/preprocessors/tests/test_tagremove.py +++ b/nbconvert/preprocessors/tests/test_tagremove.py @@ -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", @@ -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, @@ -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)