Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
Bokeh source.change.emit() only works on first change

I'm trying to create an interactive stacked bar chart that displays the data for a given slider value. The entire data is stored in a base dataset and the data displayed is a subset of this base dataset. The problem I'm facing is that the code basically works, but only the first time a change is issued.

Here is a minimal example:

from bokeh.layouts import column
from bokeh.models import CustomJS, Slider
from bokeh.plotting import ColumnDataSource, figure, output_file, show

import pandas as pd

base = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],
                     'b': [10, 20, 30, 40, 50, 60],
                     'c': ['x', 'y', 'x', 'y', 'x', 'y']})
edit = pd.DataFrame({'a': [1, 2],
                     'b': [10, 20],
                     'c': ['x', 'y']})

sbase = ColumnDataSource(data=base)
sedit = ColumnDataSource(data=edit)

p = figure(x_range=['x', 'y'], width=200, height=200)
p.vbar_stack(stackers=['a', 'b'], x='c', width=0.9, color=['#7570b3', '#1b9e77'], source=sedit)

slider = Slider(start=1, end=3, value=1, step=1, title='slider')
slider_callback = CustomJS(args=dict(sbase=sbase, sedit=sedit), code="""
    const sid = cb_obj.value;
    const bs = sbase.data;
    const ed = sedit.data;
    
    var ix = sid*2-2;
    var iy = sid*2-1;    
    
    ed['a'] = [bs['a'][ix], bs['b'][ix]];
    ed['b'] = [bs['a'][iy], bs['b'][iy]];
    
    console.log(ed)
    console.log(sedit.data)
    sedit.change.emit();
    console.log(sedit.data)
""")
slider.js_on_change('value', slider_callback)

output_file('test.html')
show(column(p, slider))

I'm kind of at a loss, because, according to the logs, the underlying data does change with subsequent inputs, but the chart still remains frozen.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

source.change.emit is something that probably should be internal, but it's been mentioned a few places where it is currently the only option. However, in general (including this case) the recommended best practice is always to make real assignments to update entire property values (e.g. .data) all at once, rather than changing things "in-place".

slider_callback = CustomJS(args=dict(sbase=sbase, sedit=sedit), code="""
    const sid = cb_obj.value;
    const bs = sbase.data;
    const ix = sid*2-2;
    const iy = sid*2-1;

    sedit.data = {
        a: [bs['a'][ix], bs['b'][ix]],
        b: [bs['a'][iy], bs['b'][iy]],
        c: bs.c
    }
""")
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda