Skip to content

Commit ed53d5e

Browse files
committed
rolling out new feature: make response from a query set using pyexcel v0.1.5
1 parent b2143ba commit ed53d5e

File tree

7 files changed

+67
-30
lines changed

7 files changed

+67
-30
lines changed

doc/source/index.rst

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,39 @@ Write up the view function for data export::
184184

185185

186186
Then run the example again. Visit http://localhost:5000/import and upload `sample-data.xls <https://github.com/chfw/Flask-Excel/blob/master/sample-data.xls>`_ . Then visit http://localhost:5000/export to download the data back.
187-
187+
188+
Export filtered query sets
189+
-----------------------------
190+
191+
Previous example shows you how to dump one or more tables over http protocol. Hereby, let's look at how to turn a query sets into an excel sheet. You can
192+
pass a query sets and an array of selected column names to :meth:`~flask_excel.make_response_from_query_sets` and generate an excel sheet from it::
193+
194+
@app.route("/custom_export", methods=['GET'])
195+
def docustomexport():
196+
query_sets = Category.query.filter_by(id=1).all()
197+
column_names = ['id', 'name']
198+
return excel.make_response_from_query_sets(query_sets, column_names, "xls")
199+
200+
Then visit http://localhost:5000/custom_export to download the data
201+
188202
All supported data types
189203
--------------------------
190204

191205
The example application likes to have array but it is not just about arrays. Here is table of functions for all supported data types:
192206

193-
=========================== ======================================================= =================================================
207+
=========================== ======================================================= ==================================================
194208
data structure from file to data structures from data structures to response
195-
=========================== ======================================================= =================================================
209+
=========================== ======================================================= ==================================================
196210
dict :meth:`~flask_excel.ExcelRequest.get_dict` :meth:`~flask_excel.make_response_from_dict`
197211
records :meth:`~flask_excel.ExcelRequest.get_records` :meth:`~flask_excel.make_response_from_records`
198212
a list of lists :meth:`~flask_excel.ExcelRequest.get_array` :meth:`~flask_excel.make_response_from_array`
199213
dict of a list of lists :meth:`~flask_excel.ExcelRequest.get_book_dict` :meth:`~flask_excel.make_response_from_book_dict`
200-
:class:`~pyexcel.Sheet` :meth:`~flask_excel.ExcelRequest.get_sheet` :meth:`~flask_excel.make_response`
201-
:class:`~pyexcel.Book` :meth:`~flask_excel.ExcelRequest.get_book` :meth:`~flask_excel.make_response`
214+
:class:`pyexcel.Sheet` :meth:`~flask_excel.ExcelRequest.get_sheet` :meth:`~flask_excel.make_response`
215+
:class:`pyexcel.Book` :meth:`~flask_excel.ExcelRequest.get_book` :meth:`~flask_excel.make_response`
202216
database table :meth:`~flask_excel.ExcelRequest.save_to_database` :meth:`~flask_excel.make_response_from_a_table`
203217
a list of database tables :meth:`~flask_excel.ExcelRequest.save_book_to_database` :meth:`~flask_excel.make_response_from_tables`
204-
=========================== ======================================================= =================================================
218+
a database query sets :meth:`~flask_excel.make_response_from_query_sets`
219+
=========================== ======================================================= ==================================================
205220

206221
See more examples of the data structures in :ref:`pyexcel documentation<pyexcel:a-list-of-data-structures>`
207222

@@ -220,7 +235,7 @@ API Reference
220235
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
221236
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
222237
*sheet_name* should be None anyway.
223-
:param keywords: additional keywords to pyexcel library
238+
:param keywords: additional keywords to :meth:`pyexcel.get_sheet`
224239
:returns: A sheet object
225240

226241
The following html form, the *field_name* should be "file"::
@@ -267,36 +282,31 @@ API Reference
267282
:param keywords: additional keywords to pyexcel library
268283
:returns: a two dimensional array, a list of lists
269284

270-
.. method:: save_to_database(field_name=None, session=None, table=None, **keywords)
285+
.. method:: save_to_database(field_name=None, session=None, table=None, initializer=None, mapdict=None **keywords)
271286

272287
:param field_name: same as :meth:`~flask_excel.ExcelRequest.get_sheet`
273288
:param session: a SQLAlchemy session
274-
:param table: a database table or a tuple which have this sequence (table, table_init_func, mapdict, name_columns_by_row, name_rows_by_column)
275-
:param table_init_func: it is needed when your table had custom __init__ function
276-
:param mapdict: it is needed when the uploaded sheet had a different column headers than the table column names this mapdict tells which column of the upload sheet maps to which column of the table
277-
:param name_columns_by_row: uses the first row of the sheet to be column headers by default. if you use name_rows_by_column, please set this to -1
278-
:param name_rows_by_column: uses a column to name rows.
279-
:param keywords: additional keywords to pyexcel library
280-
289+
:param table: a database table
290+
:param initializer: a custom table initialization function if you have one
291+
:param mapdict: the explicit table column names if your excel data do not have the exact column names
292+
:param keywords: additional keywords to :meth:`pyexcel.Sheet.save_to_database`
281293

282294
.. method:: save_book_to_database(field_name=None, session=None, tables=None, **keywords)
283295

284296
:param field_name: save as :meth:`~flask_excel.ExcelRequest.get_sheet`
285-
:param session: a SQLAlchemy session
286-
:param tables: a list of database tables or tuples which have this sequence (table, table_init_func, mapdict, name_columns_by_row, name_rows_by_column)
287-
:param table_init_funcs: it is needed when your table had custom __init__ function
288-
:param mapdict: it is needed when the uploaded sheet had a different column headers than the table column names. this mapdict tells which column of the upload sheet maps to which column of the table
289-
:param name_columns_by_row: uses the first row of each sheet to be column headers by default. if you use name_rows_by_column, please set this to -1
290-
:param name_rows_by_column: uses a column to name rows.
291-
:param keywords: additional keywords to pyexcel library
297+
:param session: a SQLAlchemy sessio
298+
:param tables: a list of database tables
299+
:param initializers: a list of model initialization functions.
300+
:param mapdicts: a list of explicit table column names if your excel data sheets do not have the exact column names
301+
:param keywords: additional keywords to :meth:`pyexcel.Book.save_to_database`
292302

293303

294304
Response methods
295305
-----------------
296306

297307
.. method:: make_response(pyexcel_instance, file_type, status=200)
298308

299-
:param pyexcel_instance: pyexcel.Sheet or pyexcel.Book
309+
:param pyexcel_instance: :class:`pyexcel.Sheet` or :class:`pyexcel.Book`
300310
:param file_type: one of the following strings:
301311

302312
* 'csv'
@@ -344,6 +354,15 @@ Response methods
344354
:param file_type: same as :meth:`~flask_excel.make_response`
345355
:param status: same as :meth:`~flask_excel.make_response`
346356

357+
.. method:: make_response_from_query_sets(query_sets, column_names, file_type status=200)
358+
359+
Produce a single sheet Excel book of *file_type* from your custom database queries
360+
361+
:param query_sets: a query set
362+
:param column_names: a nominated column names. It could not be None, otherwise no data is returned.
363+
:param file_type: same as :meth:`~flask_excel.make_response`
364+
:param status: same as :meth:`~flask_excel.make_response`
365+
347366
.. method:: make_response_from_tables(session, tables, file_type status=200)
348367

349368
Produce a multiple sheet Excel book of *file_type*. It becomes the same

examples/database_example.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
database_example.py
33
:copyright: (c) 2015 by C. W.
4-
:license: GPL v3 or BSD
4+
:license: New BSD
55
"""
66
from flask import Flask, request, jsonify
77
from flask.ext import excel
@@ -93,6 +93,14 @@ def post_init_func(row):
9393
def doexport():
9494
return excel.make_response_from_tables(db.session, [Category, Post], "xls")
9595

96+
97+
@app.route("/custom_export", methods=['GET'])
98+
def docustomexport():
99+
query_sets = Category.query.filter_by(id=1).all()
100+
column_names = ['id', 'name']
101+
return excel.make_response_from_query_sets(query_sets, column_names, "xls")
102+
103+
96104
if __name__ == "__main__":
97105
app.debug=True
98106
app.run()

examples/database_example_formatted.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
database_example_formatted.py
33
:copyright: (c) 2015 by C. W.
4-
:license: GPL v3 or BSD
4+
:license: New BSD
55
"""
66
from flask import Flask, request, jsonify
77
from flask.ext import excel
@@ -97,6 +97,13 @@ def post_init_func(row):
9797
def doexport():
9898
return excel.make_response_from_tables(db.session, [Category, Post], "xls")
9999

100+
101+
@app.route("/custom_export", methods=['GET'])
102+
def docustomexport():
103+
query_sets = Category.query.filter_by(id=1).all()
104+
column_names = ['id', 'name']
105+
return excel.make_response_from_query_sets(query_sets, column_names, "xls")
106+
100107

101108
if __name__ == "__main__":
102109
app.run()

flask_excel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def load_book(self, field_name=None, **keywords):
4343
make_response_from_records,
4444
make_response_from_book_dict,
4545
make_response_from_a_table,
46+
make_response_from_query_sets,
4647
make_response_from_tables
4748
)
4849

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
README_txt = readme.read()
1010

1111
dependencies = [
12-
'pyexcel>=0.1.2',
13-
'pyexcel-webio>=0.0.1',
12+
'pyexcel>=0.1.5',
13+
'pyexcel-webio>=0.0.2',
1414
'Flask>=0.10.1'
1515
]
1616

tests/test_database_io.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_single_sheet_file(self):
2626
data={"file": (io, file_name)},
2727
content_type="multipart/form-data")
2828
ret = pe.get_array(file_type="xls", content=response.data)
29+
print(ret)
2930
assert array == ret
3031

3132

@@ -59,6 +60,7 @@ def test_book_file(self):
5960
data={"file": (io, file_name)},
6061
content_type="multipart/form-data")
6162
ret = pe.get_book_dict(file_type="xls", content=response.data)
63+
print(ret)
6264
assert data['Category'] == ret['category']
6365
sheet = pe.Sheet(data['Post'], name_columns_by_row=0)
6466
sheet.column.format("pub_date", None, lambda d: d.isoformat())

tests/testapp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def upload_categories():
101101
def table_init_func(row):
102102
return Category(row['name'])
103103
request.save_to_database(field_name='file', session=db.session,
104-
table=(Category, table_init_func))
104+
table=Category, initializer=table_init_func)
105105
return excel.make_response_from_a_table(db.session, Category, "xls")
106106

107107
@app.route("/upload/all", methods=['POST'])
@@ -116,6 +116,6 @@ def post_init_func(row):
116116
p = Post(row['title'], row['body'], c, row['pub_date'])
117117
return p
118118
request.save_book_to_database(field_name='file', session=db.session,
119-
tables=[(Category, category_init_func),
120-
(Post, post_init_func)])
119+
tables=[Category, Post],
120+
initializers=[category_init_func, post_init_func])
121121
return excel.make_response_from_tables(db.session, [Category, Post], "xls")

0 commit comments

Comments
 (0)