[FEATURE] Allow named columns to be used in EditTools
See original GitHub issueIn the examples below I will be using the LineEditTool developed in feature branch #9968
However I believe that the issue/feature I will describe applies generally to all the EditTools and could potentially be implemented on the abstract base class.
My problem is that there appears to be only two methods of successfully getting an edit tool to work.
1) passing in data directly to the p.line method as arguments as shown in the example below
from datetime import datetime, timedelta
from bokeh.models import ColumnDataSource, LineEditTool, Circle
from bokeh.plotting import figure
from bokeh.io import curdoc, show
p = figure(
y_range=(0, 10),
x_axis_type="datetime",
width=400,
height=400,
title="TimeSeries Edit Tool",
)
dates = [datetime(2020, 5, 1) + timedelta(days=x) for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]]
l1 = p.line(
dates,
[1, 2, 3, 4, 5, 4, 3, 2, 1],
color="green",
alpha=1.0,
line_width=5,
)
c1 = p.circle([], [], size=10, color="red")
ledit_tool = LineEditTool(renderers=[l1], intersection_renderer=c1, dimensions="height")
p.add_tools(ledit_tool)
curdoc().add_root(p)
show(p)
2) creating CDS where the columns MUST be named x and y or else the edit tool does not function.
from datetime import datetime, timedelta
from bokeh.models import ColumnDataSource, LineEditTool, Circle
from bokeh.plotting import figure
from bokeh.io import curdoc, show
p = figure(
y_range=(0, 10),
x_axis_type="datetime",
width=400,
height=400,
title="TimeSeries Edit Tool",
)
dates = [datetime(2020, 5, 1) + timedelta(days=x) for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]]
cds = ColumnDataSource({"x": dates, "y":[1, 2, 3, 4, 5, 4, 3, 2, 1]})
l1 = p.line(
x="x",
y="y",
source=cds,
color="green",
alpha=1.0,
line_width=5,
)
c1 = p.circle([], [], size=10, color="red")
ledit_tool = LineEditTool(renderers=[l1], intersection_renderer=c1, dimensions="height")
p.add_tools(ledit_tool)
curdoc().add_root(p)
show(p)
Looking at the doc strings for the edit tools it seems like the necessity of using x and y is known.
The tool will modify the columns on the data source corresponding to the
``x`` and ``y`` values of the glyph. Any additional columns in the data
source will be padded with the declared``empty_value``, when adding a new
point.
I would think that generally when people are making complex interactive dashboards they will be using the CDS option and it is highly likely they are going to want to be using their own names for the columns rather than forced to use x and y so that the edit tools work.
This is certainly true in my case. While I have gotten my code to work using the x and y restriction, it did force changes that made the code less readable and generally worse off.
3) Proposed 3rd option where CDS with custom named columns can be used
I think a 3rd option would be useful where the CDS can be used and the edit tool takes additional keyword arguments allow the names of the columns to be passed.
If this can be implemented on the EditTool base class it may not be a large change but could end up having quite a large effect.
Below is the code below that I would like to see working but currently does not.
from datetime import datetime, timedelta
from bokeh.models import ColumnDataSource, LineEditTool, Circle
from bokeh.plotting import figure
from bokeh.io import curdoc, show
p = figure(
y_range=(0, 10),
x_axis_type="datetime",
width=400,
height=400,
title="TimeSeries Edit Tool",
)
dates = [datetime(2020, 5, 1) + timedelta(days=x) for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]]
cds = ColumnDataSource({"datetime": dates, "prediction":[1, 2, 3, 4, 5, 4, 3, 2, 1]})
l1 = p.line(
x="datetime",
y="prediction",
source=cds,
color="green",
alpha=1.0,
line_width=5,
)
c1 = p.circle([], [], size=10, color="red")
ledit_tool = LineEditTool(x="datetime", y="prediction", renderers=[l1],
intersection_renderer=c1, dimensions="height")
p.add_tools(ledit_tool)
curdoc().add_root(p)
show(p)
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
Ahhh! that’s what that part of the code was for 😃 Not surprising the issue was caused by code I wrote and my lack of bokeh knowledge.
Yep I will fix and make a PR.
Sorry I was quite confused. I wasn’t quite aware that the changes were already in the master branch. For some reason I thought they were still kept on a seperate branch as part of the bokeh repository.
I pulled down the latest master and added just my bug fix.
Thanks for your patience and guidance.