Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call await directly in Jupyter cell?

I've seen examples of such code working in certain environments (eg https://github.com/ipython/ipykernel/pull/323).

But for me this only raises SyntaxError so far:

enter image description here

Update:

I know that in "pure" python it won't work and await is supposed to be used in async def's only.

I also know that in jupyter it is possible (see link above) so the engine is capable of doing this. The question is how to get rid of this error message.

Update2 What's the idea of calling await directly:

I'm working with an async library. In current version of jupyter it's hard to get the return value of a coroutine:

enter image description here The best thing I've come up to is through global variables:

enter image description here

One of the problems here is that res is not ready in the cell 25 yet. I have to wait until I see "done", then use the value in the another cell.

Another problem is semantic asymmetry between calling a coroutine from the cell directly and from another coroutine, which is pretty irrelevant when writing scripts in an editor, but quite crucial for repl.

Now consider this:

enter image description here

like image 447
axil Avatar asked Jun 13 '18 09:06

axil


1 Answers

Got it!

enter image description here

This needs development version of IPython to be installed (https://github.com/ipython/ipython/pull/11155).

Which is:

pip install git+https://github.com/minrk/ipython.git@more-await-repl#egg=ipython

pip install git+https://github.com/minrk/ipykernel.git@run-async#egg=ipykernel

Update:

As noted by @ack, nowadays this functionality is present in the latest version ipykernel on pypi.

like image 150
axil Avatar answered Oct 31 '22 03:10

axil