<jupyter_start><jupyter_text>Element This is the base brick of `branca`. You can create an `Element` in providing a template string:<jupyter_code>e = Element("This is fancy text")<jupyter_output><empty_output><jupyter_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.<jupyter_code>print(e._name, e._id)
print(e.get_name())<jupyter_output>Element a1d0f648f7444f96b526931944247fd6
element_a1d0f648f7444f96b526931944247fd6<jupyter_text>You can render an `Element` using the method `render`:<jupyter_code>e.render()<jupyter_output><empty_output><jupyter_text>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:<jupyter_code>e = Element("Hello {{kwargs['you']}}, my name is `{{this.get_name()}}`.")
e.render(you='World')<jupyter_output><empty_output><jupyter_text>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`.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Now in the example above, embedding the one in the other does not change anything.<jupyter_code>print(parent.render(), child.render())<jupyter_output>This is the parent. This is the child.<jupyter_text>But you can use the tree structure in the template.<jupyter_code>parent = Element("<parent>{% for child in this._children.values() %}{{child.render()}}{% endfor %}</parent>")
Element('<child1/>').add_to(parent)
Element('<child2/>').add_to(parent)
parent.render()<jupyter_output><empty_output><jupyter_text>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:<jupyter_code>parent = Element("<parent>{% for child in this._children.values() %}{{child.render()}}{% endfor %}</parent>")
Element('<child1/>').add_to(parent, name='child_1')
parent._children<jupyter_output><empty_output><jupyter_text>That way, it's possible to overwrite a child in specifying the same name:<jupyter_code>Element('<child1_overwritten/>').add_to(parent, name='child_1')
parent.render()<jupyter_output><empty_output><jupyter_text>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.  FigureA `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 `` section that will be appended after the `` section.<jupyter_code>f = Figure()
print(f.render())<jupyter_output><!DOCTYPE html>
<head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>    
</body>
<script>    
</script><jupyter_text>You can for example create a beatiful cyan "hello-world" webpage in doing:<jupyter_code>f.header.add_child(Element("<style>body {background-color: #00ffff}</style>"))
f.html.add_child(Element("<h1>Hello world</h1>"))
print(f.render())<jupyter_output><!DOCTYPE html>
<head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <style>body {background-color: #00ffff}</style>
</head>
<body>    
    <h1>Hello world</h1>
</body>
<script>    
</script><jupyter_text>You can simply save the content of the `Figure` to a file, thanks to the `save` method:<jupyter_code>f.save('foo.html')
print(open('foo.html').read())<jupyter_output><!DOCTYPE html>
<head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <style>body {background-color: #00ffff}</style>
</head>
<body>    
    <h1>Hello world</h1>
</body>
<script>    
</script><jupyter_text>If you want to visualize it in the notebook, you can let `Figure._repr_html_` method do it's job in typing:<jupyter_code>f<jupyter_output><empty_output><jupyter_text>If this rendering is too large for you, you can force it's width and height:<jupyter_code>f.width = 300
f.height = 200
f<jupyter_output><empty_output><jupyter_text>Note that you can also define a `Figure`'s size in a matplotlib way:<jupyter_code>Figure(figsize=(5,5))<jupyter_output><empty_output><jupyter_text>MacroElement It happens you need to create elements that have multiple effects on a Figure. For this, you can use `MacroElement` whose template contains macros ; each macro writes something into the parent Figure's header, body and script.<jupyter_code>macro = MacroElement()
macro._template = Template(
    '{% macro header(this, kwargs) %}'
    'This is header of {{this.get_name()}}'
    '{% endmacro %}'

    '{% macro html(this, kwargs) %}'
    'This is html of {{this.get_name()}}'
    '{% endmacro %}'

    '{% macro script(this, kwargs) %}'
    'This is script of {{this.get_name()}}'
    '{% endmacro %}'
    )

print(Figure().add_child(macro).render())<jupyter_output><!DOCTYPE html>
<head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    This is header of macro_element_ea36a310ab8a4212a8c7ca754a4140fc
</head>
<body>    
    This is html of macro_element_ea36a310ab8a4212a8c7ca754a4140fc
</body>
<script>    
    This is script of macro_element_ea36a310ab8a4212a8c7ca754a4140fc
</script><jupyter_text>Link To embed javascript and css links in the header, you can use these class:<jupyter_code>js_link = JavascriptLink('https://example.com/javascript.js')
js_link.render()
css_link = CssLink('https://example.com/style.css')
css_link.render()<jupyter_output><empty_output><jupyter_text>Html An `Html` element enables you to create custom div to put in the *body* of your page.<jupyter_code>html = Html('Hello world')
html.render()<jupyter_output><empty_output><jupyter_text>It's designed to render the text *as you gave it*, so it won't work directly it you want to embed HTML code inside the div.<jupyter_code>Html('<b>Hello world</b>').render()<jupyter_output><empty_output><jupyter_text>For this, you have to set `script=True` and it will work:<jupyter_code>Html('<b>Hello world</b>', script=True).render()<jupyter_output><empty_output><jupyter_text>IFrame If you need to embed a full webpage (with separate javascript environment), you can use `IFrame`.<jupyter_code>iframe = IFrame('Hello World')
iframe.render()<jupyter_output><empty_output><jupyter_text>As you can see, it will embed the full content of the iframe in a *base64* string so that the ouput looks like:<jupyter_code>f = Figure(height=180)
f.html.add_child(Element("Before the frame"))
f.html.add_child(IFrame('In the frame', height='100px'))
f.html.add_child(Element("After the frame"))
f<jupyter_output><empty_output><jupyter_text>Div At last, you have the `Div` element that behaves almost like `Html` with a few differences:* The style is put in the header, while `Html`'s style is embedded inline.* `Div` inherits from `MacroElement` so that:  * It cannot be rendered unless it's embedded in a `Figure`.  * It is a useful object toinherit from when you create new classes.<jupyter_code>div = Div()
div.html.add_child(Element('Hello world'))
print(Figure().add_child(div).render())<jupyter_output><!DOCTYPE html>
<head>    
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <style> #div_e72aae6984604f7093a46452870bbebd {
        position : relative;
        width : 100.0%;
        height: 100.0%;
        left: 0.0%;
        top: 0.0%;
    </style>
</head>
<body>    
    <div id="div_e72aae6984604f7093a46452870bbebd">Hello world</div>
</body>
<script>    
</script>
{'fraction_non_alphanumeric': 0.13371873821792132, 'fraction_numerical': 0.025009425662938293, 'mean_word_length': 5.76125743415463, 'pattern_counts': {'":': 0, '<': 154, '<?xml version=': 0, '>': 154, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '29656', 'license': 'MIT', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2642, 'n_words': 759, 'path': 'examples/Elements.ipynb', 'repo_name': 'raulsteurer/branca', 'size': '7957'}