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?