from numpy import linspace, sin, cos, pi
import bokeh.plotting as plt
from bokeh.models import Range1d

fig_size = 750
ind = 0


def draw_map():
    tools = "pan,wheel_zoom,reset,previewsave"
    fig = plt.figure(title='', logo=None, tools=tools, webgl=False, x_range=Range1d(0, 15, bounds='auto'),
                     y_range=Range1d(-2, 2, bounds='auto'),
                     toolbar_location='above', plot_width=fig_size, plot_height=fig_size, min_border=10)
    return fig


def read_tracks():
    n = 50
    tracks = [list(linspace(0, 4 * pi, n)), list(sin(linspace(0, 4 * pi, n)))]
    return tracks


tracks = read_tracks()
fig = draw_map()
route = fig.line([0, 0], [0, 0], color='red', line_width=3, alpha=0.5, name='track')

from time import sleep
print('Sleeping...')
sleep(10)
print('Active...')

def update():
    global ind
    print(ind)
    route.data_source.data = dict(x=tracks[0][ind:], y=tracks[1][ind:])
    ind = ind + 1 if ind < len(tracks[0]) - 2 else 0


plt.curdoc().add_periodic_callback(update, 500)
plt.curdoc().add_root(fig)
plt.show(fig)
