-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskr.py
executable file
·46 lines (38 loc) · 1 KB
/
flaskr.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
42
43
44
45
46
#!/usr/bin/env python2
from flask import Flask, g
from os import path
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from model import Base, Entries, Users
dbname = 'flaskr.db'
dbpath = path.join('/tmp',dbname)
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + dbpath
app = Flask(__name__)
app.config.from_object(__name__)
mydb = SQLAlchemy(app)
def connect_db():
engine = create_engine(SQLALCHEMY_DATABASE_URI)
return engine.connect()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.before_first_request
def setup():
Base.metadata.drop_all(bind=mydb.engine)
Base.metadata.create_all(bind=mydb.engine)
mydb.session.add(Entries('First entry', 'Lorem ipsum'))
mydb.session.add(Users('admin', 'admin'))
mydb.session.commit()
import view
'''
if __name__ == '__main__':
app.run()
'''