forked from miguelgrinberg/flask-tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_table.py
41 lines (30 loc) · 989 Bytes
/
ajax_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), index=True)
age = db.Column(db.Integer, index=True)
address = db.Column(db.String(256))
phone = db.Column(db.String(20))
email = db.Column(db.String(120))
def to_dict(self):
return {
'name': self.name,
'age': self.age,
'address': self.address,
'phone': self.phone,
'email': self.email
}
db.create_all()
@app.route('/')
def index():
return render_template('ajax_table.html', title='Ajax Table')
@app.route('/api/data')
def data():
return {'data': [user.to_dict() for user in User.query]}
if __name__ == '__main__':
app.run()