Created
November 27, 2013 02:36
-
-
Save merriam/7669828 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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