Glyph Labels Not Updating As Expected in Bokeh Server

Hello,
My glyph labels are not updating as I expected. Could someone tell me what I’m missing?
Info:

  • Bokeh 0.12.13
  • Pandas 0.22.0
  • Spyder 3.2.6
  • Anaconda 1.6.11
  • Firefox Quantum 58.0.1 (64-bit)
    Screenshots:

The chart starts out looking fine:

But when I change the value, the label stays in the same position (I expected it to be at the end of the bar), and the text of the label does not change (I expected it to be 2).

I also get a browser error when changing the value to two. I cannot tell why I’m getting this error, as I have printed values and everything looks good to me:

Error: attempted to retrieve property array for nonexistent field ‘one’

Code:
import pandas as pd

from bokeh.layouts import row, widgetbox
from bokeh.models.widgets import Select
from bokeh.plotting import curdoc, figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.models.ranges import FactorRange

df = pd.DataFrame(data={
‘category’: [‘a’],
‘one’: [1],
‘two’: [2],
})

source = ColumnDataSource(df)

p = figure(plot_width=800, plot_height=175, title=‘test’,
y_range=FactorRange(factors=list(source.data[‘category’])),
)

h1 = p.hbar(right=‘one’, y=‘category’, color=‘darkgrey’,
height=0.8, source=source)

labels = LabelSet(
text=‘one’, x=‘one’, y=‘category’,
text_font_size=‘9pt’, level=‘glyph’,
x_offset=3, y_offset=-6.5, source=source)

p.add_layout(labels)

def update_source():
df2 = df.reindex(columns=[‘category’, nums.value, ])
source.data = source.from_df(df2)

def update_glyph():
h1.glyph.right = nums.value
labels.text = nums.value
labels.x = nums.value

def update(attr, old, new):
update_source()
update_glyph()

nums = Select(title=‘Value’, value=‘one’, options=[‘one’, ‘two’])

nums.on_change(‘value’, update)

w = widgetbox([nums])

r = row([w, p], sizing_mode=“scale_width”)

curdoc().add_root(r)
curdoc().title = “test”

``

Many thanks!

Hi,

The problem with your code is that apart from updating fields to point to new CDS columns, you also for some reason shuffle around columns of the CDS. Bokeh applies all changes that you make one by one, so if “h1.glyph.right” points to the column “one” and you remove this column from the CDS, the field becomes invalid, as you can see by errors in the JS console when you run your code.

You don’t need to shuffle CDS columns around - just change the relevant fields. E.g. remove “update_source” and “update_glyph” functions, and replace “update” callback with:
def update(attr, old, new):
h1.glyph.right = new
labels.text = new
labels.x = new

``

Regards,

Eugene

···

On Tuesday, February 13, 2018 at 1:17:44 AM UTC+7, [email protected] wrote:

Hello,
My glyph labels are not updating as I expected. Could someone tell me what I’m missing?
Info:

  • Bokeh 0.12.13
  • Pandas 0.22.0
  • Spyder 3.2.6
  • Anaconda 1.6.11
  • Firefox Quantum 58.0.1 (64-bit)
    Screenshots:

The chart starts out looking fine:

But when I change the value, the label stays in the same position (I expected it to be at the end of the bar), and the text of the label does not change (I expected it to be 2).


I also get a browser error when changing the value to two. I cannot tell why I’m getting this error, as I have printed values and everything looks good to me:

Error: attempted to retrieve property array for nonexistent field ‘one’

Code:
import pandas as pd

from bokeh.layouts import row, widgetbox
from bokeh.models.widgets import Select
from bokeh.plotting import curdoc, figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.models.ranges import FactorRange

df = pd.DataFrame(data={
‘category’: [‘a’],
‘one’: [1],
‘two’: [2],
})

source = ColumnDataSource(df)

p = figure(plot_width=800, plot_height=175, title=‘test’,
y_range=FactorRange(factors=list(source.data[‘category’])),
)

h1 = p.hbar(right=‘one’, y=‘category’, color=‘darkgrey’,
height=0.8, source=source)

labels = LabelSet(
text=‘one’, x=‘one’, y=‘category’,
text_font_size=‘9pt’, level=‘glyph’,
x_offset=3, y_offset=-6.5, source=source)

p.add_layout(labels)

def update_source():
df2 = df.reindex(columns=[‘category’, nums.value, ])
source.data = source.from_df(df2)

def update_glyph():
h1.glyph.right = nums.value
labels.text = nums.value
labels.x = nums.value

def update(attr, old, new):
update_source()
update_glyph()

nums = Select(title=‘Value’, value=‘one’, options=[‘one’, ‘two’])

nums.on_change(‘value’, update)

w = widgetbox([nums])

r = row([w, p], sizing_mode=“scale_width”)

curdoc().add_root(r)
curdoc().title = “test”

``

Many thanks!