# streaming/main.py
from bokeh import models, plotting, io
import pandas as pd
from time import sleep
from itertools import cycle
data = pd.read_csv(
    "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
data["date"] = pd.to_datetime(data["date"])
data["new_cases"] = data.groupby("state")["cases"].diff()
state = "California"
california_covid_data = data[data["state"] == state].copy()
source = models.ColumnDataSource(california_covid_data)
p = plotting.figure(
    x_axis_label="Date", y_axis_label="New Cases",
    plot_width=800, plot_height=250, x_axis_type="datetime", tools=["hover", "wheel_zoom"]
)
p.line(x="date", y="new_cases",
       source=source,
       legend_label=state,
       width=4,
       )
io.curdoc().add_root(p)
index_generator = cycle(range(len(california_covid_data.index)))
def stream():
    index = next(index_generator)
    source.data = california_covid_data.iloc[:index]
io.curdoc().add_periodic_callback(stream, 10)