Skip to content

Instantly share code, notes, and snippets.

@merriam merriam/gist:7669828
Created Nov 27, 2013

Embed
What would you like to do?
A small routine to recursively dump structure into HTML. This routine uses the Flask framework, but should work in anything. Added a couple comments to help.
from flask import g
from werkzeug import escape # so "<sqlInstance ... >" isn't taken as a tag.
@app.route("/show_user_data")
def show_user_data():
""" show the user date, i.e., the flask.g data """
## The dump_val() function is what you might want to reuse.
def dump_val(key, value):
""" return an html snippet with lists for dictionaries. . """
if isinstance(value, dict):
html = ["{!s} = <i>{!s}</i><ul>".format(key, value.__class__.__name__)]
for k, v in value.items():
name = str(key) + "." + str(k)
html.append("<li>{}</li>".format(dump_val(name, v)))
html.append("</ul>")
return "\n".join(html)
else:
return "{!s} = {}<br>\n".format(key, escape(str(value)))
assert DEBUG # callable only in development ## Flask variable
grab_user() ## Something I use to grab the user from the db.
out_html = "\n".join(("<body>",
dump_val("g", g.__dict__), ## Flask g is not iterable by itself.
"</body>"))
return make_response(out_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.