WIP: return filtered dataframe from pn.widgets.Select

Started typing this and marking as DRAFT/WIP. Will come back to it.

I would like to return a filtered dataframe using pn.widgets.Select.

In streamlit I can do something like

value = st.selectbox("values:", values)
filtered_df = df[df["COL"] == value]

I can continue to do stuff with filtered_df e.g.

st.markdown(len(filtered_df))
fig, ax = plt.subplots()
df["COL"].plot.hist(ax=ax)
st.pyplot(fig)

I would like to do the same with panel. I currently have a function to do this using @pn.depends

value = pn.widgets.Select(name="values:", options=values).servable()
@pn.depends(value=value)
def filter_df(value):
    return df[df["COL"] == value]

but iā€™m not sure how to continue working with it

1 Like

Using @pn.depends you can do it like.

import pandas as pd
import panel as pn
from matplotlib.figure import Figure

DATA = pd.util.testing.makeMixedDataFrame()
COLUMN = "C"

pn.extension(sizing_mode="stretch_width", template="fast")

select = pn.widgets.Select(name="Values:", options=list(DATA[COLUMN].unique())).servable(
    area="sidebar"
)


@pn.depends(value=select)
def component(value):
    data = DATA[DATA[COLUMN] == value]

    fig = Figure(figsize=(4, 3))
    ax = fig.subplots()
    data["A"].plot.hist(ax=ax)

    return pn.Column(len(data), data, fig)


pn.panel(component).servable()
1 Like