marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Support async/await in cells

Open ianschmitz opened this issue 1 year ago • 2 comments

Description

It would be great to support await within a cell.

For example it would be great to do something like:

from my_lib import func_from_my_lib

await func_from_my_lib()

Currently it gives an error:

line 1 SyntaxError: 'await' outside function

The motivation for this is being able to interact with existing async functions from a marimo notebook. Presumably if this was supported I could create async functions within the notebook as well.

Suggested solution

Allow await within a cell. A possible implementation may look like (note my Python is quite rusty):

asyncio.get_event_loop().run_until_complete(cell())

This would assume that the cell was defined using async def instead of def like it is today.

Alternative

No response

Additional context

No response

ianschmitz avatar Jan 17 '24 01:01 ianschmitz

Thanks for the feature request -- we can explore this. In the meantime, even though it's inconvenient, is it possible for you to use asyncio directly to run async functions? For example,

import marimo

__generated_with = "0.1.77"
app = marimo.App()


@app.cell
def __(asyncio):
    async def foo():
        await asyncio.sleep(2)
        print('done sleeping')
    return foo,


@app.cell
def __(asyncio, foo):
    asyncio.run(foo())
    return


@app.cell
def __():
    import asyncio
    return asyncio,


if __name__ == "__main__":
    app.run()

akshayka avatar Jan 17 '24 02:01 akshayka

Yes that works @akshayka. Not as ergonomic as having proper support but it is functional!

ianschmitz avatar Jan 18 '24 01:01 ianschmitz

Closed in #795.

ianschmitz avatar Feb 22 '24 06:02 ianschmitz