1

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: 'jdoe@gmail.com', 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

susdu
  • 852
  • 9
  • 22

2 Answers2

2

You can simply dump your dict into JSON-like string:

import json

# You may need some converting: dict = convert_to_jq_format(dict) 
result = json.dumps(dict)

And pass result to Jinja's template.

Of course, if your dict doesn't match plugin's format, you should convert it first.

Egor Biriukov
  • 649
  • 1
  • 7
  • 16
2

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
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • setting the w2utils datatype should be the first thing you do. You can also inspect the request in your browser's network tab, to see what request data and request header you're sending and receiving. – Mike Scotty Dec 19 '16 at 08:48
  • Got it working. I had to add more status field to my dict response, thanks. – susdu Dec 19 '16 at 09:00