-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathsimple_sqlalchemy.py
More file actions
50 lines (36 loc) · 1.3 KB
/
simple_sqlalchemy.py
File metadata and controls
50 lines (36 loc) · 1.3 KB
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
42
43
44
45
46
47
48
49
50
from __future__ import print_function
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_table import Table, Col
# Some application and database setup. This should be taken care of
# elsewhere in an application and is not specific to the tables. See
# Flask-SQLAlchemy docs for more about what's going on for this first
# bit.
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
# To suppress a warning. Not important.
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# An example model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '<User %r>' % self.username
# Create the database, and put some records in it.
db.create_all()
user1 = User('user1', 'test1@example.com')
user2 = User('user2', 'test2@example.com')
db.session.add(user1)
db.session.add(user2)
db.session.commit()
# Define a table, then pass in the database records
class UserTable(Table):
username = Col('Username')
email = Col('Email')
users = User.query.all()
print(UserTable(items=users).__html__())