user7346517
user7346517

Reputation: 25

Bokeh serve not showing results

Very new to Bokeh and trying to get the graph below to show in a browser but getting the following error: RuntimeError: Cannot run the event loop while another loop is running. How do I get this to show as it would like a JS approach. I am using Jupyter Lab.

from math import pi
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from bokeh.plotting import figure, show, output_file
import pymysql
from bokeh.io import output_notebook, show, output_file
from bokeh.models.widgets import Dropdown
from bokeh.models.annotations import Span, Arrow 
from bokeh.models.arrow_heads import OpenHead, NormalHead, VeeHead
from bokeh.models import LinearAxis, Line, Range1d, DatetimeAxis, CustomJS, Slider,HoverTool, CrosshairTool, ColumnDataSource, Dropdown, LabelSet, LinearColorMapper, ColorBar, Select 
from bokeh.transform import transform
from bokeh.plotting import figure
from bokeh.layouts import column, row, gridplot
from bokeh.plotting import figure, curdoc
from bokeh.client import push_session, pull_session
import warnings



#Connect to pricing database 
warnings.filterwarnings("ignore")
sqlEngine       = create_engine('mysql+pymysql://xxxxx@localhost/oilmarket', pool_recycle=3600)
dbConnection    = sqlEngine.connect()
#Create dataframe of all data 
dframe           = pd.read_sql("SELECT  marketdata.date, oilsort.instrument, marketdata.close FROM marketdata JOIN oilsort ON oilsort.id=marketdata.sortid;", dbConnection);
dbConnection.close()
dframe.head(5)

#Create subset data frame for just one instrument 
options = []
options.append('All')

options.extend(dframe['instrument'].unique().tolist())
source = ColumnDataSource(dframe)

p = figure()
r = p.circle(x='date', y='close', source = source)

select = Select(title="instrument",  options=options, value="All")


def update_plot(attr, old, new):
    if select.value=="All":
        df_filter = dframe.copy()
    else:
        df_filter = dframe[dframe['instrument']==select.value]
    source1 = ColumnDataSource(df_filter)
    r.data_source.data = source1.data
    select.on_change('value', update_plot)
    layout = column(row(select, width=400), p)
#show(layout)
    curdoc().add_root(layout)

session = push_session(curdoc())
session.show()
session.loop_until_closed()

Upvotes: 0

Views: 200

Answers (1)

JOP
JOP

Reputation: 11

I wonder which Bokeh version you are using? I came across with identical "Cannot run the event loop..." error message while trying to import Bokeh 2.2.3 figures to Django 3.1 web page. I finally rolled back to Bokeh 1.4.0 and Django 3.0.3 and the error disappeared. My intuition says that the root cause was in Bokeh 2.2.3.

Upvotes: 1

Related Questions