1

Using Bokeh 2.3.3. I have inherited some Python code using Bokeh from an engineer who has left. I don’t understand Bokeh well.

We have a graph which is rendering the same data twice, once as step, once as scatter, so we get a line with the points marked on it.

When I click on the legend to hide the line only the line is hidden, the points are not.

It seems to be the webgl backend which is causing the problem. This code (with Bokeh 2.3.3) shows the problem:

from bokeh.plotting import figure, show, Figure
from bokeh.models import Legend

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

p: Figure = figure(title="Simple line example", x_axis_label="x", y_axis_label="y", output_backend='webgl')

p.add_layout(Legend(click_policy='hide'), 'right')

p.step(x, y, name='rssi', legend_label='rssi', alpha=0.9, nonselection_alpha=0.5)
line_alpha = 1.0
fill_alpha = 0.5
p.scatter(x, y, name='rssi', legend_label='rssi', marker='x', size=8,
          line_alpha=line_alpha, fill_alpha=fill_alpha,
          nonselection_line_alpha=line_alpha * 0.5, nonselection_fill_alpha=fill_alpha * 0.5)

show(p)

I guess webgl was chosen for performance reasons because we have some graphs with 10,000s of points.

I have tried upgrading to 2.4.3 but then the Bokeh figures don’t appear. Also tried 3.1.0 but then our code doesn’t run because lots of names seem to have changed.

UPDATE:

Screenshot for the bokehjs related problem.

HTML for non-working 3.1.0

mosc9575
  • 5,618
  • 2
  • 9
  • 32
quite68
  • 31
  • 7
  • Running your code with 3.1.0 and 2.4.3 I can't reprocude your problem (anymore). Looks like this was a Bug and is already solved. Check the [Changelog](https://docs.bokeh.org/en/latest/docs/releases.html). From 2.3.3 to 2.4.0 there are some fixes in `webgl`. try to update. – mosc9575 Mar 30 '23 at 09:37
  • With 2.4.0, 2.43 & 3.1.0 I just get a blank page with no figure (on Windows 11). I even tried doing a fresh install of Python 3.11.1 and installing just bokeh 3.1.0 (and dependencies) but still nothing. – quite68 Mar 30 '23 at 10:37
  • Can you check your browser console for some error messages? Sometimes there is a conflict with ols bokehjs code. Make sure to reload your browser. If you have any further information, please update your question. – mosc9575 Mar 30 '23 at 10:56
  • So I'm getting " ReferenceError: Bokeh is not defined" for lines like Bokeh.set_log_level("info"); – quite68 Mar 30 '23 at 11:03
  • Is this line in your Code or in the source code of bekoh? Could be some spelling. I guess the correct spelling should be [`set_log_level(info.bokehLogLevel)`](https://github.com/bokeh/bokeh/pull/3063/files#diff-1341c674164bdcd46f72419fe3439074c6589f8d4a4112cb6aae267e85493455). At the moment I am not sure how to help. – mosc9575 Mar 30 '23 at 12:12
  • OK. sorry I wasn't clear. " ReferenceError: Bokeh is not defined" appears in the browser console. And Bokeh.set_log_level("info"); is the line in the (auto-generated) HTML that the error relates to. That isn't a line that appears in my code. – quite68 Mar 30 '23 at 12:15
  • Can you verify that you use the correct version of bokehjs to your bokeh version. See [the section about embeding](https://docs.bokeh.org/en/latest/docs/user_guide/output/embed.html#components). – mosc9575 Mar 30 '23 at 13:46
  • So they look right: https://cdn.bokeh.org/bokeh/release/bokeh-3.1.0.min.js, https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.1.0.min.js. I'll add picture of HTML to main question. With 2.3.3 there's no bokeh-gl and script statements have integrity and cross origin arguments which aren't there for 3.1.0. Interestingly if I save the HTML from the browser that downloads local copies of those js files and I can then load the HTML and it works. – quite68 Mar 30 '23 at 14:16

2 Answers2

1

Thanks to mosc9575 for being incredibly helpful.

The real answer to the question is that Bokeh 2.3.3 has bugs in the webGL code that were causing my initial problem.

Upgrading to Bokeh 2.4.3 fixes these issues.

If you've been following the rest of the saga: my further issues were due to the original developer embedding the bokeh-2.3.3 *.min.js files in our app.

I still don't know why a fresh install of Python 3.11 and Bokeh 3.1.0 didn't work for my simple test code.

quite68
  • 31
  • 7
  • Congratulation. Let's mark one answer as accepted, to other can see, that we found a working solution. – mosc9575 Mar 30 '23 at 15:29
0

If I understand you question correctlly, you are trying to activate the click and hide option on all renderers.

The bokeh Legend object has an attribure click_policy, which also exists in 2.3.3. You have to set this to "hide" to enable this on all LegendItems.

Setting the same legend_label to multiple renderers will apply the same rule to multiple renderers.

Minimal Example using implicit legend

from bokeh.plotting import figure, show, output_notebook
output_notebook()

p = figure()
p.line(x=[1,2,3,4], y=[1,2,3,4], color='blue', legend_label='line')
p.line(x=[1,2,3,4], y=[2,3,4,5], color='green', legend_label='line') 
p.circle(x=[1,2,3,4], y=[1,2,3,4], color='red', legend_label='circle')
p.legend.click_policy = 'hide'
show(p)

You can read more about legends or for 2.3.3 in the docs.

If you are using an explicit legend, as described in the docs it can look different to the example above.

mosc9575
  • 5,618
  • 2
  • 9
  • 32