DataFrame table does not get rendered with on_click() event widgets

Hello I can’t seem to get a dataframe table rendered using hvplot.table() or pn.panel.DataFrame or pn.widgets.DataFrame when using an on_click() event. Outside of an on_click() event, I can get a dataframe to render fine.

panel==0.12.6
bokeh==2.4.2
hvplot==0.7.3
jupyterlab==3.2.3

With the code below, nothing gets rendered when I click on the button.

import pandas as pd
import panel as pn
import hvplot.pandas

button = pn.widgets.Button(name='Click Me', button_type='primary')

def my_func(event=None):
    df = pd.read_csv('https://raw.githubusercontent.com/rashida048/Datasets/master/cars.csv')
    
    print(df.head())

    return df.hvplot.table()

button.on_click(my_func)

pn.Column(button)

Printing the df works so I know I have a known good dataframe.

However, if I make my_func() a “standalone” function or not with on_click() event, the dataframe gets rendered fine:

import pandas as pd
import panel as pn
import hvplot.pandas

def my_func():
    df = pd.read_csv('https://raw.githubusercontent.com/rashida048/Datasets/master/cars.csv')
    
    return df.hvplot.table()

my_func()

I ran pn.extension() also, but still the same no good result. I have jupyterlab version 3.2.3.

Any help with this is much appreciated!

Daniel

On click only runs the function, it does not add it to the layout. Try something like this:

import pandas as pd
import panel as pn
import hvplot.pandas


def my_func(click):
    if click:
        df = pd.read_csv('https://raw.githubusercontent.com/rashida048/Datasets/master/cars.csv')
        return df.hvplot.table()

    
button = pn.widgets.Button(name='Click Me', button_type='primary')
pn.Column(button, pn.bind(my_func, button))

Thank you @Hoxbro ! Could you point me to documentation that references pn.bind? I looked at the documentation for using the button, but doesn’t seem to mention pn.bind(). Thanks!

Try to take a look here: APIs — Panel 0.12.6 documentation

1 Like

.bind is the favourited api of Panel currently (over .watch, .depends, .interact etc.). All the apis have their place. But if you don’t know better try using .bind.

Its not that clear from the docs yet though :slight_smile:

1 Like