jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
951 stars 188 forks source link

background reading, to better understand pythreejs code? #44

Closed paul-shannon closed 8 years ago

paul-shannon commented 8 years ago

I have been studying pythreejs.py in order to understand ipywidgets, and tolearn how to create my own custom widget. And maybe as prelude to creating, as you did with sage, some higher level visualizations. Can I get a little advice from you? Perhaps you can point me towards background reading to fill in what I don't understand?

My confusion begins with the implicit operations which take place in the Mesh constructor, as called out of the first line of the Examples notebook:

ball = Mesh(geometry=SphereGeometry(radius=1),
            material=LambertMaterial(color='red'), 
            position=[2,1,0])

class Mesh(Object3d):
   _view_name = Unicode('MeshView').tag(sync=True)
   _model_name = Unicode('MeshModel').tag(sync=True)
   geometry = Instance(Geometry).tag(sync=True, **widget_serialization)
   material = Instance(Material).tag(sync=True, **widget_serialization)

In particular, I scratched my head over the absence of ctor arguments in the class definition. No init method. I imagine that traitlet configuration and the "_widget_serialization" argument dig out the geometry and material parameters. Then the constructor of the base class, Object3d, is implicitly called and (?) the position is extracted from children (another traitlet initialized from _widget_serialization perhaps?). That's my best guess...

Thus there is a lot going on here which I simply don't grasp. A few hours of web searching and reading have clarified a few things. If you can point me to some relevant documentation, some background material to read, I will be very grateful.

jasongrout commented 8 years ago

The init is part of how traitlets works. Any of the traitlet attributes can be initialized with keyword arguments in the construction call. So what this is doing is declaring the Mesh class to have several attributes:

The constructor then initializes geometry and material attributes, and passes the remaining attributes up to the parent constructor (Object3d), which initializes its position attribute from the position argument.

Does that help? It sounds like your questions here are really questions about traitlets: https://github.com/ipython/traitlets (docs at http://traitlets.readthedocs.io/en/stable/)

jasongrout commented 8 years ago

The **widget_serialization part has to do with encoding how state is transferred to the browser, and is an ipywidgets thing. Basically, we need a way to sync the state of the object (i.e., any attributes with the sync=True tag) between python and javascript. For attributes that are basic values, like Unicode, Float, etc., we can easily convert those. However, more complicated attributes need custom methods to serialize and deserialize them for communication with javascript. In this case, these attributes hold references to instances of widgets, so we supply the serialization functions ipywidgets provides for widgets: https://github.com/ipython/ipywidgets/blob/eb4dcd9956d88ae842bff7822a9c32fc038906dd/ipywidgets/widgets/widget.py#L44

paul-shannon commented 8 years ago

Thanks, Jason: I'll dig into the reading you suggest.

Is there a custom ipywidget demo available for study, one that is a couple of degrees more complex than HelloWorld, not as rich as bqplot, pythreejs, or ipyleaflet?

jasongrout commented 8 years ago

https://github.com/ipython/ipywidgets/blob/master/docs/source/examples/Custom%20Widget%20-%20Hello%20World.ipynb and https://github.com/ipython/ipywidgets/blob/master/docs/source/examples/Date%20Picker%20Widget.ipynb might be good places to read.

jasongrout commented 8 years ago

(@SylvainCorlay - do you know if those examples are up to date?)

SylvainCorlay commented 8 years ago

Yes they are.

paul-shannon commented 8 years ago

These two examples do not appear to follow the cookiecutter approach to "packaging and distributing Jupyter widgets" described at the bottom of the HelloWorld notebook.

Does the code for these two examples, as presented, map into that template form in a straightforward manner? So that a notebook/widget novice could realistically attempt the conversion?

SylvainCorlay commented 8 years ago

Yes, the notebook explain in details how to implement jupyter widgets in an interactive fashion in the jupyter notebook.

The cookiecutter project is meant for people who want to package them in an installable package.

paul-shannon commented 8 years ago

Very helpful suggestions and advice. Thanks!