This selected in sidebar will render my plot on main. But I have a class that has a button to trigger an insert into the database.
sb = pn.Column(pn.bind(my_func,submitButton))
I need my dataframe to repull from the database and update tabedit = pn.widgets.Tabulator(
value=df_ora, show_index=False, selectable=True, disabled=True, theme=“site”, height=140
)
If you are trying to show a pandas dataframe has a grid to allow users to select record and pass a value to a pn widget no problem. Now if you have a class with serveral functions with a submit button that will triger a database row insert, and you now need the dataframe outside the class to refresh. How do you get the dataframe to to update once a button has been clicked inside a class?
The Tabulator widget has a .value attribute that you can reassign with a new DataFrame. You could use that in your submit method.
An example:
import numpy as np
import pandas as pd
import panel as pn
pn.extension('tabulator')
# Create a DataFrame and a Tabulator widget
df = pd.DataFrame(np.random.rand(5, 5))
w = pn.widgets.Tabulator(df)
# Reassign the widget value with a new DataFrame.
w.value = pd.DataFrame(np.random.rand(5, 5))
Alternatively, instead of updating the whole table, you can decide to just patch it.
Regarding your code I’d advise you to try not to mix too many APIs of Panel (@param.depends, pn.bind, etc.). Not that the code won’t run, it’s just that it’s easier to reason about the code when you use just one API. In your case since you’re already creating a Parameterized class you could stick to that approach, which means you don’t need to use pn.bind but can just use @param.depends.
This is a small example which shows how to get a Parameterized class to render one of its Parameters as a Button. For that you just declare a param.Event Parameter, give it a label that will be the Button title, and add a callback that will be called every time the Button is clicked.
import param
import panel
class P(param.Parameterized):
submit = param.Event(label='Submit data')
@param.depends('submit', watch=True)
def _submit_cb(self):
print('Callback triggered when the submit button is clicked')
p = P()
pn.Param(p.param)
As for your code, I don’t know if this is an indentation issue or not but it is a little awkward to declare sb in the class body, and for my_func not having self as an argument.
Thanks. I did notice some wierd behavior with bind on a button. The button would execute on load up, which is not the behavior i want.
When you ask panel to render Comp.sb in the view method, it will compute pn.Column(pn.bind(my_func,submitButton)) which will indeed trigger the bound function my_func. This is expected, any function/method foo that you ask Panel to render with pn.panel(foo) (or pn.Column(foo), etc.) will be called on render, since Panel needs something to get some objects