-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdatabase_example.py
123 lines (93 loc) · 3.47 KB
/
database_example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
database_example.py
:copyright: (c) 2015-2017 by C. W. :license: New BSD
"""
from flask import Flask, request, jsonify, redirect, url_for
import flask_excel as excel
app = Flask(__name__)
excel.init_excel(app)
@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
return jsonify({"result": request.get_array('file')})
return '''
<!doctype html>
<title>Upload an excel file</title>
<h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
<form action="" method=post enctype=multipart/form-data><p>
<input type=file name=file><input type=submit value=Upload>
</form>
'''
@app.route("/download", methods=['GET'])
def download_file():
return excel.make_response_from_array([[1, 2], [3, 4]], "csv")
from flask_sqlalchemy import SQLAlchemy # noqa
from datetime import datetime # noqa
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tmp.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
category = db.relationship('Category',
backref=db.backref('posts',
lazy='dynamic'))
def __init__(self, title, body, category, pub_date=None):
self.title = title
self.body = body
if pub_date is None:
pub_date = datetime.utcnow()
self.pub_date = pub_date
self.category = category
def __repr__(self):
return '<Post %r>' % self.title
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
def __init__(self, name):
self.name = name
def __repr__(self):
return '<Category %r>' % self.name
db.create_all()
@app.route("/import", methods=['GET', 'POST'])
def doimport():
if request.method == 'POST':
def category_init_func(row):
c = Category(row['name'])
c.id = row['id']
return c
def post_init_func(row):
c = Category.query.filter_by(name=row['category']).first()
p = Post(row['title'], row['body'], c, row['pub_date'])
return p
request.save_book_to_database(
field_name='file', session=db.session,
tables=[Category, Post],
initializers=[category_init_func, post_init_func])
return redirect(url_for('.handson_table'), code=302)
return '''
<!doctype html>
<title>Upload an excel file</title>
<h1>Excel file upload (xls, xlsx, ods please)</h1>
<form action="" method=post enctype=multipart/form-data><p>
<input type=file name=file><input type=submit value=Upload>
</form>
'''
@app.route("/export", methods=['GET'])
def doexport():
return excel.make_response_from_tables(db.session, [Category, Post], "xls")
@app.route("/custom_export", methods=['GET'])
def docustomexport():
query_sets = Category.query.filter_by(id=1).all()
column_names = ['id', 'name']
return excel.make_response_from_query_sets(query_sets, column_names, "xls")
@app.route("/handson_view", methods=['GET'])
def handson_table():
return excel.make_response_from_tables(
db.session, [Category, Post], 'handsontable.html')
if __name__ == "__main__":
app.debug = True
app.run()