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

Register events and access the runned cell content #891

Closed
theevann opened this issue Dec 6, 2019 · 2 comments
Closed

Register events and access the runned cell content #891

theevann opened this issue Dec 6, 2019 · 2 comments

Comments

@theevann
Copy link

theevann commented Dec 6, 2019

I would like to be able to register events on colab like in Jupyter, and then access the runned cell content using: IPython.get_ipython().events.register('post_run_cell', function)

  • In Jupyter, post_run_cell is called with an argument (an ExecutionResult object) as you can see here, so its signature is post_run_cell(ExecutionResult result).
    But in colab, there is no argument given to post_run_cell. Should it be considered a bug ?

  • I tried a workaround using the last_execution_result variable (also an ExecutionResult object), with IPython.get_ipython().last_execution_result but once again its seems to work in Jupyter only and I didn't find any equivalent. Is there any ?

In Jupyter, any of these two gives me an ExecutionResult object, from which I can get the cell raw input: result.info.raw_cell.

I finally found a workaround that would work only in colab :

import IPython
ip = IPython.get_ipython()
def f():
    cell_content = ip.get_parent()['content']['code']
ip.events.register('post_run_cell', f)

My questions : Are all these differences normal ? Is there a "cross-platform" way to do it ?

@blois
Copy link
Contributor

blois commented Dec 6, 2019

I think the issue is the version of IPython that Colab is on right now. If I upgrade the kernel then it appears to work as you're expecting:
https://colab.research.google.com/gist/blois/057009f08ff1b4d6b7142a511a04dad1/post_run_cell.ipynb

@colaboratory-team
Copy link
Contributor

colaboratory-team commented Dec 6, 2019

Colab's IPython is currently on version 5.5 (version 6.0 dropped Python 3 support, and Colab still supports Python 2 kernels). If you're interested in using IPython events in IPython version 5.X, you can follow the documentation at https://ipython.readthedocs.io/en/5.x/config/callbacks.html.

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

No branches or pull requests

3 participants