Is it possible for widget callback to call sub-function?

Hi,

Is it possible to have a widget callback function call a sub-function?

I have a number of callback functions that share some common code and I’d like to move that common code into a function to avoid code duplication.

I see the color_sliders example: color_sliders.py — Bokeh 2.4.2 Documentation

defines sub-functions within the definition of the callback but this doesn’t help my situation.

Here is a contrived example of what I’m trying to accomplish:

sub_function = CustomJS( args=dict(source=source), code = “”"

source.trigger(‘change’);

“”")

callback_select = CustomJS( args=dict(source=source), code="""

var data = source.data;

var v = cb_obj.value;

data[‘y’] = data[v];

sub_function( source )

“”")

When callback_select is run, I get the error “sub_function is not defined”.

thanks

albert.

Hi,

···

On Thu, Dec 15, 2016 at 9:27 PM, Albert Chau [email protected] wrote:

Hi,

Is it possible to have a widget callback function call a sub-function?

I have a number of callback functions that share some common code and I’d like to move that common code into a function to avoid code duplication.

I see the color_sliders example: http://bokeh.pydata.org/en/latest/docs/gallery/color_sliders.html

defines sub-functions within the definition of the callback but this doesn’t help my situation.

Here is a contrived example of what I’m trying to accomplish:

sub_function = CustomJS( args=dict(source=source), code = “”"

source.trigger(‘change’);

“”")

callback_select = CustomJS( args=dict(source=source), code=“”"

var data = source.data;

var v = cb_obj.value;

data[‘y’] = data[v];

sub_function( source )

“”")

When callback_select is run, I get the error “sub_function is not defined”.

sub_function is a name only visible in Python. In bokehjs callbacks are anonymous functions, so code sharing like this isn’t possible. In fact, without adding some custom javascript to bokeh’s output, it’s not that easy to add javascript functions for reuse. I may consider handling this within https://github.com/bokeh/bokeh/issues/3674, which asks only for variables, but we can extend it a little further.

Mateusz

thanks

albert.

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/7e858325-78ec-4126-a18d-fd863df6ce4d%40continuum.io.

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