Skip to content

Instantly share code, notes, and snippets.

@chrisseto
Last active March 18, 2019 07:13
Show Gist options
  • Save chrisseto/8f7289497f40ad442a30 to your computer and use it in GitHub Desktop.
Save chrisseto/8f7289497f40ad442a30 to your computer and use it in GitHub Desktop.
from IPython.core.magic import Magics, magics_class, line_magic
import asyncio
@magics_class
class AsyncMagics(Magics):
@line_magic
def await(self, line):
return asyncio.get_event_loop().run_until_complete(eval(line, self.shell.user_global_ns, self.shell.user_ns))
def load_ipython_extension(ipython):
ipython.register_magics(AsyncMagics)
@chrisseto
Copy link
Author

Usage:

>>> %install_ext https://gist.githubusercontent.com/chrisseto/8f7289497f40ad442a30/raw/4a50eb0b67a2fb8e7f027a8f279c0226ad91c297/await.py
Installed await.py. To use it, type:
  %load_ext await
>>> %load_ext await
>>> import aiohttp
>>> %await aiohttp.request('GET', 'http://google.com')
<ClientResponse(http://www.google.com/) [200 OK]>
<CIMultiDictProxy('DATE': 'Sun, 25 Oct 2015 23:34:21 GMT', 'EXPIRES': '-1', 'CACHE-CONTROL': 'private, max-age=0', 'CONTENT-TYPE': 'text/html; charset=ISO-8859-1', 'P3P': 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."', 'CONTENT-ENCODING': 'gzip', 'SERVER': 'gws', 'CONTENT-LENGTH': '7319', 'X-XSS-PROTECTION': '1; mode=block', 'X-FRAME-OPTIONS': 'SAMEORIGIN', 'SET-COOKIE': 'PREF=ID=1111111111111111:FF=0:TM=1445816061:LM=1445816061:V=1:S=9vM2LcGVfeFu2r8L; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.com', 'SET-COOKIE': 'NID=72=SlAbNGKXBwWSWU6yG2RwqnQIQd23pURav9-n68bND5ynmSG3J7-Qu6-dcwktKH4t3Nsk0fxTmT_I86KoA256IiVqGfnGwFUbyDTHwK8SefAaWr8yOEzqlO6iOVFsk2cfTUrnr4-9s7ef_KaYtXV8Rt5ZYheBnqomdJbYgAuYLgYjK3_AYLrHUuMTetc; expires=Mon, 25-Apr-2016 23:34:21 GMT; path=/; domain=.google.com; HttpOnly')>

@rickardraysearch
Copy link

I wanted to be able to use it with concurrent.futures as well, and ended up with the following:

from IPython.core.magic import Magics, magics_class, line_magic
import asyncio
from concurrent.futures import Future

@magics_class
class AsyncMagics(Magics):
    @line_magic
    def await(self, line):
        value = eval(line, self.shell.user_global_ns, self.shell.user_ns)
        if isinstance(value, Future):
            value = asyncio.wrap_future(value)
        return asyncio.get_event_loop().run_until_complete(value)


def load_ipython_extension(ipython):
    ipython.register_magics(AsyncMagics)

@lars-tiede
Copy link

Could we make this pip installable, say as package "ipython-await-magics" (analogous to https://pypi.python.org/pypi/ipython-pip-magics)?

The reason I'm asking is because IPython deprecated %install_ext (ipython/ipython#8601). The recommended way of doing things is pip, apparently.

I would volunteer to do it. In that case, I guess you'd have to attach a license to await.py so I can use it.

(update: @chrisseto and @rickardraysearch, did you get notified about this?)

@regnarg
Copy link

regnarg commented Nov 10, 2017

My ipython does not have %install_ext, I had to copy it into ~/.ipython/extensions/await.py. To autoload it on every start, you can set the following in the config file (probably ~/.ipython/profile_default/ipython_config.py):

c = get_config()
c.InteractiveShellApp.extensions = [
    'await'
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment