On Apr 14, 2018, at 10:43, beezerlm <[email protected]> wrote:
Here is a working example. If you select the April 2nd and May 1st records, then slide the left bar past April 2nd, you will see the selection jump to the right.
import pandas as pd
import numpy as np
import math
from datetime import date, datetime
from bokeh.plotting import figure
from bokeh.layouts import widgetbox, row, column
from bokeh.models import ColumnDataSource, HoverTool, CustomJS, WheelZoomTool, Label, LabelSet
from bokeh.models.widgets import DateRangeSlider, Div, Select, Tabs, Panel, TableColumn, DataTable, DateFormatter
from bokeh.io import curdoc, show, output_file, save
def update_plot(attr, old, new):
try:
selected = source.selected
except:
print("nothing selected...")
start_date = date_range_slider.value_as_datetime[0]
end_date = date_range_slider.value_as_datetime[1]
mask = (df.ShipDate > start_date) & (df.ShipDate <= end_date)
new_df = df[mask]
new_data = {
'x' : new_df.ShipDate,
'x0' : new_df.ShipDate,
'x1' : new_df.ShipDate,
'y' : new_df.DaysBeforeDue,
'y1' : new_df.DaysBeforeDue
}
try:
print('selected',source.selected)
print('selected.indices',source.selected.indices)
print('selected.line_indices',source.selected.line_indices)
print('selected.multiline_indices',source.selected.multiline_indices)
source.selected = selected
except:
print('source.trigger exception raised.')
source.data = new_data
def update_slider(attr, old, new):
print('updating_slider')
if select_year.value == str(date.today().year):
start_date = str(date.today().year) + "-01-01"
end_date = date.today().strftime('%Y-%m-%d')
else:
start_date = select_year.value + "-01-01"
end_date = select_year.value + "-12-31"
new_start_date = start_date.replace("-", " ")
new_start_date = new_start_date.split(" ")
new_start_date = date(int(new_start_date[0]),int(new_start_date[1]),int(new_start_date[2]))
new_end_date = end_date.replace("-", " ")
new_end_date = new_end_date.split(" ")
new_end_date = date(int(new_end_date[0]),int(new_end_date[1]),int(new_end_date[2]))
date_range_slider.value = (new_start_date,new_end_date)
date_range_slider.start = new_start_date
date_range_slider.end = new_end_date
p.x_range.start = new_start_date
p.x_range.end = new_end_date
update_plot()
print('update_complete')
df = pd.DataFrame()
df['ShipDate'] = [pd.Timestamp('2017-04-02'), pd.Timestamp('2017-05-01'), pd.Timestamp('2017-08-01')]
df['DaysBeforeDue'] = [50, 37, 65]
print(df)
start_date = '2017-04-01'
end_date = '2017-08-01'
source = ColumnDataSource(data={'x' : df.ShipDate,
'x0' : df.ShipDate,
'x1' : df.ShipDate,
'y' : df.DaysBeforeDue,
'y1' : df.DaysBeforeDue
})
x_range = (pd.Timestamp('2017-03-01'),df.ShipDate.max())
y_range = (-75, 75)
p = figure(plot_width=1500,
plot_height=550,
title='On-Time Delivery Report ',
tools=('pan','tap','reset','save','poly_select', 'wheel_zoom'),
sizing_mode='scale_width',
x_range=(x_range),
y_range=(y_range))
p.segment(x0='x0', y0=0, x1='x1', y1='y1', line_color='green', line_width=3, source=source)
p.circle('x', 'y', size=15, selection_color = 'yellow', fill_color='yellow', line_color='green', line_width=5, nonselection_alpha=0.5, name='circle', source=source)
p.xaxis.axis_label = "Due Date"
p.yaxis.axis_label = "Number Of Days From Due Date"
new_start_date = start_date.replace("-", " ")
new_start_date = new_start_date.split(" ")
new_start_date = date(int(new_start_date[0]),int(new_start_date[1]),int(new_start_date[2]))
new_end_date = end_date.replace("-", " ")
new_end_date = new_end_date.split(" ")
new_end_date = date(int(new_end_date[0]),int(new_end_date[1]),int(new_end_date[2]))
date_range_slider = DateRangeSlider(title="Date range",
value=(new_start_date, new_end_date),
start=new_start_date,
end=new_end_date,
step=1,
css_classes=["custom_slider"]
)
date_range_slider.on_change('value', update_plot)
layout = row(p, sizing_mode='scale_width')
widget_box = widgetbox(date_range_slider, sizing_mode='scale_width', height=70)
update_plot('attr','old','new')
curdoc().add_root(widget_box)
curdoc().add_root(layout)
curdoc().title = 'On-Time Delivery Report'
On Saturday, April 14, 2018 at 11:56:23 AM UTC-4, Bryan Van de ven wrote:
Hi,
Well, setting (as opposed to reading only) selection is definitely not as thoroughly exercised a feature. Additionally, there was just a big change to use the Selection model (instead of the old cumbersome dict). So it's entirely possible there are just bugs. This is definitely a case that having an MRE to examine will help.
Thanks,
Bryan
> On Apr 14, 2018, at 10:51, beezerlm <[email protected]> wrote:
>
> Bryan,
>
> I will try to put a working example together. My code has become a bit unorganized I need to clean it up so it's easier to follow.
>
> On Saturday, April 14, 2018 at 11:42:37 AM UTC-4, Bryan Van de ven wrote:
> I don't see anything at a glance. It's often the case that if I can actually run code, I can identify a problem quickly. If you can share a complete minimal reproducer, I'd be happy to take a closer look.
>
> Thanks,
>
> Bryan
>
> > On Apr 14, 2018, at 10:07, beezerlm <[email protected]> wrote:
> >
> > Update: It has nothing to do with tabs, I removed tabs which fixed the "Deserialization error", but the selection behaviour is the same.
> >
> > On Saturday, April 14, 2018 at 8:27:26 AM UTC-4, beezerlm wrote:
> > One more thing to mention, I am getting a "Deserialization error" in the console every time the DateRangeSlider is updated, but I think this is caused by Tabs(). I am pretty sure the report still functioned properly even with the this error showing in the console.
> >
> > On Saturday, April 14, 2018 at 8:19:49 AM UTC-4, beezerlm wrote:
> > Update: I removed the year dropdown and still have this issue, so I am guessing the issue does not lie with the code I posted, but with a change I may have made to accomodate the year dropdown menu. Looking elsewhere at the moment.
> >
> > On Saturday, April 14, 2018 at 8:15:36 AM UTC-4, beezerlm wrote:
> > I didn't notice this problem until I added a "select" drop down menu to filter my report by year (2018 2017 etc...). I'm sure I have something wrong but I am not sure where. My plot circle's column source data is selected by "df.ShipDate". If my selection is, let's say index 100 of the selectable group, then I scrub the slider, it appears it is maintaining the selection as index 100 but the record that was index 100 is now index 95. So as I scrub the selection is jumping all over the report to different records. Here is the function I added that I suspect caused this behaviour:
> >
> > def update_slider(attr, old, new):
> > print('updating_slider')
> >
> > if select_year.value == str(date.today().year):
> > start_date = str(date.today().year) + "-01-01"
> > end_date = date.today().strftime('%Y-%m-%d')
> > else:
> > start_date = select_year.value + "-01-01"
> > end_date = select_year.value + "-12-31"
> >
> > new_start_date = start_date.replace("-", " ")
> > new_start_date = new_start_date.split(" ")
> > new_start_date = date(int(new_start_date[0]),int(new_start_date[1]),int(new_start_date[2]))
> >
> > new_end_date = end_date.replace("-", " ")
> > new_end_date = new_end_date.split(" ")
> > new_end_date = date(int(new_end_date[0]),int(new_end_date[1]),int(new_end_date[2]))
> >
> > date_range_slider.value=(new_start_date,new_end_date)
> > date_range_slider.start = new_start_date
> > date_range_slider.end = new_end_date
> >
> > p.x_range.start = new_start_date
> > p.x_range.end = new_end_date
> >
> > print('update_complete')
> >
> >
> > This function is triggered by:
> >
> > select_year.on_change('value', update_slider)
> >
> >
> > Here is a screenshot for reference:
> >
> >
> >
> > Any Ideas what I might be doing wrong?
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
> > To post to this group, send email to bo...@continuum.io.
> > To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/1c97cef3-68bc-4cbd-905c-a6b667a22cb0%40continuum.io\.
> > For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
> To post to this group, send email to bo...@continuum.io.
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/74dcd14d-964f-4955-ac4e-171f6d39b0a9%40continuum.io\.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout\.
--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/ab03561d-26a1-4954-bca7-56adc27f4790%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.