{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Use Python lxml library to create HTML elements.\n", "\n", "## Resources\n", "\n", "* [lxml documentation](https://lxml.de/api.html)\n", "* [Chameleon Language Reference](https://chameleon.readthedocs.io/en/latest/reference.html)"]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": ["from lxml import etree"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"data": {"text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["root = etree.Element(\"root\")\n", "root"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"data": {"text/plain": ["[]"]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["list(root.iter())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Output from `etree.SubElement??`\n", "\n", "```python\n", "Signature: etree.SubElement(_parent, _tag, attrib=None, nsmap=None, **_extra)\n", "Call signature: etree.SubElement(*args, **kwargs)\n", "Type: cython_function_or_method\n", "String form: \n", "Docstring: \n", "SubElement(_parent, _tag, attrib=None, nsmap=None, **_extra)\n", "\n", "Subelement factory. This function creates an element instance, and\n", "appends it to an existing element.\n", "\n", "```"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Get the attributes from Bootstrap starter template header element.\n", "\n", "Try to recreate this with lxml."]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": ["head_html = \"\"\" \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " ${title}\n", " \n", "\"\"\""]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"data": {"text/plain": ["[('meta', {'charset': 'utf-8'}),\n", " ('meta',\n", " {'name': 'viewport', 'content': 'width=device-width, initial-scale=1, shrink-to-fit=no'}),\n", " ('link',\n", " {'rel': 'stylesheet', 'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', 'integrity': 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T', 'crossorigin': 'anonymous'}),\n", " ('script',\n", " {'defer': 'defer', 'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js', 'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo', 'crossorigin': 'anonymous'}),\n", " ('script',\n", " {'defer': 'defer', 'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js', 'integrity': 'sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1', 'crossorigin': 'anonymous'}),\n", " ('script',\n", " {'defer': 'defer', 'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js', 'integrity': 'sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM', 'crossorigin': 'anonymous'}),\n", " ('title', {})]"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["tree = etree.fromstring(head_html, etree.HTMLParser())\n", "inner_element_collection = [\n", " (e.tag, e.attrib) for e in tree.iterdescendants() if isinstance(e.tag, str)\n", "][1:]\n", "inner_element_collection"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": ["for tag, attrib in inner_element_collection:\n", " etree.SubElement(root, tag, attrib=attrib)"]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [{"data": {"text/plain": ["[,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ]"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["header_elements = list(root.iterdescendants())\n", "header_elements"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": [""]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["sample, *_ = header_elements\n", "sample"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"data": {"text/plain": ["['fromstring', 'fromstringlist', 'tostring', 'tostringlist']"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["[item for item in dir(etree) if not item.startswith(\"_\") and \"string\" in item]"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"data": {"text/plain": ["[b'',\n", " b'',\n", " b'',\n", " b'