pythonbokehpandas-bokeh

How to update Span (Bokeh) using ColumnDataSource?


I am trying to update Span using ColumnDataSource, but the information is not being passed onto the source. Span unfortunately does not have a paremeter "source", so is there a better way?

I have defined my sources, figure and line like so:

m1_source = ColumnDataSource(data=dict(x1=[], y1=[]))
m1_spans = ColumnDataSource(data=dict(x1=[]))    
p = figure(x_axis_type="datetime", title="", sizing_mode="fixed", height = 500, width = 1400)
p.line(x ="x1", y="y1", color = 'blue', source=m1_source)

Then I have a for loop that should ideally plot multiple spans, each 'i' will be a separate timestamp:

for i in m1_spans.data['x1']:
    p.add_layout(Span(location=i, dimension='height', line_color='red', line_dash='solid', line_width=1))

This is taken from my update() function:

m1_source.data = dict(
   x1=machine_1_vals['mpTimestamp'],
   y1=machine_1_vals['Value'])

m1_spans.data = dict( x1=ToolsDF.loc[ToolsDF['Value'] == float(vals['Tool_val'])]['Timestamp'])

I have checked this, and m1_spans does successfully return multiple timestamps, so the error shouldn't be here.

The reason I am confused, is because my p.line will successfully update with no issues, but it does have a source parameter, while span does not.

I would be really appreciative for any advice about how to solve this issue. If I should have supplied more information, I apologize and can update as needed, I just tried to keep it brief for you.

Thanks.


Solution

  • Span objects do not currently have an ability to be "powered" by a ColumnDataSource. Each Span only draws one span, specified by its own location property.

    You will need to update the location property individually on each Span object in order to update it. Alternatively, if you absolutely want to be able to drive updates through a CDS, you could look at using a multi_line, segment, or ray glyph instead. Those all have different ways to configure their coordinates, so you'd have to see which is most convenient to your use-case. But they all come with one trade-off, which is that none of them have the full "infinite extent" that a Span supports.