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 a cell decorator #5

Closed
MSeal opened this issue Mar 16, 2020 · 8 comments
Closed

Execute a cell decorator #5

MSeal opened this issue Mar 16, 2020 · 8 comments
Labels
enhancement New feature or request GSoC-2020 Part of Google Summer of Code 2020
Milestone

Comments

@MSeal
Copy link
Member

MSeal commented Mar 16, 2020

Add the most basic "execute indicated cell" before unittest runs decorator. This will be used to prep a test case for evaluation.

@MSeal MSeal added the enhancement New feature or request label Mar 16, 2020
@rohitsanj
Copy link
Member

rohitsanj commented Mar 18, 2020

Hey, I'm looking into this.

So are we looking at some functionality like..

@execute_cell(3)
def test_foo():
  pass

If yes,
How and where do we define which notebook file is being executed?

Also, I'm guessing that it should actually be testbook.execute_cell but I guess we could wrap it all later?

@MSeal
Copy link
Member Author

MSeal commented Mar 18, 2020

Yes something like this would be good.

testbook.execute_cell is correct, but you can assume people may do from testbook import execute_cell to get back to the function name alone.

I see two ways we can support which notebook to be executing. First is to require a separate decorator first (e.g. @testbook.notebook(path_to_notebook) which adds the notebook keyword argument to the function call) or as as an optional argument to execute_cell(path_to_notebook, 3). I imagine we'll want to support both patterns, but we can just use one for now to make it easier. Either one should add notebook as an argument to the test_foo(notebook) call so the in-memory object can be accessed / tested. At first this can just be the nbformat / nbclient response but we likely will want to make a wrapper objects with test assertion calls available.

@rohitsanj
Copy link
Member

rohitsanj commented Mar 18, 2020

I imagine we'll want to support both patterns, but we can just use one for now to make it easier.

Alright, I'll go ahead with adding an optional argument for now.

At first this can just be the nbformat / nbclient response but we likely will want to make a wrapper objects with test assertion calls available.

I'm not sure what you meant by this? 😅

@MSeal
Copy link
Member Author

MSeal commented Mar 18, 2020

At first this can just be the nbformat / nbclient response but we likely will want to make a wrapper objects with test assertion calls available.

I'm not sure what you meant by this? sweat_smile

So when you load (or execute) a notebook you get this nbformat object which is basically a dict holding the v4 notebook schema. This contains things like tags, cells, outputs, metadata, etc. In our testbook case we want to expose the values therein so people can make assertions against the fields. But we likely will want to add additional functions like notebook.assert_output_text('I printed this message') for convienence. This means we'll want to make a new wrapping object which mimics or inherits from nbformat's document kind of like https://github.com/nteract/scrapbook/blob/master/scrapbook/models.py#L40

@rohitsanj
Copy link
Member

So I tried to use NotebookClient.execute_cell as follows:

# assume that the following is inside a decorator
with open(notebook_filename) as f:
    nb = nbformat.read(f, as_version=4)

client = NotebookClient(nb)
client.execute_cell(nb['cells'][cell_index], cell_index)

@execute_cell('../testbook_example.ipynb', 2)
def foo():
    pass

foo()

It throws an AttributeError

File "/Users/rohitsanjay/miniconda3/envs/testbook/lib/python3.7/site-packages/nbclient/client.py", line 528, in execute_cell
    parent_msg_id = self.kc.execute(
AttributeError: 'NoneType' object has no attribute 'execute'

I looked into the code in client.py and found that setup_kernel is not being used in execute_cell and hence in async_execute_cell.

I'm not sure if this a bug or not. @MSeal

@rohitsanj
Copy link
Member

At first this can just be the nbformat / nbclient response but we likely will want to make a wrapper objects with test assertion calls available.

I'm not sure what you meant by this? sweat_smile

So when you load (or execute) a notebook you get this nbformat object which is basically a dict holding the v4 notebook schema. This contains things like tags, cells, outputs, metadata, etc. In our testbook case we want to expose the values therein so people can make assertions against the fields. But we likely will want to add additional functions like notebook.assert_output_text('I printed this message') for convienence. This means we'll want to make a new wrapping object which mimics or inherits from nbformat's document kind of like https://github.com/nteract/scrapbook/blob/master/scrapbook/models.py#L40

Thanks for the explanation. It makes perfect sense now 😄

@MSeal
Copy link
Member Author

MSeal commented Mar 18, 2020

I looked into the code in client.py and found that setup_kernel is not being used in execute_cell and hence in async_execute_cell.

I'm not sure if this a bug or not. @MSeal

We're refactoring that code a bit -- make sure to use the released 0.1 code for now -- we'll be keeping that contract as best we can before 0.2 releases.

For now I would look at how papermill extends it to prep/wrap nbclient: https://github.com/nteract/papermill/blob/master/papermill/clientwrap.py#L30-L70. We can also update nbclient to have better extension interfaces in the future as we progress.

@rohitsanj
Copy link
Member

rohitsanj commented Mar 18, 2020 via email

@rohitsanj rohitsanj added this to the 0.1.0 milestone Jun 18, 2020
@rohitsanj rohitsanj added the GSoC-2020 Part of Google Summer of Code 2020 label Aug 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request GSoC-2020 Part of Google Summer of Code 2020
Projects
None yet
Development

No branches or pull requests

2 participants