New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored execute preprocessor to have a process_message function #905
Changes from all commits
72bac11
6ed3f0b
a08dc68
6cbad47
9ed71d7
a8b4989
498d886
2f9c697
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
from ..execute import ExecutePreprocessor, CellExecutionError, executenb | ||
|
||
import IPython | ||
from mock import MagicMock | ||
from traitlets import TraitError | ||
from nbformat import NotebookNode | ||
from jupyter_client.kernelspec import KernelSpecManager | ||
|
@@ -51,7 +52,6 @@ def _normalize_base64(b64_text): | |
except (ValueError, TypeError): | ||
return b64_text | ||
|
||
|
||
class ExecuteTestBase(PreprocessorTestsBase): | ||
def build_preprocessor(self, opts): | ||
"""Make an instance of a preprocessor""" | ||
|
@@ -185,18 +185,18 @@ def normalize_output(output): | |
def assert_notebooks_equal(self, expected, actual): | ||
expected_cells = expected['cells'] | ||
actual_cells = actual['cells'] | ||
self.assertEqual(len(expected_cells), len(actual_cells)) | ||
assert len(expected_cells) == len(actual_cells) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huzzah! Many thanks for switching to using the pytest |
||
|
||
for expected_cell, actual_cell in zip(expected_cells, actual_cells): | ||
expected_outputs = expected_cell.get('outputs', []) | ||
actual_outputs = actual_cell.get('outputs', []) | ||
normalized_expected_outputs = list(map(self.normalize_output, expected_outputs)) | ||
normalized_actual_outputs = list(map(self.normalize_output, actual_outputs)) | ||
self.assertEqual(normalized_expected_outputs, normalized_actual_outputs) | ||
assert normalized_expected_outputs == normalized_actual_outputs | ||
|
||
expected_execution_count = expected_cell.get('execution_count', None) | ||
actual_execution_count = actual_cell.get('execution_count', None) | ||
self.assertEqual(expected_execution_count, actual_execution_count) | ||
assert expected_execution_count == actual_execution_count | ||
|
||
|
||
def test_constructor(self): | ||
|
@@ -395,6 +395,30 @@ def test_custom_kernel_manager(self): | |
for method, call_count in expected: | ||
self.assertNotEqual(call_count, 0, '{} was called'.format(method)) | ||
|
||
def test_process_message_wrapper(self): | ||
outputs = [] | ||
|
||
class WrappedPreProc(ExecutePreprocessor): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if we directly created the context needed so that I'd prefer if we weren't concatenating all of the outputs into a single object for the whole notebook, I'm thinking it should follow the cell structure. I'd also like to make sure that message types that are supposed to return None are returning None, which means keeping track of at least the msg type in relation to the output if not some of the more predictive fields (e.g., a display_id that we know is set to a particular value). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done -- this is rebased off #982 now |
||
def process_message(self, msg, cell, cell_index): | ||
result = super(WrappedPreProc, self).process_message(msg, cell, cell_index) | ||
if result: | ||
outputs.append(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this still need special logic distinct from the standard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's purely to test that one could wrap an retrieve outputs to process on it's own. It has no real purpose for doing so beyond showing how to wrap the function in inherited classes. Namely to prove we can wrap the function safely in papermill cleanly. |
||
return result | ||
|
||
current_dir = os.path.dirname(__file__) | ||
filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb') | ||
|
||
with io.open(filename) as f: | ||
input_nb = nbformat.read(f, 4) | ||
|
||
original = copy.deepcopy(input_nb) | ||
wpp = WrappedPreProc() | ||
executed = wpp.preprocess(input_nb, {})[0] | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
assert outputs == [ | ||
{'name': 'stdout', 'output_type': 'stream', 'text': 'Hello World\n'} | ||
] | ||
self.assert_notebooks_equal(original, executed) | ||
mpacer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_execute_function(self): | ||
# Test the executenb() convenience API | ||
filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously there was a
continue
in the case where there was anupdate_display_data
message with a statement that# update_display_data doesn't get recorded
am I correct that that logic is now implicit if it's present at all?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MSeal could you respond to this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late response (post rebase), but yes it's implicit by not raising CellExecutionComplete here