Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let reactive expressions .watch method support async functions #913

Closed
Tracked by #6425
MarcSkovMadsen opened this issue Mar 3, 2024 · 0 comments · Fixed by #917
Closed
Tracked by #6425

Let reactive expressions .watch method support async functions #913

MarcSkovMadsen opened this issue Mar 3, 2024 · 0 comments · Fixed by #917
Labels
type-feature Feature request
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Mar 3, 2024

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
@MarcSkovMadsen MarcSkovMadsen added type-feature Feature request TRIAGE User-submitted issue that hasn't been triaged yet. labels Mar 3, 2024
@philippjfr philippjfr removed the TRIAGE User-submitted issue that hasn't been triaged yet. label Mar 3, 2024
@philippjfr philippjfr added this to the 2.1.0 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature Feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants