New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jslink not working for dropdown widget, why? #2382
Comments
The Dropdown index and labels are synced to javascript, but not the value. Sometimes the value can be a python object which cannot be transferred over to javascript, so you don't have access to the value on the javascript side. Does jslinking the label work? CC also @maartenbreddels |
I tried to jslink the label with another widget and i got same error
But this time, it's Dropdown.label which cannot be synced. However, I can jslink the index. I will try something else. |
Is there a solution for this? Can we use the index to reference the original list within the Text widget? |
First, note that this only affects jslink. linking on the python side should work fine.
@maartenbreddels had the same idea a while ago. I'm not sure it was pursued.
It does look like perhaps this is an overly restrictive check. I wonder what happens if we remove the check that we are only jslinking synced attributes, and trust the user to link things that make sense. |
@jasongrout My goal is to use the widget with jupyter-sphinx. I'm not sure I understand what you mean by "linking on the python side". Would I use drop = widgets.Dropdown(options=[1,2])
text = widgets.Text()
l = widgets.link((text, 'value'), (drop, 'value'))
display(drop,text) |
I believe I need to link on the js side in order to achieve my goal of using this widget in a static html page |
Yes, it does appear that do need jslink for it to work with jupyter-sphinx. Then I think a next step would be to think through the ramifications of removing that check apparently in the link code of only linking traits that are synced, or explore @maartenbreddels idea from a long time about a way to use the index to look up in the list of labels. |
Reopening since it looks like the discussion may be leading to a solution for this. |
@jasongrout It seems like @maartenbreddels's idea about using the index to look up labels is the quickest short term solution. Could you point me to the area in the code where |
Is this what you are looking for? ipywidgets/packages/controls/src/widget_selection.ts Lines 112 to 115 in a311d75
|
@jasongrout I thought I could do something like this: from ipywidgets.widgets import Textarea
from ipywidgets.widgets.widget_string import _String
import ipywidgets as widgets
OPTIONS=[4,5,6,7,8]
class FunctionText(Textarea):
def __init__(self, value=None, method=lambda x:x, **kwargs):
if value is not None:
kwargs['value'] = method(value)
super(_String, self).__init__(**kwargs)
def method(index):
return str(OPTIONS[int(index)])
drop = widgets.Dropdown(options=OPTIONS)
text = FunctionText(value='4', method=method)
l = widgets.jslink((text, 'value'), (drop, 'index'))
display(drop,text) But on second thought, the subclassing would probably have to be on the javascript side and is likely more complex than I was hoping! |
A kind of general widget that I once created that can take a list of widgest, and an index is here: https://github.com/jupyter-widgets/ipywidgets/pull/2232/files#diff-15c92b08e71d4d11cfc6f037268c839cR76 I've always been a fan of having a more rich jslink. I think this should first live outside of core ipywidgets, and could be very expressive with @martinRenou 's py2vega! |
Here is a quick hack to the existing classes and methods: from ipywidgets import RadioButtons, Text, Select, Dropdown, jslink
from ipywidgets.widgets.widget import Widget, register, widget_serialization
from ipywidgets.widgets.widget_core import CoreWidget
from IPython.display import display
from traitlets import Unicode, Tuple, Instance, TraitError
Dropdown.value.tag(sync=True)
selection = Dropdown(options=[('1','a'),('2','b'),('3','c')])
text = Text(value='a')
l = jslink((text, 'value'), (selection, 'value'))
display(selection,text) |
Above works fine in a notebook, but not in a static page. |
Also, relevant to this discussion: if jslink could take .a parameter like |
I like your hack :) my guess is that it doesn't work in the static version because value does not get serialized in the json. The issue back then is we didn't know how we would evaluate the transformation, I think with py2vega, we have a much better story now (without allowing arbitrary code execution in the frontend/browser). |
Actually, I'm not sure what to modify in the serialization? Is there a quick hack for this too? |
Is it possible to enumerate and pre-compute all the possible states of the widget? Is that what |
@maartenbreddels, @jasongrout I'd like to help this issue along. Is there something I can do to move it forward? Or can you point me in the right direction on how to approach this and I'll take a stab at a PR? |
@jetheurer I see two possible options.
|
Some useful libraries might be (apart from vega/py2vega):
|
@vidartf +1 for option 2 |
I would mark that as "help needed", as I don't have enough capacity to work on that at the moment :) |
My approach now is to create a new widget using ipyvuetify template system: |
Hello,
I created a leaflet map on my jupyterlab notebook with WidgetsControl.
I added a widgets.dropdown that allows me to change the layer using a widgets.link that works.
Now, I want to put my map in an HTML page by changing the widgets.link by a widgets.jslink but an error occurs.
TypeError: Dropdown.value can not be synced
I would like to know if it's possible or not to do this.
Thanks.
The text was updated successfully, but these errors were encountered: