I am trying to use the Date Picker to filter a table in a browser. However the contents of the table are not updating after selecting a new date. I tried to follow the examples listed here. The code which generates the output is below. Please advise.
Below the table not responding to the selection in the date picker:
Code which generates the above output is below:
import datetime
import pandas as pd
import panel as pn
from io import BytesIO
from bokeh.util.browser import view
from bokeh.resources import INLINE
import datetime as dt
#define the temporary html file for viewing
html_file = 'temp.html'
#utility function for writing html to temporary file and viewing
def write_to_temp(html):
with open(html_file, mode='w',encoding='utf-8') as f:
f.write(html)
view(html_file)
# utility function for getting html from panel and saving to file
def get_pn_html(panel_object):
tmpfile = BytesIO()
panel_object.save(filename=tmpfile, resources=INLINE)
tmpfile.seek(0)
html = tmpfile.read().decode('utf-8')
write_to_temp(html)
def add_day():
return datetime.timedelta(days=(1))
# creation of dummy data
def create_uptime_data():
up_time = []
for i in range(81):
up_time.append('UP')
for i in range(19):
up_time.append('DOWN')
df_data = {"UP TIME": up_time}
return pd.DataFrame.from_dict(df_data)
def create_date_data():
start = dt.date(2021, 1, 26)
dates = [start + dt.timedelta(days=i) for i in range(len(df))]
return dates
df = create_uptime_data()
ate_picker = pn.widgets.DatePicker(name='Date Picker',
start=create_date_data()[0],
end=create_date_data()[-1],
value=create_date_data()[0])
@pn.depends(date_picker.param.value)
def create_table(date=date_picker.param.value):
df = create_uptime_data()
df['date'] = create_date_data()
df = df[df['date']==date]
tab = pn.widgets.Tabulator(df, height=500, layout='fit_columns',
sizing_mode='stretch_width')
return tab
daily = pn.Row(pn.Column(date_picker),create_table)
get_pn_html(daily)
relevant versions below:
bokeh==2.3.3
panel==0.12.0
param==1.12.0