mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
fa42a56c6a
commit
2366a016f2
5 changed files with 32 additions and 3 deletions
|
|
@ -20,6 +20,7 @@ from .utils import (
|
||||||
InvalidSql,
|
InvalidSql,
|
||||||
path_from_row_pks,
|
path_from_row_pks,
|
||||||
path_with_added_args,
|
path_with_added_args,
|
||||||
|
path_with_ext,
|
||||||
compound_pks_from_path,
|
compound_pks_from_path,
|
||||||
sqlite_timelimit,
|
sqlite_timelimit,
|
||||||
validate_sql_select,
|
validate_sql_select,
|
||||||
|
|
@ -174,7 +175,10 @@ class BaseView(HTTPMethodView):
|
||||||
extra_template_data()
|
extra_template_data()
|
||||||
if callable(extra_template_data)
|
if callable(extra_template_data)
|
||||||
else extra_template_data
|
else extra_template_data
|
||||||
)}
|
), **{
|
||||||
|
'url_json': path_with_ext(request, '.json'),
|
||||||
|
'url_jsono': path_with_ext(request, '.jsono'),
|
||||||
|
}}
|
||||||
r = self.jinja.render(
|
r = self.jinja.render(
|
||||||
self.template,
|
self.template,
|
||||||
request,
|
request,
|
||||||
|
|
@ -240,19 +244,26 @@ class DatabaseView(BaseView):
|
||||||
|
|
||||||
async def data(self, request, name, hash):
|
async def data(self, request, name, hash):
|
||||||
sql = 'select * from sqlite_master'
|
sql = 'select * from sqlite_master'
|
||||||
|
custom_sql = False
|
||||||
params = {}
|
params = {}
|
||||||
if request.args.get('sql'):
|
if request.args.get('sql'):
|
||||||
params = request.raw_args
|
params = request.raw_args
|
||||||
sql = params.pop('sql')
|
sql = params.pop('sql')
|
||||||
validate_sql_select(sql)
|
validate_sql_select(sql)
|
||||||
|
custom_sql = True
|
||||||
rows = await self.execute(name, sql, params)
|
rows = await self.execute(name, sql, params)
|
||||||
columns = [r[0] for r in rows.description]
|
columns = [r[0] for r in rows.description]
|
||||||
return {
|
return {
|
||||||
'database': name,
|
'database': name,
|
||||||
'rows': rows,
|
'rows': rows,
|
||||||
'columns': columns,
|
'columns': columns,
|
||||||
|
'query': {
|
||||||
|
'sql': sql,
|
||||||
|
'params': params,
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
'database_hash': hash,
|
'database_hash': hash,
|
||||||
|
'custom_sql': custom_sql,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,11 @@
|
||||||
|
|
||||||
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ database }}</h1>
|
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ database }}</h1>
|
||||||
|
|
||||||
<p><a href="/{{ database }}-{{ database_hash }}.db">download {{ database }}.db</a></p>
|
{% if custom_sql %}
|
||||||
|
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
||||||
|
{% else %}
|
||||||
|
<p><a href="/{{ database }}-{{ database_hash }}.db">download {{ database }}.db</a></p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ table }}: {{ ', '.join(primary_key_values) }}</a></h1>
|
<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ table }}: {{ ', '.join(primary_key_values) }}</a></h1>
|
||||||
|
|
||||||
|
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@
|
||||||
<h2>{{ "{:,}".format(table_rows) }} total row{% if table_rows == 1 %}{% else %}s{% endif %} in this table</h2>
|
<h2>{{ "{:,}".format(table_rows) }} total row{% if table_rows == 1 %}{% else %}s{% endif %} in this table</h2>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -42,8 +44,10 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|
||||||
{% if after_link %}
|
{% if after_link %}
|
||||||
<p><a href="{{ after_link }}">Next page</a></p>
|
<p><a href="{{ after_link }}">Next page</a></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,14 @@ def path_with_added_args(request, args):
|
||||||
return request.path + '?' + urllib.parse.urlencode(current)
|
return request.path + '?' + urllib.parse.urlencode(current)
|
||||||
|
|
||||||
|
|
||||||
|
def path_with_ext(request, ext):
|
||||||
|
path = request.path
|
||||||
|
path += ext
|
||||||
|
if request.query_string:
|
||||||
|
path += '?' + request.query_string
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
_css_re = re.compile(r'''['"\n\\]''')
|
_css_re = re.compile(r'''['"\n\\]''')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue