# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_notebook_context.ipynb.

# %% auto 0
__all__ = ['get_notebook_data']

# %% ../nbs/01_notebook_context.ipynb 4
from ipykernel.comm import Comm
import asyncio

async def get_notebook_data(timeout=0.1):
    future = asyncio.Future()
    
    def on_msg(msg):
        if not future.done():
            future.set_result(msg['content']['data'])
    
    comm = Comm(target_name='wordslab_notebook_comm', show_warning=False)
    comm.on_msg(on_msg)
    comm.send({'request': 'get_notebook_data'})

    try:
        return await asyncio.wait_for(future, timeout=timeout)
    except asyncio.TimeoutError:
        raise TimeoutError("Failed to receive notebook context from frontend")

# The first call always fails, I didn't find a way around it
try:
    await get_notebook_data()
except:
    ...
