Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating HTML documents in python

Tags:

python

html

In python, what is the most elegant way to generate HTML documents. I currently manually append all of the tags to a giant string, and write that to a file. Is there a more elegant way of doing this?

like image 510
shoes Avatar asked Jul 19 '11 14:07

shoes


2 Answers

You can use yattag to do this in an elegant way. FYI I'm the author of the library.

from yattag import Doc  doc, tag, text = Doc().tagtext()  with tag('html'):     with tag('body'):         with tag('p', id = 'main'):             text('some text')         with tag('a', href='/my-url'):             text('some link')  result = doc.getvalue() 

It reads like html, with the added benefit that you don't have to close tags.

like image 122
John Smith Optional Avatar answered Sep 23 '22 02:09

John Smith Optional


I would suggest using one of the many template languages available for python, for example the one built into Django (you don't have to use the rest of Django to use its templating engine) - a google query should give you plenty of other alternative template implementations.

I find that learning a template library helps in so many ways - whenever you need to generate an e-mail, HTML page, text file or similar, you just write a template, load it with your template library, then let the template code create the finished product.

Here's some simple code to get you started:

#!/usr/bin/env python  from django.template import Template, Context from django.conf import settings settings.configure() # We have to do this to use django templates standalone - see # http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django  # Our template. Could just as easily be stored in a separate file template = """ <html> <head> <title>Template {{ title }}</title> </head> <body> Body with {{ mystring }}. </body> </html> """  t = Template(template) c = Context({"title": "title from code",              "mystring":"string from code"}) print t.render(c) 

It's even simpler if you have templates on disk - check out the render_to_string function for django 1.7 that can load templates from disk from a predefined list of search paths, fill with data from a dictory and render to a string - all in one function call. (removed from django 1.8 on, see Engine.from_string for comparable action)

like image 23
Erik Forsberg Avatar answered Sep 22 '22 02:09

Erik Forsberg