I am currently trying to create a program that reads from a csv file and updates a Bokeh plot in real-time. I’ve already finished coding out everything, however, as I want the user to launch the program as an executable rather than using the command line and ‘bokeh serve’, I would like to launch the bokeh server from code. Here is what I have as a sample program so far:
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
import pandas as pd
import os
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
def test_doc(doc):
def getData():
file = os.path.basename('Test.csv')
return pd.read_csv(file)
# init plot
source = ColumnDataSource(data=getData())
print(source.data)
plot = figure(
title=None, plot_width=600, plot_height=600,
min_border=0, toolbar_location= 'below')
plot.circle(x = 'x',y = 'y', color = 'blue',
source=source, size=8, nonselection_alpha = 1)
curdoc().add_root(plot)
# update plot
def update():
bokeh_source = getData()
source.stream(bokeh_source, rollover=50)
curdoc().add_periodic_callback(update, 100)
apps = {'/': Application(FunctionHandler(test_doc))}
server = Server(apps, port=8888)
server.run_until_shutdown()
When I tested the program using bokeh serve and without the last 3 lines of code, the html plot managed to update whenever I updated my csv file. However, once I added the last 3 lines, I kept getting different errors. The first I got was:
[WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
which was strange as I never ran anything else on the port. On subsequent reruns of the code on different ports, I now instead got:
This event loop is already running
Could someone explain to me what I am doing wrong?