mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
First working version of .csv?_stream=1, refs #266
This commit is contained in:
parent
654fde5792
commit
ced379ea32
2 changed files with 47 additions and 9 deletions
|
|
@ -199,7 +199,40 @@ class BaseView(RenderMixin):
|
||||||
|
|
||||||
return await self.view_get(request, name, hash, **kwargs)
|
return await self.view_get(request, name, hash, **kwargs)
|
||||||
|
|
||||||
|
async def as_csv_stream(self, request, name, hash, **kwargs):
|
||||||
|
assert not request.args.get("_next") # TODO: real error
|
||||||
|
kwargs['_size'] = 'max'
|
||||||
|
|
||||||
|
async def stream_fn(r):
|
||||||
|
first = True
|
||||||
|
next = None
|
||||||
|
writer = csv.writer(r)
|
||||||
|
while first or next:
|
||||||
|
if next:
|
||||||
|
kwargs['_next'] = next
|
||||||
|
data, extra_template_data, templates = await self.data(
|
||||||
|
request, name, hash, **kwargs
|
||||||
|
)
|
||||||
|
if first:
|
||||||
|
writer.writerow(data["columns"])
|
||||||
|
first = False
|
||||||
|
next = data["next"]
|
||||||
|
for row in data["rows"]:
|
||||||
|
writer.writerow(row)
|
||||||
|
|
||||||
|
return response.stream(
|
||||||
|
stream_fn,
|
||||||
|
# headers={
|
||||||
|
# "Content-Disposition": 'attachment; filename="{}.csv"'.format(
|
||||||
|
# name
|
||||||
|
# )
|
||||||
|
# },
|
||||||
|
content_type="text/plain; charset=utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
async def as_csv(self, request, name, hash, **kwargs):
|
async def as_csv(self, request, name, hash, **kwargs):
|
||||||
|
if request.args.get("_stream"):
|
||||||
|
return await self.as_csv_stream(request, name, hash, **kwargs)
|
||||||
try:
|
try:
|
||||||
response_or_template_contexts = await self.data(
|
response_or_template_contexts = await self.data(
|
||||||
request, name, hash, **kwargs
|
request, name, hash, **kwargs
|
||||||
|
|
@ -226,12 +259,13 @@ class BaseView(RenderMixin):
|
||||||
|
|
||||||
return response.stream(
|
return response.stream(
|
||||||
stream_fn,
|
stream_fn,
|
||||||
headers={
|
# headers={
|
||||||
"Content-Disposition": 'attachment; filename="{}.csv"'.format(
|
# "Content-Disposition": 'attachment; filename="{}.csv"'.format(
|
||||||
name
|
# name
|
||||||
)
|
# )
|
||||||
},
|
# },
|
||||||
content_type="text/csv; charset=utf-8"
|
content_type="text/plain; charset=utf-8"
|
||||||
|
#content_type="text/csv; charset=utf-8"
|
||||||
)
|
)
|
||||||
|
|
||||||
async def view_get(self, request, name, hash, **kwargs):
|
async def view_get(self, request, name, hash, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -232,7 +232,7 @@ class RowTableShared(BaseView):
|
||||||
|
|
||||||
class TableView(RowTableShared):
|
class TableView(RowTableShared):
|
||||||
|
|
||||||
async def data(self, request, name, hash, table):
|
async def data(self, request, name, hash, table, _next=None, _size=None):
|
||||||
table = urllib.parse.unquote_plus(table)
|
table = urllib.parse.unquote_plus(table)
|
||||||
canned_query = self.ds.get_canned_query(name, table)
|
canned_query = self.ds.get_canned_query(name, table)
|
||||||
if canned_query is not None:
|
if canned_query is not None:
|
||||||
|
|
@ -411,7 +411,7 @@ class TableView(RowTableShared):
|
||||||
)
|
)
|
||||||
count_sql = "select count(*) {}".format(from_sql)
|
count_sql = "select count(*) {}".format(from_sql)
|
||||||
|
|
||||||
_next = special_args.get("_next")
|
_next = _next or special_args.get("_next")
|
||||||
offset = ""
|
offset = ""
|
||||||
if _next:
|
if _next:
|
||||||
if is_view:
|
if is_view:
|
||||||
|
|
@ -498,7 +498,7 @@ class TableView(RowTableShared):
|
||||||
|
|
||||||
extra_args = {}
|
extra_args = {}
|
||||||
# Handle ?_page_size=500
|
# Handle ?_page_size=500
|
||||||
page_size = request.raw_args.get("_size")
|
page_size = _size or request.raw_args.get("_size")
|
||||||
if page_size:
|
if page_size:
|
||||||
if page_size == "max":
|
if page_size == "max":
|
||||||
page_size = self.max_returned_rows
|
page_size = self.max_returned_rows
|
||||||
|
|
@ -545,6 +545,8 @@ class TableView(RowTableShared):
|
||||||
pass
|
pass
|
||||||
facet_results = {}
|
facet_results = {}
|
||||||
for column in facets:
|
for column in facets:
|
||||||
|
if _next:
|
||||||
|
continue
|
||||||
facet_sql = """
|
facet_sql = """
|
||||||
select {col} as value, count(*) as count
|
select {col} as value, count(*) as count
|
||||||
{from_sql} {and_or_where} {col} is not null
|
{from_sql} {and_or_where} {col} is not null
|
||||||
|
|
@ -649,6 +651,8 @@ class TableView(RowTableShared):
|
||||||
for facet_column in columns:
|
for facet_column in columns:
|
||||||
if facet_column in facets:
|
if facet_column in facets:
|
||||||
continue
|
continue
|
||||||
|
if _next:
|
||||||
|
continue
|
||||||
suggested_facet_sql = '''
|
suggested_facet_sql = '''
|
||||||
select distinct {column} {from_sql}
|
select distinct {column} {from_sql}
|
||||||
{and_or_where} {column} is not null
|
{and_or_where} {column} is not null
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue