Skip to content

Execute preprocess cells#1380

Merged
MSeal merged 9 commits into
jupyter:masterfrom
MSeal:executePreprocessCells
Sep 13, 2020
Merged

Execute preprocess cells#1380
MSeal merged 9 commits into
jupyter:masterfrom
MSeal:executePreprocessCells

Conversation

@MSeal

@MSeal MSeal commented Sep 12, 2020

Copy link
Copy Markdown
Contributor

Fixes inheritance models where subclasses are used to implement preprocess_cells on the execute preprocessor.

There's one hack in this process, but the alternatives are all worse for maintenance pov (including some ones I didn't mention like not dual inheriting classes) so it is what it is to maintain backwards and forward compatibility.

@MSeal

MSeal commented Sep 12, 2020

Copy link
Copy Markdown
Contributor Author

Forgot to mention this fixes #1370

@willingc willingc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSeal Thanks for doing this. I've suggested some modifications for clarity. Consider this approved once the failing test is resolved.

There are a lot of deprecation warnings. Do we want to handle in docs or open an issue for future cleanup.

Comment thread nbconvert/preprocessors/execute.py Outdated
Comment thread nbconvert/preprocessors/execute.py Outdated
Comment thread nbconvert/preprocessors/execute.py Outdated
Comment thread nbconvert/preprocessors/execute.py Outdated
Comment thread nbconvert/preprocessors/execute.py Outdated
if resources:
self.resources = resources
self.reset_execution_trackers()
# This may look wrong, but preprocess acts like an init on execution state and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move all of this into docstring with an important directive so that this populates through to the docs.

Index of the cell being processed
"""
self._check_assign_resources(resources)
cell = run_sync(NotebookClient.async_execute_cell)(self, cell, index, store_history=self.store_history)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's comment this line to unpack what is happening here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do subclasses need to call super().preprocess_cell() now? (IIRC they didn't need to before; it's fine either way, although if this has changed it would be great to document the breaking change in the release notes.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSeal ^^ I'm happy to document the response.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably best to always call super for this preprocessor now.

Technically you don't have to so long as you call something equivilent to or exactly NotebookClient.async_execute_cell, but that's fairly complex to explain or expect given the subtlety of the multi-inheritance and overrides at play.

@willingc Documenting as such would be a good idea, happy to merge if you want to make the PR

MSeal and others added 4 commits September 12, 2020 16:46
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
@MSeal

MSeal commented Sep 12, 2020

Copy link
Copy Markdown
Contributor Author

There are a lot of deprecation warnings. Do we want to handle in docs or open an issue for future cleanup.

Future

Comment thread nbconvert/preprocessors/execute.py Outdated
@MSeal MSeal merged commit b52a82d into jupyter:master Sep 13, 2020
@SylvainCorlay

Copy link
Copy Markdown
Member

cc @davidbrochart

"""
self._check_assign_resources(resources)
# Because nbclient is an async library, we need to wrap the parent async call to generate a syncronous version.
cell = run_sync(NotebookClient.async_execute_cell)(self, cell, index, store_history=self.store_history)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSeal You could have called NotebookClient.execute_cell here, right?
Shouldn't this cell execution be in async_execute_cell?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, possibly. I was worried any refactors on the wrapping pattern for NotebookClient.execute_cell might make it lazy, causing in infinite loop here in the future since this overwrites async_execute_cell in this child class to allow for intercepting and running preprocess_cell... basically it's a hack to allow us to intercept the the execute_cell call from nbclient and run whatever the nbconvert inheritance tree demands.

@davidbrochart

Copy link
Copy Markdown
Member

Shouldn't preprocess_cell be in nbclient? I have the feeling that it would have led to a cleaner design.

@MSeal

MSeal commented Sep 14, 2020

Copy link
Copy Markdown
Contributor Author

Shouldn't preprocess_cell be in nbclient? I have the feeling that it would have led to a cleaner design.

Should nbclient have pre/post execute hooks? Yes probably. I went with a solution here that could unblock the issue with dependencies as is. I think someone was working on adding these in a similar flavor to papermill's hooks but since it hasn't landed it's likely not an active effort. I would not copy the preprocess_cell pattern itself, but if we had the ability to register callbacks in nbclient it would help make this simpler for sure.

@davidbrochart

Copy link
Copy Markdown
Member

Maybe we could revive jupyter/nbclient#79?

@MSeal

MSeal commented Sep 14, 2020

Copy link
Copy Markdown
Contributor Author

Maybe, or making a more concentrated PR for just hook operations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants