List comprehension doesn't work outside function in Bokeh Server App

List comprehension (mylist = [file for file in os.listdir(path) if file.endswith(’.parquet’)]) doesn’t work outside of a function in a server app. Is it an intended behaviour?

If you just add a list comprehension to the slider example it doesn’t load anything, but if you put the same list comprehension inside a function and retrieve it with a return statement, everything works just fine. Tested with Firefox 59.0b5 (64 bit) and Microsoft Edge 41.16299.15.0.

Hi Thomas,

Can you provide a complete example where the comprehension doesn’t work?

Regards,

Eugene

···

On Tuesday, January 30, 2018 at 8:25:37 PM UTC+7, Thomas Cercato wrote:

List comprehension (mylist = [file for file in os.listdir(path) if file.endswith(’.parquet’)]) doesn’t work outside of a function in a server app. Is it an intended behaviour?

If you just add a list comprehension to the slider example it doesn’t load anything, but if you put the same list comprehension inside a function and retrieve it with a return statement, everything works just fine. Tested with Firefox 59.0b5 (64 bit) and Microsoft Edge 41.16299.15.0.

Are you running with "bokeh serve"? If so, you should know that that does not immediately run the code. The code is only run when a session is opened in a browser. The code is for creating new Bokeh document for every session, so it only runs when sessions are created.

If you want code that runs at "server start up", before any browser sessions, you should look into the lifecycle hooks:

  https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks

Thanks,

Bryan

···

On Jan 30, 2018, at 07:37, Eugene Pakhomov <[email protected]> wrote:

Hi Thomas,

Can you provide a complete example where the comprehension doesn't work?

Regards,
Eugene

On Tuesday, January 30, 2018 at 8:25:37 PM UTC+7, Thomas Cercato wrote:
List comprehension (mylist = [file for file in os.listdir(path) if file.endswith('.parquet')]) doesn't work outside of a function in a server app. Is it an intended behaviour?

If you just add a list comprehension to the slider example it doesn't load anything, but if you put the same list comprehension inside a function and retrieve it with a return statement, everything works just fine. Tested with Firefox 59.0b5 (64 bit) and Microsoft Edge 41.16299.15.0.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/889eb505-7487-4379-9116-6c7a63e65052%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

This is the slider example in Bokeh website gallery, modifed with a folder data path and a list comprehesion. I don’t see anything when I run a session with bokeh serve --show ‘myfile.py’.

import os
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

data_path = ‘chose\your\data\path’
file_list = [file.replace(’.parquet’, ‘’) for file in os.listdir(data_path) if file.endswith(’.parquet’)]

Set up data

N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

Set up plot

plot = figure(plot_height=400, plot_width=400, title=“my sine wave”,
tools=“crosshair,pan,reset,save,wheel_zoom”,
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line(‘x’, ‘y’, source=source, line_width=3, line_alpha=0.6)

Set up widgets

text = TextInput(title=“title”, value=‘my sine wave’)
offset = Slider(title=“offset”, value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title=“amplitude”, value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title=“phase”, value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title=“frequency”, value=1.0, start=0.1, end=5.1, step=0.1)

Set up callbacks

def update_title(attrname, old, new):
plot.title.text = text.value

text.on_change(‘value’, update_title)

def update_data(attrname, old, new):

Get the current slider values

a = amplitude.value
b = offset.value
w = phase.value
k = freq.value

Generate the new curve

x = np.linspace(0, 4np.pi, N)
y = a
np.sin(k*x + w) + b

source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
w.on_change(‘value’, update_data)

Set up layouts and add to document

inputs = widgetbox(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = “Sliders”

So, it’s not a list comprehension that doesn’t work, it’s your plot not showing up at all?
First of all, your code works perfectly for me (I had to fix formatting - please use code formatting in the toolbar next time).

The code doesn’t work only if I provide a non-existant path to “data_path”.

Do you see any errors in the console where you start “bokeh serve” or in the JS console in the browser?

  • Eugene
···

On Wednesday, January 31, 2018 at 11:51:12 PM UTC+7, Thomas Cercato wrote:

This is the slider example in Bokeh website gallery, modifed with a folder data path and a list comprehesion. I don’t see anything when I run a session with bokeh serve --show ‘myfile.py’.

import os
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

data_path = ‘chose\your\data\path’
file_list = [file.replace(’.parquet’, ‘’) for file in os.listdir(data_path) if file.endswith(’.parquet’)]

Set up data

N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

Set up plot

plot = figure(plot_height=400, plot_width=400, title=“my sine wave”,
tools=“crosshair,pan,reset,save,wheel_zoom”,
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line(‘x’, ‘y’, source=source, line_width=3, line_alpha=0.6)

Set up widgets

text = TextInput(title=“title”, value=‘my sine wave’)
offset = Slider(title=“offset”, value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title=“amplitude”, value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title=“phase”, value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title=“frequency”, value=1.0, start=0.1, end=5.1, step=0.1)

Set up callbacks

def update_title(attrname, old, new):
plot.title.text = text.value

text.on_change(‘value’, update_title)

def update_data(attrname, old, new):

Get the current slider values

a = amplitude.value
b = offset.value
w = phase.value
k = freq.value

Generate the new curve

x = np.linspace(0, 4np.pi, N)
y = a
np.sin(k*x + w) + b

source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
w.on_change(‘value’, update_data)

Set up layouts and add to document

inputs = widgetbox(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = “Sliders”

Yeah, basically I don’t see anything in my browser windows. But if I remove the list comprehension, everything works just fine. I also don’t see any error when I start the bokeh serve command, but I have not tried the JS console in browser (no idea what it is, I’m n00b).

Sorry for the code formatting.

···

Il giorno mercoledì 31 gennaio 2018 17:57:15 UTC+1, Eugene Pakhomov ha scritto:

So, it’s not a list comprehension that doesn’t work, it’s your plot not showing up at all?
First of all, your code works perfectly for me (I had to fix formatting - please use code formatting in the toolbar next time).

The code doesn’t work only if I provide a non-existant path to “data_path”.

Do you see any errors in the console where you start “bokeh serve” or in the JS console in the browser?

  • Eugene

On Wednesday, January 31, 2018 at 11:51:12 PM UTC+7, Thomas Cercato wrote:

This is the slider example in Bokeh website gallery, modifed with a folder data path and a list comprehesion. I don’t see anything when I run a session with bokeh serve --show ‘myfile.py’.

import os
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

data_path = ‘chose\your\data\path’
file_list = [file.replace(’.parquet’, ‘’) for file in os.listdir(data_path) if file.endswith(’.parquet’)]

Set up data

N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

Set up plot

plot = figure(plot_height=400, plot_width=400, title=“my sine wave”,
tools=“crosshair,pan,reset,save,wheel_zoom”,
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line(‘x’, ‘y’, source=source, line_width=3, line_alpha=0.6)

Set up widgets

text = TextInput(title=“title”, value=‘my sine wave’)
offset = Slider(title=“offset”, value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title=“amplitude”, value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title=“phase”, value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title=“frequency”, value=1.0, start=0.1, end=5.1, step=0.1)

Set up callbacks

def update_title(attrname, old, new):
plot.title.text = text.value

text.on_change(‘value’, update_title)

def update_data(attrname, old, new):

Get the current slider values

a = amplitude.value
b = offset.value
w = phase.value
k = freq.value

Generate the new curve

x = np.linspace(0, 4np.pi, N)
y = a
np.sin(k*x + w) + b

source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
w.on_change(‘value’, update_data)

Set up layouts and add to document

inputs = widgetbox(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = “Sliders”

It depends on your browser and OS. In Google Chrome you can press F12, I think it’s a cross-platform key shortcut. There you’ll see “Console” tab.

  • Eugene
···

On Thursday, February 1, 2018 at 12:00:58 AM UTC+7, Thomas Cercato wrote:

Yeah, basically I don’t see anything in my browser windows. But if I remove the list comprehension, everything works just fine. I also don’t see any error when I start the bokeh serve command, but I have not tried the JS console in browser (no idea what it is, I’m n00b).

Sorry for the code formatting.

Il giorno mercoledì 31 gennaio 2018 17:57:15 UTC+1, Eugene Pakhomov ha scritto:

So, it’s not a list comprehension that doesn’t work, it’s your plot not showing up at all?
First of all, your code works perfectly for me (I had to fix formatting - please use code formatting in the toolbar next time).

The code doesn’t work only if I provide a non-existant path to “data_path”.

Do you see any errors in the console where you start “bokeh serve” or in the JS console in the browser?

  • Eugene

On Wednesday, January 31, 2018 at 11:51:12 PM UTC+7, Thomas Cercato wrote:

This is the slider example in Bokeh website gallery, modifed with a folder data path and a list comprehesion. I don’t see anything when I run a session with bokeh serve --show ‘myfile.py’.

import os
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

data_path = ‘chose\your\data\path’
file_list = [file.replace(’.parquet’, ‘’) for file in os.listdir(data_path) if file.endswith(’.parquet’)]

Set up data

N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

Set up plot

plot = figure(plot_height=400, plot_width=400, title=“my sine wave”,
tools=“crosshair,pan,reset,save,wheel_zoom”,
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line(‘x’, ‘y’, source=source, line_width=3, line_alpha=0.6)

Set up widgets

text = TextInput(title=“title”, value=‘my sine wave’)
offset = Slider(title=“offset”, value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title=“amplitude”, value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title=“phase”, value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title=“frequency”, value=1.0, start=0.1, end=5.1, step=0.1)

Set up callbacks

def update_title(attrname, old, new):
plot.title.text = text.value

text.on_change(‘value’, update_title)

def update_data(attrname, old, new):

Get the current slider values

a = amplitude.value
b = offset.value
w = phase.value
k = freq.value

Generate the new curve

x = np.linspace(0, 4np.pi, N)
y = a
np.sin(k*x + w) + b

source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
w.on_change(‘value’, update_data)

Set up layouts and add to document

inputs = widgetbox(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = “Sliders”