#!/usr/bin/env python # coding: utf-8 # In[1]: import sys sys.path.insert(0, '..') from branca.element import * # ## Element # This is the base brick of `branca`. You can create an `Element` in providing a template string: # In[2]: e = Element("This is fancy text") # Each element has an attribute `_name` and a unique `_id`. You also have a method `get_name` to get a unique string representation of the element. # In[3]: print(e._name, e._id) print(e.get_name()) # You can render an `Element` using the method `render`: # In[4]: e.render() # In the template, you can use keyword `this` for accessing the object itself ; and the keyword `kwargs` for accessing any keyword argument provided in the `render` method: # In[5]: e = Element("Hello {{kwargs['you']}}, my name is `{{this.get_name()}}`.") e.render(you='World') # Well, this is not really cool for now. What makes elements useful lies in the fact that you can create trees out of them. To do so, you can either use the method `add_child` or the method `add_to`. # In[6]: child = Element('This is the child.') parent = Element('This is the parent.').add_child(child) parent = Element('This is the parent.') child = Element('This is the child.').add_to(parent) # Now in the example above, embedding the one in the other does not change anything. # In[7]: print(parent.render(), child.render()) # But you can use the tree structure in the template. # In[8]: parent = Element("{% for child in this._children.values() %}{{child.render()}}{% endfor %}") Element('').add_to(parent) Element('').add_to(parent) parent.render() # As you can see, the child of an element are referenced in the `_children` attibute in the form of an `OrderedDict`. You can choose the key of each child in specifying a `name` in the `add_child` (or `add_to`) method: # In[9]: parent = Element("{% for child in this._children.values() %}{{child.render()}}{% endfor %}") Element('').add_to(parent, name='child_1') parent._children # That way, it's possible to overwrite a child in specifying the same name: # In[10]: Element('').add_to(parent, name='child_1') parent.render() # I hope you start to find it useful. # # In fact, the real interest of `Element` lies in the classes that inherit from it. The most important one is `Figure` described in the next section. # ## Figure # # A `Figure` represents an HTML document. It's composed of 3 parts (attributes): # # * `header` : corresponds to the `` part of the HTML document, # * `html` : corresponds to the `` part, # * `script` : corresponds to a `