Skip to content
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

Execute preprocess cells #1380

Merged
merged 9 commits into from
Sep 13, 2020
Merged

Execute preprocess cells #1380

merged 9 commits into from
Sep 13, 2020

Conversation

MSeal
Copy link
Contributor

@MSeal MSeal commented Sep 12, 2020

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
Copy link
Contributor Author

MSeal commented Sep 12, 2020

Forgot to mention this fixes #1370

Copy link
Member

@willingc willingc left a comment

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.

nbconvert/preprocessors/execute.py Outdated Show resolved Hide resolved
nbconvert/preprocessors/execute.py Outdated Show resolved Hide resolved
nbconvert/preprocessors/execute.py Outdated Show resolved Hide resolved
nbconvert/preprocessors/execute.py Outdated Show resolved Hide resolved
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
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
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
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
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
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
Copy link
Contributor Author

MSeal commented Sep 12, 2020

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

Future

@MSeal MSeal merged commit b52a82d into jupyter:master Sep 13, 2020
@SylvainCorlay
Copy link
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
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
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
Member

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

@MSeal
Copy link
Contributor Author

MSeal commented Sep 14, 2020

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
Member

Maybe we could revive jupyter/nbclient#79?

@MSeal
Copy link
Contributor Author

MSeal commented Sep 14, 2020

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