holoviz / param

Param: Make your Python code clearer and more reliable by declaring Parameters
https://param.holoviz.org
BSD 3-Clause "New" or "Revised" License
410 stars 69 forks source link

Let reactive expressions .watch method support async functions #913

Closed MarcSkovMadsen closed 3 months ago

MarcSkovMadsen commented 4 months ago

I'm trying to make an async .watch example for the Panel basic tutorials.

It seems reactive expressions .watch does not support async functions.

import asyncio
import concurrent.futures
from time import sleep

import panel as pn

pn.extension()

is_stopped=pn.rx(True)
is_active = pn.rx(False)

def name(stopped):
    if stopped:
        return "Start the wind turbine"
    else:
        return "Stop the wind turbine"

rx_name = pn.rx(name)(is_stopped)

submit = pn.widgets.Button(name=rx_name, disabled=is_active, loading=is_active)

async def start_stop_wind_turbine(clicked):
    if not clicked:
        return
    is_active.rx.value=True
    with submit.param.update(loading=True, disabled=True):
        with concurrent.futures.ThreadPoolExecutor() as executor:
            future = executor.submit(sleep, 1)
            result = await asyncio.wrap_future(future)

        is_stopped.rx.value = not is_stopped.rx.value
    is_active.rx.value=False
    print("done")

b_stop_wind_turbine = submit.rx.watch(start_stop_wind_turbine)

pn.Column(submit, b_stop_wind_turbine, is_active).servable()

When I click the button I see

RuntimeWarning: coroutine 'start_stop_wind_turbine' was never awaited