nteract / vdom

šŸŽ„ Virtual DOM for Python
https://github.com/nteract/vdom/blob/master/docs/mimetype-spec.md
BSD 3-Clause "New" or "Revised" License
222 stars 35 forks source link

Example code from `docs/event_spec.md` does not work #73

Closed st-pasha closed 5 years ago

st-pasha commented 6 years ago

When running this code in a JupyterLab notebook

from IPython.display import display
from vdom.helpers import button

count = 0

def on_click(event):
    global count
    count += 1
    my_display.update(counter())

def counter():
    return button(str(count), onClick=on_click, style={'width': 100, 'height': 40})

my_display = display(counter(), display_id=True)

my_display;

the following error occurs:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-3a32abc6a1e2> in <module>()
     12     return button(str(count), onClick=on_click, style={'width': 100, 'height': 40})
     13 
---> 14 my_display = display(counter(), display_id=True)
     15 
     16 my_display;

~/py36/lib/python3.6/site-packages/IPython/core/display.py in display(include, exclude, metadata, transient, display_id, *objs, **kwargs)
    307                 # kwarg-specified metadata gets precedence
    308                 _merge(md_dict, metadata)
--> 309             publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
    310     if display_id:
    311         return DisplayHandle(display_id)

~/py36/lib/python3.6/site-packages/IPython/core/display.py in publish_display_data(data, metadata, source, transient, **kwargs)
    131         data=data,
    132         metadata=metadata,
--> 133         **kwargs
    134     )
    135 

TypeError: publish() got an unexpected keyword argument 'transient'

My environment is: vdom-0.5 IPython-7.0.1 jupyterlab-0.35.0

st-pasha commented 6 years ago

After digging around, the error has disappeared when I upgraded ipykernel from 4.5.2 to the latest 5.1.0 version. Or, rather, it was replaced with a different error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-7e0ca782c360> in <module>
     12     return button(str(count), onClick=on_click, style={'width': 100, 'height': 40})
     13 
---> 14 my_display = display(counter(), display_id=True)
     15 
     16 my_display;

~/py36/lib/python3.6/site-packages/IPython/core/display.py in display(include, exclude, metadata, transient, display_id, *objs, **kwargs)
    303                 # kwarg-specified metadata gets precedence
    304                 _merge(md_dict, metadata)
--> 305             publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
    306     if display_id:
    307         return DisplayHandle(display_id)

~/py36/lib/python3.6/site-packages/IPython/core/display.py in publish_display_data(data, metadata, source, transient, **kwargs)
    118         data=data,
    119         metadata=metadata,
--> 120         **kwargs
    121     )
    122 

~/py36/lib/python3.6/site-packages/ipykernel/zmqshell.py in publish(self, data, metadata, source, transient, update)
    127         # hooks before potentially sending.
    128         msg = self.session.msg(
--> 129             msg_type, json_clean(content),
    130             parent=self.parent_header
    131         )

~/py36/lib/python3.6/site-packages/ipykernel/jsonutil.py in json_clean(obj)
    189         out = {}
    190         for k,v in iteritems(obj):
--> 191             out[unicode_type(k)] = json_clean(v)
    192         return out
    193     if isinstance(obj, datetime):

~/py36/lib/python3.6/site-packages/ipykernel/jsonutil.py in json_clean(obj)
    189         out = {}
    190         for k,v in iteritems(obj):
--> 191             out[unicode_type(k)] = json_clean(v)
    192         return out
    193     if isinstance(obj, datetime):

~/py36/lib/python3.6/site-packages/ipykernel/jsonutil.py in json_clean(obj)
    189         out = {}
    190         for k,v in iteritems(obj):
--> 191             out[unicode_type(k)] = json_clean(v)
    192         return out
    193     if isinstance(obj, datetime):

~/py36/lib/python3.6/site-packages/ipykernel/jsonutil.py in json_clean(obj)
    189         out = {}
    190         for k,v in iteritems(obj):
--> 191             out[unicode_type(k)] = json_clean(v)
    192         return out
    193     if isinstance(obj, datetime):

~/py36/lib/python3.6/site-packages/ipykernel/jsonutil.py in json_clean(obj)
    195 
    196     # we don't understand it, it's probably an unserializable object
--> 197     raise ValueError("Can't clean for JSON: %r" % obj)

ValueError: Can't clean for JSON: <function on_click at 0x11010b9d8>
st-pasha commented 6 years ago

It seems the encode_attributes function in core.py is supposed to handle callback methods, yet it is not called from anywhere.

rgbkrk commented 6 years ago

cc @gnestor šŸ‘€

gnestor commented 6 years ago

@st-pasha Sorry the front-end side hasn't been implemented yet. There is an open PR on jupyter-renderers (https://github.com/jupyterlab/jupyter-renderers/pull/134) that did implement this in JupyterLab, but this stuff needs to be moved into @nteract/transform-vdom and the kernel-related parts in @jupyterlab/vdom-extension.

saluto commented 5 years ago

Is there any progress on this? It would be really great to have that functionality. Thanks.

gnestor commented 5 years ago

Just waiting for https://github.com/jupyterlab/jupyterlab/pull/5670 to be merged in jupyterlab

gnestor commented 5 years ago

It was merged a while back, closing now...