3

I am new to Python, but I want to get fast in understanding it. Currently I am working on Flask+SQLAlchemy+PostgreSQL+jQuery in Openshift platform. And I finding it hard to try using this jQuery plugins datatables.net here :

http://datatables.net/release-datatables/examples/server_side/server_side.html

The server side example is in PHP, I can understand it well, but I am having problem in translating it to Python. My understanding on Python Dict (as I understand that is is comparable to JSON) data structure is not good at the moment.

I am working now to translating it, but if you can show me the correct python code to do that, I will be delighted

EDIT 1 After reading and playing around with Python docs on dict, I can build the exact JSON data by using regular python dict + json.dumps() :

@app.route ('/dataset/users')
    def dataset_users():                                                                                

    data = {}                                                                                            
    data['aaData']=[]                                                                                    
    data['iTotalRecords']=3                                                                              
    data['sEcho']=1                                                                                      
    data['iTotalDisplayRecords']=1                                                                       

    data['aaData'].append(['Gecko', 'FFox', 'Win', '1.1','A'])                                           
    data['aaData'].append(['Webkit', 'Safari', 'OSX', '3','B'])                                          
    data['aaData'].append(['IE', 'IE', 'Win', '2','C'])                                                  
    return json.dumps(data)   

Working now on using SQLAlchemy + Flask to jsonify the resultset

EDIT 2 These are my current flask+sqlalchemy code that conform to jquery datatables plugin :

@app.route ('/dataset/users')
def dataset_users():                                                                                     
    data = {}                                                                                            
    data['iTotalRecords'] = 2                                                                            
    data['sEcho'] = 1                                                                                    
    data['iTotalDisplayRecords'] =  2                                                                    

    aaData = []                                                                                          
    users=models.Users.query.with_entities(                                                              
        models.Users.id, models.Users.username,                                                          
        models.Users.email).order_by(models.Users.username).all()                                        
    for user in users:                                                                                   
        aaData.append([user.id, user.username, user.email, 'Modify'])                                                       

    data['aaData']=aaData                                                                                
    return json.dumps(data)                          

I believe that we can streamline the code so that it will not use for loop. Any idea?

swdev
  • 4,997
  • 8
  • 64
  • 106
  • I don't see anything in this question that involves twitter-bootstrap. – Paul Tomblin Sep 28 '13 at 13:12
  • Pardon me as I am a complete new in Bootstrap + Python, but isn't that datatables.net is a bootstrap .. wait a minute. it's a jquery plugin. Thanks for noticing. I will rephrase my question – swdev Sep 28 '13 at 13:16

1 Answers1

4

You can convert a Python dict to JSON using the json module:

import json
print json.dumps({"key": "value", "numbers": [1, 2, 3],})
# {"numbers": [1, 2, 3], "key": "value"}
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Thanks! I already know that function, but .. how to construct that specified dict, is beyond my current understanding. This is the url that the given example use to send the correct JSON data : http://datatables.net/release-datatables/examples/server_side/scripts/server_processing.php I am trying to mimic that into python dict.. or, wait a minute, maybe I can save that into a file, and find python function that will directly convert it into dict... And after that I can construct python code on how to create that python dict using my data – swdev Sep 28 '13 at 13:56
  • OKay, got it. http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python I already had that json as python dict. working now on how to construct that dict in my program. Thanks @Thomas for your concise answer. It lead me to clear path ;) – swdev Sep 28 '13 at 14:03