5

I have dictionary and would like to produce html page where will be drawn simple html table with keys and values. How it can be done from python code?

3
  • 2
    Umm... print HTML in your Python code, maybe? Commented Dec 23, 2009 at 16:07
  • 1
    You'll need to provide more information. How are you outputting this HTML; are you using a template library like Genshi? How are you producing a table from a dictionary? Is each item in the dict a list of values for that column? Commented Dec 23, 2009 at 16:08
  • I don't use any template libraries and if you can advice something to have output then it would be good. Html table is simple. Two columns where first are the keys and second their corresponding values. Commented Dec 23, 2009 at 16:11

5 Answers 5

10
output = "<html><body><table>"
for key in your_dict:
  output += "<tr><td>%s</td><td>%s</td></tr>" % (key, your_dict[key])
output += "</table></body></html>
print output
Sign up to request clarification or add additional context in comments.

4 Comments

maybe you meant 'for key, value in your_dict:' ;-)
Remember to HTML-escape any text you concatenate into HTML, or you'll have bugs and cross-site-scripting security holes.
Fixed the value issue, thanks jbochi. Regarding HTML escaping I could have added cgi.escape(...) but I'll leave it simple.
Thank you rui. Formatting html strings is enough for simple html table.
2

You can use a template engine like Jinja. A list of engines for templating is available here.

Comments

1

You maybe interested by markup see http://markup.sourceforge.net/

Comments

1

You may also consider using Mako template library.

Comments

0

Along with the numerous template engines, you might consider using Python's built in Template. It will give you basic templating functionality without needing to learn (or choose) a template library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.