-
Notifications
You must be signed in to change notification settings - Fork 55
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
Is it possible to programmatically inspect variables of a NotebookNode? #277
Comments
Nbclient runs the code in a kernel, which is a separate process, so (de)serialization is the only way to get a result back. |
Sure, that's an option. But since there are IDEs that let you inspect variables in a running Jupyter notebook, I thought they might have some other way of doing this. Like a function for client/kernel communication using serialization over a socket or pipe, rather than manually mucking about with files. |
I'm not talking about files, cell outputs are broadcast over the IOPub channel, which is a ZMQ socket. |
Ooh, ok thank you - that makes enough sense for me to see why it would exist, but it's new enough to me that I'm not 100% sure what to do with it. Apologies if some of my replies don't make sense - I'm not too accustomed to concepts like sockets and channels. Since cell outputs can be repr's rather and are not necessarily serialized data, is it correct that I would need to have the
|
I've not tried, but does this work? import base64
import pickle
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
nb = nbformat.v4.new_notebook()
nb["cells"] = [nbformat.v4.new_code_cell(
source="foo=1\n"
"print(base64.b64encode(pickle.dumps(foo)))"
)]
ep = ExecutePreprocessor(timeout=-1)
ep.preprocess(nb) result = pickle.loads(base64.b64decode(nb["cells"][0]["outputs"][0]["text"][:-1])) |
Asking in nbclient repo because
nbconvert.preprocessors.ExecutePreprocessor
gets it'sexecute_cell
method fromnbclient.client.NotebookClient
, so I hope this is the right place.I've been trying to run certain experiments in a Jupyter Notebook in order to take advantage of the nice layout of code and plots. However, in order to do so repeatably and store other metadata, I set up and run the notebook in memory and convert to HTML.
When I want to use the results of the experiment in some of the calling code, I currently print it in a
nbformat.notebooknode.NotebookNode
cell and then read a notebook cell's ["outputs"] value. But for objects whoserepr
isn't enough to reconstruct the object, is there a way to inspect the global scope of theNotebookNode
?MWE:
Currently, I run
Desired
The text was updated successfully, but these errors were encountered: