Search code examples
jupyter-notebookipywidgets

Widget callback does not fire while another notebook cell is executing


I have a cell that produces a button that invokes a function when clicked:

stop_button = widgets.Button(description='Stop Motor')
stop_button.on_click(stop_motor)

When another cell is executing, however, the button's callback is not fired (even though the button appears to respond to the click).

Can I make the button always work, even when another cell is executing?


Solution

  • I'm not sure this is the best answer, but I made my longer-running task asynchronous:

    %gui asyncio
    import asyncio
    
    async def work():
        while True:
            await asyncio.sleep(0.1)
    
    task = asyncio.create_task(work())
    

    However you would need to repeat this pattern for every cell that might block you from clicking your button.