ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

Extending jslink?

Open maartenbreddels opened this issue 8 years ago • 3 comments

jslink is really useful for having some frontend only communication, however there are more use cases I can think of that would be useful. I wonder if some thought went already into this, if not maybe it is useful to discuss some use cases that could be implemented simply.

  • bijective transformation, for instance a slider that controls the log of a value, or a linear transformation. Could be a js code snippet argument to jslink, or maybe only a property to the Link class, and a different function to set this, e.g. jslinkf((widget1, 'value', 'Math.log(value)'), (widget2, 'value', 'Math.exp(value)'
  • Regarding #1091 I can imagine this. We have a some widget, with a property children (list of widgets), and we want to set another property based on a togglebutton (or any _selector subclass). On the JS side you could use the .index to get the child from the list, and then assign it to a target. jslinkXXX((selector, 'index'), (some_widget, 'children'), (visible_panel_proxy, 'child')). I hope this example is clear.
  • Sometimes if something changes, you do not want to link that 'something', but a propery of that 'something'. Say if scatter.style changes, you want to link scatter.style["color"] to bar.selected_style["color"], e.g. jslinkYYY((scatter, 'style', 'color'), (bar, 'selected_style', 'color')).

maartenbreddels avatar Feb 14 '17 15:02 maartenbreddels

We were thinking of having a js container run an arbitrary js transformation specified in a third transform argument, a bit like in traitlets' link. Security is tricky, but can probably be achieved.

SylvainCorlay avatar Feb 14 '17 17:02 SylvainCorlay

Been discussing with @SylvainCorlay about transformations, Similar to traitlets, would be nice to have:

jsdlink((slider1, 'index'), (floattext, 'value'), transformation='log(value)')

We probably don't want to use eval, so that leave us to choose a library for parsing that: From http://stackoverflow.com/questions/2276021/evaluating-a-string-as-a-mathematical-expression-in-javascript I found these:

  • http://silentmatt.com/javascript-expression-evaluator/
  • http://mathjs.org/

Another option I would like to see, is sth similar, but instead of a math transformation, it's is a non-linear mapping, but an option from say a dict:

animation_delay_toggle_button = widgets.ToggleButtons(
    options=['None', 'Fast, 'Regular', 'Slow'],
    description='Delay:',
)
jsdlink((animation_delay_toggle_button, 'index'), (figure, 'animation_delay'), \
 choice={0:0, 1:100, 2:500, 3:1000})

Using a dict it would also be possible to map strings to values.

Having this in jslink would also be nice, but lets wait the discussion/PR here

maartenbreddels avatar May 08 '17 18:05 maartenbreddels

Hi, sorry for digging up this topic, but I stumbled upon it when i searched for linking between a checkbox and a stack

Do you know if there have been some work on extending jslink, especially with the dict mapping ?

I know I can use the observe or interactive_output methods, but these require an active python kernel, and I would like it to be available for static widgets, because the notebook is part of a documentation that gets compiled with sphinx.

For now I'm using ToggleButtons that have an index that I can link with my stack, but having a selector only for 2 values is a bit overkill in my case.

ClementPinard avatar Mar 14 '24 17:03 ClementPinard