streaming.py 979 B

123456789101112131415161718192021222324252627
  1. # streaming/main.py
  2. from bokeh import models, plotting, io
  3. import pandas as pd
  4. from time import sleep
  5. from itertools import cycle
  6. data = pd.read_csv(
  7. "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
  8. data["date"] = pd.to_datetime(data["date"])
  9. data["new_cases"] = data.groupby("state")["cases"].diff()
  10. state = "California"
  11. california_covid_data = data[data["state"] == state].copy()
  12. source = models.ColumnDataSource(california_covid_data)
  13. p = plotting.figure(
  14. x_axis_label="Date", y_axis_label="New Cases",
  15. plot_width=800, plot_height=250, x_axis_type="datetime", tools=["hover", "wheel_zoom"]
  16. )
  17. p.line(x="date", y="new_cases",
  18. source=source,
  19. legend_label=state,
  20. width=4,
  21. )
  22. io.curdoc().add_root(p)
  23. index_generator = cycle(range(len(california_covid_data.index)))
  24. def stream():
  25. index = next(index_generator)
  26. source.data = california_covid_data.iloc[:index]
  27. io.curdoc().add_periodic_callback(stream, 10)