Search code examples
pythonbokeh

bokeh graph broken since version 2.0.0


Since bokeh version 2.0.0, I am no longer getting an output from the code below. The code was fine yesterday, but it is not working now. The expected output is geograph which shows only data corresponding filtered date. I am using jupyter notebook and tried on ipython. I have tried running the code step by step and everything running fine until "doc.add_root(layout)".

python version: 3.6.8, bokeh version: 2.0.0

Edit: Actually I don't have any output.Jupyter notebook only prints BokehJS 2.0.0 loaded as picture at below. Jupyter Output

Edit2 I removed virtual enviroment and reinstalled it. New bokeh version is 2.0.1 and I am getting this output now.

enter image description here

Data as dictionary (sample):

data = {'date': {66: '2015-08-13',
  317: '2015-07-16',
  61: '2015-07-09',
  71: '2015-09-17',
  294: '2016-01-29'},
 'location': {66: 'fason',
  317: 'yenidogan',
  61: 'fason',
  71: 'fason',
  294: 'sultanbeyli'},
 'qty': {66: 68016.0, 317: 1309952.0, 61: 55134.0, 71: 55699.0, 294: 641157.0},
 'wh': {66: 2, 317: 3, 61: 2, 71: 2, 294: 2},
 'x_axis': {66: 3253339.8929858636,
  317: 3255353.4952555867,
  61: 3253339.8929858636,
  71: 3253339.8929858636,
  294: 3255618.7696021474},
 'y_axis': {66: 5012258.806203845,
  317: 5015160.9407821335,
  61: 5012258.806203845,
  71: 5012258.806203845,
  294: 5008555.491935941},
 'density': {66: 34008.0,
  317: 436650.66666666674,
  61: 27567.0,
  71: 27849.5,
  294: 320578.5},
 'color': {66: 'green', 317: 'red', 61: 'green', 71: 'green', 294: 'orange'},
 'c_size': {66: 750, 317: 750, 61: 750, 71: 750, 294: 750}}

Data link (full data): link

The Code:

import pandas as pd
pd.set_option('display.float_format', lambda x: '%d' % x)
import numpy as np

import datetime
from datetime import datetime

import math
from math import pi

#import warnings
#warnings.filterwarnings("ignore")

import bokeh
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.core.properties import value
from bokeh.io import  output_file, output_notebook, push_notebook
from bokeh.layouts import column,row
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource
from bokeh.models import NumeralTickFormatter
from bokeh.models import DatetimeTickFormatter
from bokeh.models import Column
from bokeh.models import Legend
from bokeh.models import Select
from bokeh.palettes import Colorblind
from bokeh.plotting import figure, show
from bokeh.tile_providers import CARTODBPOSITRON, get_provider

# df = pd.read_csv('data.csv')
df = pd.DataFrame(data, columns = ['date', 'location', 'qty', 'wh', 'x_axis', 'y_axis', 'density', 'color', 'c_size'])

def modify_doc(doc):
    def make_dataset(date):
        data = df[df['date'] == date]
        data = data[data['qty'] != 0]        

        return ColumnDataSource(data)

    def update():

        date_to_plot = date_selection.value

        new_src = make_dataset(date_to_plot)

        src.data.update(new_src.data)       


    def graph(src):          

        tile_provider = get_provider(CARTODBPOSITRON)

        # range bounds supplied in web mercator coordinates
        z = figure(x_range=(3254079.2550728847,3274493.100585638), y_range=(4983881.858145405,5018160.940782133),
                   x_axis_type="mercator", y_axis_type="mercator")
        z.title.text = 'Warehouse Change'

        z.add_tile(tile_provider)
        z.circle(x='x_axis', y ='y_axis',source=src, radius='c_size',color='color', alpha=0.5)

        tooltips1 = [
                ('Location','@location'),
                ('Number of Warehouses','@wh{,}'),
                ('Number of Items','@qty{,}') 
               ]

        z.add_tools(HoverTool(tooltips=tooltips1)) 


        return z

    date_list = ['2015-04-30','2015-05-07','2015-05-14','2015-05-21','2015-05-28','2015-06-04',
                 '2015-06-11','2015-06-18','2015-06-25','2015-07-02','2015-07-09','2015-07-16',
                 '2015-07-23','2015-07-30','2015-08-06','2015-08-13','2015-08-20','2015-08-27',
                 '2015-09-03','2015-09-10','2015-09-17','2015-09-24','2015-10-01','2015-10-08',
                 '2015-10-15','2015-10-22','2015-10-29','2015-11-05','2015-11-12','2015-11-19',
                 '2015-11-26','2015-12-03','2015-12-10','2015-12-17','2015-12-24','2015-12-31',
                 '2016-01-07','2016-01-15','2016-01-22','2016-01-29','2016-02-05','2016-02-12',
                 '2016-02-19','2016-02-26','2016-03-04','2016-03-11','2016-03-18','2016-03-25',
                 '2016-04-01','2016-04-08','2016-04-15']


    date_selection = Select(title="Date:", value='2015-04-30', options=date_list)

    date_selection.on_change('value', lambda attr, old, new: update()) 

    controls = Column(date_selection)

    initial_list = date_selection.value  

    src = make_dataset(initial_list)

    fig = graph(src)

    layout = Column(controls, fig)

    doc.add_root(layout)  


output_notebook()
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app)


Solution

  • Your notebook is not running on the default port 8888, so you will explicitly need to set the notebook_url when you call show:

    # The error message stated the URL to use here
    show(app, notebook_url="http://localhost:8891")  
    

    You will need to set this any and every time the notebook URL is anything other than the default.

    It would be great if Bokeh could detect and adjust for this automatically, however the Jupyter team has steadfastly (for years) refused to provide a supported public API for Python code to know the current notebook address, so that is unfortunately not possible.

    FYI this has been the behavior for a long time, so it is purely coincidental that you happen to be running on non-default ports right now, and also newly Bokeh 2.x at the same time.