[BUG] Better warn about passing source in the CustomJSFilter args dict for 2.0 and above
See original GitHub issueThe requirement to no longer specify source
in the args dict tripped me (a layman) up when using a script that worked in 1.40 in 2.0.2. I couldn’t work out what was wrong so I asked in a StackOverflow thread where @bryevdv explained what was going on, updated his previous example, and asked me to create this issue.
Here’s the previous code which didn’t work, and the only change was source=source
added:
from bokeh.plotting import figure, show
from bokeh.models import Slider, CustomJSFilter, CDSView, ColumnDataSource, CustomJS
from bokeh.layouts import column, layout
data = dict(Flights=[97, 34, 23, 6, 26, 97, 21, 92, 73, 10, 92, 14, 77, 4, 25, 48, 26, 39, 93],
Not_Cancelled=[87, 63, 56, 38, 57, 63, 73, 56, 30, 23, 66, 47, 76, 15, 80, 78, 69, 87, 28],
OnTime_Arrivals=[21, 65, 86, 39, 32, 62, 46, 51, 17, 79, 64, 43, 54, 50, 47, 63, 54, 84, 79])
source = ColumnDataSource(data=data)
MinFlights = Slider(start=0, value=50, end=100, step=1)
# this filter selects rows of data source that satisfy the constraint
custom_filter = CustomJSFilter(args=dict(slider=MinFlights), code="""
const indices = []
for (var i = 0; i < source.get_length(); i++) {
if (source.data['Flights'][i] > slider.value) {
indices.push(true)
} else {
indices.push(false)
}
}
return indices
""")
view = CDSView(source=source, filters=[custom_filter])
# force a re-render when the slider changes
MinFlights.js_on_change('value', CustomJS(args=dict(source=source), code="""
source.change.emit()
"""))
p = figure()
p.circle('OnTime_Arrivals', 'Not_Cancelled', source=source, view=view, size=20)
inputs = column(MinFlights, width=200)
show(layout([[inputs,p]]))
I’m unsure what solution to this would’ve been best but at least for me a simple warning that source=source
is no longer required in CustomJS as of 2.0 would’ve been helpful.
First time creating an issue here so please let me know if I’m doing anything wrong or could better explain something. Unsure if this is a bug but didn’t know what else to set it as.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
@cyborg1995 The
args
property forCustomJSFilter
is currently defined as a plain (bokeh)Dict
property:https://github.com/bokeh/bokeh/blob/f91a3a668ea3ee39e02eee2fc46bba759b8395ab/bokeh/models/filters.py#L105-L109
But users should not really ever pass a
source
key in this dict, becauseCustomJSFilter
already automatically provides aasource
value on its own. So I proposed the idea of creating a new property type that can disallow specified keys, e.g something likeThe definition of
Dict
is here:https://github.com/bokeh/bokeh/blob/531452908b70f7eeb128c955aebe2e4247e5ee08/bokeh/core/property/container.py#L156
I think it would suffice to simply subclass
Dict
and supply a newvalidate
method that checks the user-supplied keys against theself.disallow
list of keys and raises an error if a disallowed key is present (it would need to call the supervalidate
method too, of course). New unit tests would also need to be added here:https://github.com/bokeh/bokeh/blob/531452908b70f7eeb128c955aebe2e4247e5ee08/tests/unit/bokeh/core/property/test_container.py
Let’s focus on the property-based solution described above, instead of a document validation approach.
We’d love to have your contribution, it’s probably worth just exploring Bokeh as a user first a little, there are some tutorials linked from the main GitHub and docs pages.
I have submitted a PR https://github.com/bokeh/bokeh/pull/10814.