Skip to content

Instantly share code, notes, and snippets.

@kuatroka
Last active December 17, 2024 00:23
Show Gist options
  • Save kuatroka/ab0cdbb00a4a52ebd333640716236933 to your computer and use it in GitHub Desktop.
Save kuatroka/ab0cdbb00a4a52ebd333640716236933 to your computer and use it in GitHub Desktop.
holoviz + panel + hvplot - chart, table and brush selection on both
# Fully working hvplot data cleaning tool
import panel as pn
import pandas as pd
import holoviews as hv
pn.extension('tabulator', template='material', sizing_mode='stretch_width')
import hvplot.pandas # noqa
hvplot.extension('bokeh')
pd.options.display.float_format = '{:.0f}'.format
common_df_pd = common_df.to_pandas()
cik_dropdown = pn.widgets.Select(name='CIK',
options=common_df_pd['cik'].unique().tolist(),
value=common_df_pd['cik'].unique().tolist()[0])
common_dfi = common_df_pd.round().interactive(sizing_mode='stretch_width')
filtered_common_dfi = common_dfi[(common_dfi['cik'] == cik_dropdown)]
# # points plot
points_plot = filtered_common_dfi.hvplot(x='rdate',
y='file_value_sum',
kind='points',
# height=350,
width=1300,
persist=True,
hover_cols=['fdate','quarter', 'value_format', 'n_holdings', 'accession_number'],
yformatter='%d'
)
# rewrite the selection expression to use holoviz
ls_common = hv.link_selections.instance(unselected_alpha=0.08)
ls_common.show_regions=True
@pn.depends(cik_dropdown.param.value,watch=True)
def clear_selection_on_drop_down_change(self):
ls_common.selection_expr = None
# # Table is not yet dynamically linked to the linked selection
table = (filtered_common_dfi[['rdate', 'fdate', 'accession_number','ratio_median_avg','file_value_sum', 'value_format','n_holdings', 'quarter', 'file_shares_sum',
]]
.sort_values(by='quarter', ascending=False)
.pipe(ls_common.filter, selection_expr=ls_common.param.selection_expr)
.pipe(pn.widgets.Tabulator, pagination='remote', page_size=8))
column = pn.Column(filtered_common_dfi.widgets(),ls_common(points_plot.holoviews()).opts(hv.opts.Points(active_tools=['box_select'])),
ls_common(table.panel()), width=1500)
column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment