Date Slider Not Updating Plot

I am attempting to create a date slider which will filter the data for map plot I created. The slider appears but will not filter the data when I use it. I am not sure how to address this problem. My code is below

from pyproj import Proj, transform
import pandas as pd
import numpy as np
from bokeh.plotting import figure
from bokeh.tile_providers import get_provider, WIKIMEDIA, CARTODBPOSITRON, STAMEN_TERRAIN, STAMEN_TONER, ESRI_IMAGERY, OSM
from bokeh.io import output_notebook, show
import requests
import io
import zipfile
import pandas as pd
from io import StringIO
from bokeh.layouts import row, column
from bokeh.models import ColumnDataSource, BooleanFilter, CDSView, DateRangeSlider, CustomJS
from bokeh.models.widgets import DateSlider
from bokeh.resources import INLINE
import bokeh.io
import datetime
from bokeh.plotting import figure, output_file, save



def download_data(url):
    response = requests.get(url)
    with zipfile.ZipFile(io.BytesIO(response.content)) as thezip:
        for zipinfo in thezip.infolist():
            if 'csv' in str(zipinfo):
                with thezip.open(zipinfo) as thefile:
                    csv_contents = thefile.read().decode('UTF-8')
                    df = pd.read_csv(StringIO(csv_contents))
                    return df
            

def create_df_projections(df):
    inProj = Proj(init='epsg:3857')
    outProj = Proj(init='epsg:4326')

    lons, lats = [], []

    for i in range(0, len(df)):
        lat = df['lat'][i]
        lon = df['lng'][i]
        x, y = transform(outProj,inProj,lon,lat)
        lons.append(x)
        lats.append(y)

    df["MercatorX"] = lons
    df["MercatorY"] = lats
    return df


def update_plot(attr, old, new):
    datesel = datetime.fromtimestamp(new / 1000).strftime('%Y-%m-%d')
    new_data = df[df['date'] == datesel]
    source.data.update(ColumnDataSource(new_data).data)
    
def create_map(df):
    inProj = Proj(init='epsg:3857')
    outProj = Proj(init='epsg:4326')

    media = get_provider(OSM)
    shape_0 = df.shape[0]

    lon1, lat1 = transform(outProj,inProj,-180,-85)
    lon2, lat2 = transform(outProj,inProj,180,85)

    p = figure(plot_width=600, plot_height=600,
               x_range=(lon1, lon2), y_range=(lat1, lat2),
               x_axis_type="mercator", y_axis_type="mercator",
               title="POINTS On Map")
    
    date_filter = BooleanFilter(booleans=[True] * shape_0)


    p.add_tile(media)
    
    date_slider = DateSlider(start = min(df.Date), end = max(df['Date']), value = min(df['Date']), step = 1, title = "Date")
    date_slider.on_change('value', update_plot)

    p.circle(x="MercatorX", y="MercatorY",
             size=2,
             fill_color="dodgerblue", line_color="dodgerblue",
             fill_alpha=0.3,
             source=df)
    return p, date_slider

url = 'https://simplemaps.com/static/data/world-cities/basic/simplemaps_worldcities_basicv1.74.zip'

df_len = 250

df = download_data(url)
df = df.head(df_len)


test_date = datetime.datetime(2020, 1, 4)
res = [test_date + datetime.timedelta(days=idx) for idx in range(df_len)]

df['Date'] = res


df_map = create_df_projections(df)

p, slider = create_map(df_map)
output_file('dash.html')


show(column(p,slider))

My version of Bokeh is 1.4.0. Thank you in advance.

I’m in the middle of prepping a release so I can’t do more than mention the main issue, which is that real Python callbacks require running the code as a Bokeh Server application. (The Bokeh server is the Python process that would actually run your callback code). Using output_file and show as you have above generates a static, standalone HTML output. Only JavaScript callbacks (CustomJS) are possible in that case, because JavaScript is all your browser knows about. There is an entire chapter of examples of JS callbacks in the docs:

Also FYI, Bokeh 1.4 is quite old at this point. I would advise that you update to a recent version if at all possible (the link above is to latest docs, if you can’t upgrade you should select the old version in the version chooser, since you won’t be able to use any newer features in the latests docs).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.