How to change elements online

How to change elements online? For an example, i want to change color of a square every 1 second. How i can do it?

You can save the code below as main.py and run it as follows:
bokeh serve --show main.py

``

The content of main.py is:

from bokeh.models.widgets import Div, TextInput
from bokeh.models import ColumnDataSource
from bokeh.layouts import column
from bokeh.plotting import figure, curdoc
from bokeh.palettes import Dark2_5 as colors
import itertools

source = ColumnDataSource({‘x’: [2], ‘y’: [6], ‘color’: [‘olive’]})
color_index = 0

p = figure()
square = p.square(x = ‘x’, y = ‘y’, color = ‘color’, source = source, size = 80, alpha = 0.5)

def update():
global color_index
color_index = color_index + 1 if color_index + 1 < len(colors) else 0
square.data_source.data.update(x = [6], y = [2], color = [colors[color_index]])

curdoc().add_root§
curdoc().add_periodic_callback(update, 1000)

``

···

On Thursday, February 7, 2019 at 4:26:03 PM UTC+1, Стас Бокун wrote:

How to change elements online? For an example, i want to change color of a square every 1 second. How i can do it?