Setting loading=True doesn't work with async/threads

I have a button that runs a long running function. I have it so that it sets the button widget to loading while it runs. This works fine but if I run panel with threads or change the on_click function to async, the spinner doesn’t show. How do I get this to work?

class MyView(pn.Viewable.Viewer):                                                     
    def __init__(self, **params):                                                     
        self.submit_button = pn.widgets.Button(name="Submit", button_type="primary")
        self.submit_button.on_click(self.on_click)
                                                                                      
    def on_click(self):                                                               
        with self.submit_button.param.update(loading=True):                           
            time.sleep(10)                        

I gave it another try and it seems to work now. My code had changed since I last tried so maybe the reworked code allowed it to work.

class MyView(pn.Viewable.Viewer):                                                     
    def __init__(self, **params):                                                     
        self.submit_button = pn.widgets.Button(name="Submit", button_type="primary")
        self.submit_button.on_click(self.on_click)
                                                                                      
    async def on_click(self):                                                               
        with self.submit_button.param.update(loading=True):                           
            await asyncio.get_event_loop().run_in_executor(None, long_running_func, args)      
1 Like