Search code examples
jquerypythonjinja2w2ui

python: turn nested dict into JSON-like format


I have a nested dict in the form of:

dict = {
"BLOCK_NAME": {
    "SUB_BLOCK_NAME1": {
        "ENTRY_NUMBER1": {
            "FIELD_NAME" : "VALUE"
            "FIELD_NAME2" : "VALUE2"
            "FIELD_NAME3" : "VALUE3"
        }
    }
}

}

I want to display it in an HTML page using a jquery tree table plugin (w2ui). The plugin initializes from data in the following format:

{ recid: 1, key_1: 'John', key_2: 'doe', w2ui: { children: [] }},
        { recid: 2, key_1: 'Stuart', key_2: 'Motzart', 
            w2ui: {
                children: [
                    { recid: 21, key_1: 'Stuart', key_2: 'Motzart',w2ui: { children: [] } },
                    { recid: 22, key_1: 'Jin', key_2: 'Franson',
                        w2ui: {
                            children: [

and so on... I'm using Jinja2 as a template engine and I'm thinking what's the best way to accomplish this task.

The options I can think of are:

  • Write a python function that transforms dict into a long string that matches that plugin's format and pass it to Jinja.

  • Put all the logic inside the template while I iterate over it and create the JS formatting.

  • Save the dict as JSON and process it in JS (less preferable, my JS is weak)

What do you think?

EDIT: following @mpf82 answer, I've tried the following:

HTML:

<script type="text/javascript">
$(function () {
    $('#grid').w2grid({ 
        name: 'grid', 
        url  : 'get_json',
        show: { 
            toolbar: true,
        },
        multiSearch: false,
        searches: [
            { field: 'lname', caption: 'Last Name', type: 'text' },
            { field: 'fname', caption: 'First Name', type: 'text' },
            { field: 'email', caption: 'Email', type: 'text' },
            { field: 'sdate', caption: 'Start Date', type: 'date' }
        ],
        columns: [                
            { field: 'lname', caption: 'Last Name', size: '30%' },
            { field: 'fname', caption: 'First Name', size: '30%' },
            { field: 'email', caption: 'Email', size: '40%' },
            { field: 'sdate', caption: 'Start Date', size: '90px' }
        ]

    });
    w2utils.settings['dataType'] = 'JSON'  
});
</script>

Cherrypy:

    @cherrypy.expose
    @cherrypy.tools.json_in()
    @cherrypy.tools.json_out()
    def get_json(self):
        try:
        # optionally get the w2ui request
            requested_data = cherrypy.request.json
        except:
            pass
        # build your w2ui data dict
        my_data = { recid: 1, fname: 'John', lname: 'doe', email: '[email protected]', sdate: '4/3/2012', w2ui: { children: [] }}
        # return dict, no further conversion neccessary
        return my_data

I get error 415 from Cherrypy: unsupported media type Expected an entity of content type application/json, text/javascript


Solution

  • No need to pass your data through jinja or create a long string.

    Instead, use the w2ui grid's url property, set w2utils to use JSON (w2utils.settings.dataType = 'JSON';) and if you're using cherrypy, all you need to do is use the JSON decorators on your URL:

    @cherrypy.expose
    @cherrypy.tools.json_in()
    @cherrypy.tools.json_out()
    def my_url():
        try:
            # optionally get the w2ui request
            requested_data = cherrypy.request.json
        except:
            pass
        # build your w2ui data dict
        my_data = { recid: 1, key_1: 'John', key_2: 'doe', w2ui: { children: [] }}
        # return dict, no further conversion neccessary
        return my_data