A common set up is to have a scope with filters and other things in it that interact with various components.
Is there a better way to do this than to create a hidden placeholder with a watch? This example changes the filter on click and then the logic for how to update things is in the watch callback.
import panel as pn
import pandas as pd
import numpy as np
from string import ascii_uppercase
np.random.seed(0)
height = 300
pn.extension('tabulator')
filters = dict()
@pn.cache
def apply_filters(df, filters):
for k, v in filters.items():
df = df[df[k] == v]
return df
df = pd.DataFrame(np.random.randn(100, 3), columns=['a', 'b', 'c'])
df['d'] = [ascii_uppercase[i] for i in np.random.randint(0, len(ascii_uppercase), df.shape[0])]
agg = df.groupby('d').size().to_frame('count')
df_filtered = apply_filters(df, filters)
agg_tabulator = pn.widgets.Tabulator(
agg,
width=400,
height=height,
hidden_columns=['index'],
columns=[dict(field=k, editable=False) for k in agg.columns.tolist()],
disabled=True,
selectable=False,
)
df_tabulator = pn.widgets.Tabulator(
df_filtered,
width=400,
height=height,
hidden_columns=['index'],
columns=[dict(field=k, editable=False) for k in df_filtered.columns.tolist()],
disabled=True,
selectable=False,
)
text = pn.pane.Markdown(f'## what {filters}')
def click(event):
if filters.get(event.column, None) == event.value:
filters.pop(event.column)
else:
filters[event.column] = event.value
print('click event', filters)
hidden_target.object = filters
# text.object = f'filters: {filters}'
# df_tabulator.value = apply_filters(df, filters)
print('aaa', hidden_target.object)
agg_tabulator.on_click(click, column='d')
hidden_target = pn.pane.PaneBase('init')
def watch_callback(*events):
# RERENDER EVERYTHING AS NEEDED I GUESS, ANY OTHER WAY?
# WE ARE STORING THE DAG LOGIC IN HERE AND IN THE CODE?
print(f'watch callback {events}')
for event in events:
print(event.name, event.new, event.old)
text.object = f'what? {filters}'
df_tabulator.value = apply_filters(df, filters)
watcher = hidden_target.param.watch(watch_callback, ['object'], onlychanged=False)
app = pn.Column(
text,
agg_tabulator,
df_tabulator,
)
app.servable()