Using my own python function inside CustomJS.from_py_func()

I’m trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:

Here is a minimal example of what I am trying to do.

---------start of example---------------

from bokeh.layouts import column

from bokeh.models import CustomJS, ColumnDataSource, Slider

from bokeh.plotting import Figure, output_file, show

create my pow function

def Mypow(base,power):

return base**power

do a sanity check

print(‘Mypow test 2**4 is:’,Mypow(2,4))

output_file(“callback.html”)

x = [x*0.005 for x in range(0, 200)]

y = x

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

plot = Figure(plot_width=400, plot_height=400)

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

def callback(source=source, window=None):

data = source.get('data')

f = cb_obj.get('value')

x, y = data['x'], data['y']

for i in range(len(x)):

y[i] = window.Math.pow(x[i], f)

    y[i] = window.Mypow(x[i], f)     # attempt to use my pow function instead of Math.pow

source.trigger('change')

slider = Slider(start=0.1, end=4, value=1, step=.1, title=“power”,

            callback=CustomJS.from_py_func(callback))

layout = column(slider, plot)

show(layout)

---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I’m happy to provide any additional information.

I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,

Andrew

Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is "compiled" from Python using from_py_func). Accordingly, *all* the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

  https://demo.bokeh.org/

Thanks,

Bryan

···

On Jul 14, 2016, at 10:15 AM, Andrew Hamilton <[email protected]> wrote:

I'm trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:
Interaction — Bokeh 3.3.2 Documentation

Here is a minimal example of what I am trying to do.

---------start of example---------------
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show

# create my pow function
def Mypow(base,power):
    return base**power

# do a sanity check
print('Mypow test 2**4 is:',Mypow(2,4))

output_file("callback.html")

x = [x*0.005 for x in range(0, 200)]
y = x

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

plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

def callback(source=source, window=None):
    data = source.get('data')
    f = cb_obj.get('value')
    x, y = data['x'], data['y']
    for i in range(len(x)):
# y[i] = window.Math.pow(x[i], f)
        y[i] = window.Mypow(x[i], f) # attempt to use my pow function instead of Math.pow
    source.trigger('change')

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power",
                callback=CustomJS.from_py_func(callback))
layout = column(slider, plot)
show(layout)
---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I'm happy to provide any additional information.
I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,
Andrew

--
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/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

As another alternative, if you happen to be in the notebook, you could consider `push_notebook` as an option as well. See example notebooks here:

  https://github.com/bokeh/bokeh/tree/master/examples/howto/notebook_comms

Bryan

···

On Jul 14, 2016, at 10:32 AM, Bryan Van de Ven <[email protected]> wrote:

Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is "compiled" from Python using from_py_func). Accordingly, *all* the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

  https://demo.bokehplots.com/

Thanks,

Bryan

On Jul 14, 2016, at 10:15 AM, Andrew Hamilton <[email protected]> wrote:

I'm trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:
Interaction — Bokeh 3.3.2 Documentation

Here is a minimal example of what I am trying to do.

---------start of example---------------
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show

# create my pow function
def Mypow(base,power):
   return base**power

# do a sanity check
print('Mypow test 2**4 is:',Mypow(2,4))

output_file("callback.html")

x = [x*0.005 for x in range(0, 200)]
y = x

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

plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

def callback(source=source, window=None):
   data = source.get('data')
   f = cb_obj.get('value')
   x, y = data['x'], data['y']
   for i in range(len(x)):
# y[i] = window.Math.pow(x[i], f)
       y[i] = window.Mypow(x[i], f) # attempt to use my pow function instead of Math.pow
   source.trigger('change')

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power",
               callback=CustomJS.from_py_func(callback))
layout = column(slider, plot)
show(layout)
---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I'm happy to provide any additional information.
I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,
Andrew

--
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/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Thanks very much for the clarification. I will try both the server and the push_notebook.

Cheers,

Andrew

···

On Thursday, July 14, 2016 at 5:34:11 PM UTC+2, Bryan Van de ven wrote:

As another alternative, if you happen to be in the notebook, you could consider push_notebook as an option as well. See example notebooks here:

    [https://github.com/bokeh/bokeh/tree/master/examples/howto/notebook_comms](https://github.com/bokeh/bokeh/tree/master/examples/howto/notebook_comms)

Bryan

On Jul 14, 2016, at 10:32 AM, Bryan Van de Ven [email protected] wrote:

Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is “compiled” from Python using from_py_func). Accordingly, all the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

    [https://demo.bokehplots.com/](https://demo.bokehplots.com/)

Thanks,

Bryan

On Jul 14, 2016, at 10:15 AM, Andrew Hamilton [email protected] wrote:

I’m trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:
http://bokeh.pydata.org/en/latest/docs/user_guide/interaction.html#customjs-with-a-python-function

Here is a minimal example of what I am trying to do.

---------start of example---------------

from bokeh.layouts import column

from bokeh.models import CustomJS, ColumnDataSource, Slider

from bokeh.plotting import Figure, output_file, show

create my pow function

def Mypow(base,power):

return base**power

do a sanity check

print(‘Mypow test 2**4 is:’,Mypow(2,4))

output_file(“callback.html”)

x = [x*0.005 for x in range(0, 200)]

y = x

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

plot = Figure(plot_width=400, plot_height=400)

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

def callback(source=source, window=None):

data = source.get(‘data’)

f = cb_obj.get(‘value’)

x, y = data[‘x’], data[‘y’]

for i in range(len(x)):

y[i] = window.Math.pow(x[i], f)

   y[i] = window.Mypow(x[i], f)     # attempt to use my pow function instead of Math.pow

source.trigger(‘change’)

slider = Slider(start=0.1, end=4, value=1, step=.1, title=“power”,

           callback=CustomJS.from_py_func(callback))

layout = column(slider, plot)

show(layout)

---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I’m happy to provide any additional information.

I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,

Andrew


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/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi Bryan,

The server almost does what I would like. I’ve got the simple “myapp.py” example from the server documentation to run with my “mypow” method.

I would really like a slider to control the python callback method. It works for the button.on_click() method, because that accepts a method. However, the Slider object requires an object of type Callback, which I can’t see how to create from my python method.

Is there any way to have a slider control a python callback method?

Thanks,

Andrew

···

On Thursday, July 14, 2016 at 5:32:34 PM UTC+2, Bryan Van de ven wrote:

Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is “compiled” from Python using from_py_func). Accordingly, all the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

    [https://demo.bokehplots.com/](https://demo.bokehplots.com/)

Thanks,

Bryan

On Jul 14, 2016, at 10:15 AM, Andrew Hamilton [email protected] wrote:

I’m trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:
http://bokeh.pydata.org/en/latest/docs/user_guide/interaction.html#customjs-with-a-python-function

Here is a minimal example of what I am trying to do.

---------start of example---------------

from bokeh.layouts import column

from bokeh.models import CustomJS, ColumnDataSource, Slider

from bokeh.plotting import Figure, output_file, show

create my pow function

def Mypow(base,power):

return base**power

do a sanity check

print(‘Mypow test 2**4 is:’,Mypow(2,4))

output_file(“callback.html”)

x = [x*0.005 for x in range(0, 200)]

y = x

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

plot = Figure(plot_width=400, plot_height=400)

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

def callback(source=source, window=None):

data = source.get('data')
f = cb_obj.get('value')
x, y = data['x'], data['y']
for i in range(len(x)):

y[i] = window.Math.pow(x[i], f)

    y[i] = window.Mypow(x[i], f)     # attempt to use my pow function instead of Math.pow
source.trigger('change')

slider = Slider(start=0.1, end=4, value=1, step=.1, title=“power”,

            callback=CustomJS.from_py_func(callback))

layout = column(slider, plot)

show(layout)

---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I’m happy to provide any additional information.

I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,

Andrew


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/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Can you post some example code or an exact error message? It is difficult to speculate. An example of server callbacks with sliders is here:

  https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py

Thanks,

Bryan

···

On Jul 15, 2016, at 9:15 AM, Andrew Hamilton <[email protected]> wrote:

Hi Bryan,

The server almost does what I would like. I've got the simple "myapp.py" example from the server documentation to run with my "mypow" method.

I would really like a slider to control the python callback method. It works for the button.on_click() method, because that accepts a method. However, the Slider object requires an object of type Callback, which I can't see how to create from my python method.

Is there any way to have a slider control a python callback method?

Thanks,
Andrew

On Thursday, July 14, 2016 at 5:32:34 PM UTC+2, Bryan Van de ven wrote:
Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is "compiled" from Python using from_py_func). Accordingly, *all* the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

        https://demo.bokehplots.com/

Thanks,

Bryan

> On Jul 14, 2016, at 10:15 AM, Andrew Hamilton <[email protected]> wrote:
>
> I'm trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.
>
> Based on the example at:
> Interaction — Bokeh 3.3.2 Documentation
>
> Here is a minimal example of what I am trying to do.
>
> ---------start of example---------------
> from bokeh.layouts import column
> from bokeh.models import CustomJS, ColumnDataSource, Slider
> from bokeh.plotting import Figure, output_file, show
>
> # create my pow function
> def Mypow(base,power):
> return base**power
>
> # do a sanity check
> print('Mypow test 2**4 is:',Mypow(2,4))
>
> output_file("callback.html")
>
> x = [x*0.005 for x in range(0, 200)]
> y = x
>
> source = ColumnDataSource(data=dict(x=x, y=y))
>
> plot = Figure(plot_width=400, plot_height=400)
> plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
>
> def callback(source=source, window=None):
> data = source.get('data')
> f = cb_obj.get('value')
> x, y = data['x'], data['y']
> for i in range(len(x)):
> # y[i] = window.Math.pow(x[i], f)
> y[i] = window.Mypow(x[i], f) # attempt to use my pow function instead of Math.pow
> source.trigger('change')
>
> slider = Slider(start=0.1, end=4, value=1, step=.1, title="power",
> callback=CustomJS.from_py_func(callback))
> layout = column(slider, plot)
> show(layout)
> ---------end of example---------------
>
> This code produces the plot, but changing the slider has no effect on the plot.
>
> Any help would be greatly appreciated! I'm happy to provide any additional information.
> I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5
>
> Thanks,
> Andrew
>
>
> --
> 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 bokeh+un...@continuum.io.
> To post to this group, send email to bo...@continuum.io.
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io\.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

--
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/6affa5b9-486e-4725-838f-a912223880ec%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Exactly what I was looking for, it works now. It was the on_change that I didn’t understand.

Thanks,

Andrew

···

On Friday, July 15, 2016 at 5:58:50 PM UTC+2, Bryan Van de ven wrote:

Can you post some example code or an exact error message? It is difficult to speculate. An example of server callbacks with sliders is here:

    [https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py](https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py)

Thanks,

Bryan

On Jul 15, 2016, at 9:15 AM, Andrew Hamilton [email protected] wrote:

Hi Bryan,

The server almost does what I would like. I’ve got the simple “myapp.py” example from the server documentation to run with my “mypow” method.

I would really like a slider to control the python callback method. It works for the button.on_click() method, because that accepts a method. However, the Slider object requires an object of type Callback, which I can’t see how to create from my python method.

Is there any way to have a slider control a python callback method?

Thanks,

Andrew

On Thursday, July 14, 2016 at 5:32:34 PM UTC+2, Bryan Van de ven wrote:

Andrew,

I am sorry if it was unclear, but this kind of usage cannot be supported. All the code in CustomJS callback ends up as JavaScript code, to execute in the browser (even if it is “compiled” from Python using from_py_func). Accordingly, all the code has to be in the single block of code or function that defines the callback. If you require to call actual python code or functions, or have more sophisticated usage needs, that is what Bokeh server apps can accomplish. You can see several examples, with links to source code, here:

    [https://demo.bokehplots.com/](https://demo.bokehplots.com/)

Thanks,

Bryan

On Jul 14, 2016, at 10:15 AM, Andrew Hamilton [email protected] wrote:

I’m trying to use my own function inside the CustomJS.from_py_func(callback), but it does not seem to be working.

Based on the example at:
http://bokeh.pydata.org/en/latest/docs/user_guide/interaction.html#customjs-with-a-python-function

Here is a minimal example of what I am trying to do.

---------start of example---------------
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show

create my pow function

def Mypow(base,power):
return base**power

do a sanity check

print(‘Mypow test 2**4 is:’,Mypow(2,4))

output_file(“callback.html”)

x = [x*0.005 for x in range(0, 200)]
y = x

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

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

def callback(source=source, window=None):
data = source.get(‘data’)
f = cb_obj.get(‘value’)
x, y = data[‘x’], data[‘y’]
for i in range(len(x)):

y[i] = window.Math.pow(x[i], f)

    y[i] = window.Mypow(x[i], f)     # attempt to use my pow function instead of Math.pow
source.trigger('change')

slider = Slider(start=0.1, end=4, value=1, step=.1, title=“power”,
callback=CustomJS.from_py_func(callback))
layout = column(slider, plot)
show(layout)
---------end of example---------------

This code produces the plot, but changing the slider has no effect on the plot.

Any help would be greatly appreciated! I’m happy to provide any additional information.
I am using python 3.5 with Bokeh 0.12.0, jupyter 1.0.0, flexx 0.4.1 on OSX 10.11.5

Thanks,
Andrew


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/2e43119b-2cd7-43f1-83c1-d57e5db5cba3%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.


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/6affa5b9-486e-4725-838f-a912223880ec%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.