python: serialize a dictionary into a simple html output

前端 未结 9 781
轮回少年
轮回少年 2021-01-12 00:59

using app engine - yes i know all about django templates and other template engines.

Lets say i have a dictionary or a simple object, i dont know its structure and i

9条回答
  •  甜味超标
    2021-01-12 01:15

    The example made by pyfunc could easily be modified to generate simple nested html lists.

    z = {'data':{'id':1,'title':'home','address':{'street':'some road','city':'anycity','postal':'somepostal'}}}
    
    def printItems(dictObj, indent):
        print '  '*indent + '
      \n' for k,v in dictObj.iteritems(): if isinstance(v, dict): print ' '*indent , '
    • ', k, ':', '
    • ' printItems(v, indent+1) else: print ' '*indent , '
    • ', k, ':', v, '
    • ' print ' '*indent + '
    \n' printItems(z,0)

    Not terribly pretty of course, but somewhere to start maybe. If all you want to do is visualize data, the pprint module really is good enough. You could just use the "pre" tag on the result from pprint and put that on your web page.

    the pprint version would look something like this:

    import pprint
    z = {'data':{'id':1,'title':'home','address':{'street':'some road','city':'anycity','postal':'somepostal'}}}
    
    print '
    ', pprint.pformat(z), '
    '

    And the html output look something like this:

    {'data': {'address': {'city': 'anycity',
                          'postal': 'somepostal',
                          'street': 'some road'},
              'id': 1,
              'title': 'home'}}
    

    Which isn't that pretty, but it at least shows the data in a more structured way.

提交回复
热议问题