Can not updated vbar_stack with stream data with python bokeh

I try update vbar_stack using ds.stream as below code

i = 1
ds = ColumnDataSource({"x": ['0', '1'], "d1": [0, 2], "d2": [0, 1]})
colors = ['red', 'green']

def update():
#     print('calling update')
    global i
    i += 1
    ds.stream({'x': [str(i)], 'd1': [np.random.randint(1, 3)], 'd2': [np.random.randint(1, 3)]})
    print(ds.data)

def bkapp(doc):
    p = figure(x_range=ds.data['x'], height=250)
    p.vbar_stack(['d1', 'd2'], x='x', width=0.5, color=colors, source=ds)

    doc.add_root(column(p))
    doc.add_periodic_callback(update, 1000)

server = Server({'/': bkapp}, num_procs=1)
server.start()

if __name__ == '__main__':
    print('Opening Bokeh application on http://localhost:5006/')

    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()

Initial data is well done with vbar_stack but update didn’t operation.
This problem also uploaded stackoverflow.
stackoverflow link : Can not updated vbar_stack with stream data with python bokeh

Well your code is not complete (it is missing imports, at least) so I cannot run it to actually test out, but at a minimum, you would need to update the x-range to include the new categorical factor that you are adding in the update function. Categorical ranges are always specified completely explicitly (there is not “auto” ranging for categorical ranges) and only the factors that are configured get displayed.

Thanks for you answer…
I got a result I wanted as below code…
I wrong understood about x-range…

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
from bokeh.server.server import Server
from bokeh.themes import Theme
import numpy as np

i = 1
ds = ColumnDataSource({'x': [0], "d1": [0], "d2": [0]})
colors = ['red', 'green']
# p = figure(x_range=ds.data['x'], height=250)

def update():
#     print('calling update')
    global i
    i += 1
    ds.stream({'x': [i], 'd1': [np.random.randint(1, 3)], 'd2': [np.random.randint(1, 3)]})
    print(ds.data)

def bkapp(doc):
#     p = figure(x_range=ds.data['x'], height=250)
    p = figure(height=250)
    p.vbar_stack(['d1', 'd2'], x='x', width=0.1, color=colors, source=ds)
    

    doc.add_root(column(p))
    doc.add_periodic_callback(update, 100)

server = Server({'/': bkapp}, num_procs=1)
server.start()

if __name__ == '__main__':
    print('Opening Bokeh application on http://localhost:5006/')

    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()
1 Like

@hyeongseo Yes, switching from a categorical range to a numeric range is also an option, since numeric ranges can support auto-ranging.