Closed paul-shannon closed 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:
_view_name
is a unicode attribute with default value 'MeshView'_model_name
is a unicode attribute with default value 'MeshModel'geometry
is an attribute that is an instance of class Geometrymaterial
is an attribute that is an instance of class MaterialThe 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/)
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
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?
(@SylvainCorlay - do you know if those examples are up to date?)
Yes they are.
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?
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.
Very helpful suggestions and advice. Thanks!
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:
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.