# The bokeh-server must be running to see this example

from __future__ import print_function

from bokeh.plotting import figure, output_server, push
from bokeh.plotting import cursession
import time

#prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# output to static HTML file
output_server("animated_line")

# Plot a `line` renderer
# setting the color, line thickness, title, and legend value.
p = figure(title="One Line")
p.line(x, y, legend="Temp.", x_axis_label='x', y_axis_label='y',
       name='example1')

push()

# create some simple animation..
# first get our figure example data source
renderer = p.select(dict(name="example1"))
ds = renderer[0].data_source

# we create 19 new lists of y-values to use them as steps in our animation
values = []
for i in range(1, 20):
    values.append([y_ ** i for y_ in y])


def update():
    while True:
        # now we update the y column in the datasource
        # with each y-value list in the values list
        for val in values:
            ds.data["y"] = val
            cursession().store_objects(ds)
            time.sleep(0.5)

        for val in values[::-1]:
            ds.data["y"] = val
            cursession().store_objects(ds)
            time.sleep(0.5)


#while True:
#    ds.data['y'][0] = ds.data['y'][0] + 1
#    renderer[0].glyph.line_color = 'red'
#    cursession().store_objects(renderer[0], dirty_only=False)
#    cursession().store_objects(ds)
#    time.sleep(0.5)
#
#    ds.data['y'][0] = ds.data['y'][0] + 1
#    renderer[0].glyph.line_color = 'blue'
#    cursession().store_objects(renderer[0], dirty_only=False)
#    cursession().store_objects(ds)
#    time.sleep(0.5)
