Rendering HTML - garyhurtz/html2json.py GitHub Wiki
After HTML is parsed into JSON, it can be rendered back to HTML using the Jinja/nunjucks macro contained in the json2html.html template. Simply import this macro, pass the JSON into the context, and watch the HTML magically appear.
You can learn pretty much everything about Jinja and nunjucks on their websites, but in short you set up an environment, tell it which template you want to use, then render the HTML.
Build the HTML:
dut = Element(u'ul', u'some text', {u'class': u'some class'})
dut.child.append(Element(u'li', u'some text', {u'class': u'some class'}))
dut.child.append(Element(u'li', u'some text', {u'class': u'some class'}))
Render the JSON:
json = dut.render()
Then reconstruct the HTML. With Jinja it looks something like this, and nunjucks is very similar:
env = Environment(loader=FileSystemLoader(u'template_path'))
result = env.get_template(u'json2html.html').render(root=json)
Which provides the following HTML:
<ul class="some class">some text<li class="some class">some text</li><li class="some class">some text</li></ul>
Alternatively, json2html.js supports pure javascript rendering of HTML from the JSON:
html = json2html(json)