Implemented new database view and template

Closes #53 - see comments there for screenshots.
This commit is contained in:
Simon Willison 2017-11-11 17:50:21 -08:00
commit fd3a33989a
3 changed files with 42 additions and 17 deletions

View file

@ -243,27 +243,26 @@ class DatabaseView(BaseView):
template = 'database.html' template = 'database.html'
async def data(self, request, name, hash): async def data(self, request, name, hash):
sql = 'select * from sqlite_master' tables = []
custom_sql = False table_metadata = self.ds.metadata()[name]['tables']
params = {} for table_name, table_rows in table_metadata.items():
if request.args.get('sql'): rows = await self.execute(
params = request.raw_args name,
sql = params.pop('sql') 'PRAGMA table_info({});'.format(table_name)
validate_sql_select(sql) )
custom_sql = True tables.append({
rows = await self.execute(name, sql, params) 'name': table_name,
columns = [r[0] for r in rows.description] 'columns': [r[1] for r in rows],
'table_rows': table_rows,
})
tables.sort(key=lambda t: t['name'])
views = await self.execute(name, 'select name from sqlite_master where type = "view"')
return { return {
'database': name, 'database': name,
'rows': rows, 'tables': tables,
'columns': columns, 'views': [v[0] for v in views],
'query': {
'sql': sql,
'params': params,
}
}, { }, {
'database_hash': hash, 'database_hash': hash,
'custom_sql': custom_sql,
} }

View file

@ -64,3 +64,12 @@ th {
.hd :link { .hd :link {
text-decoration: none; text-decoration: none;
} }
.db-table p {
margin-top: 0;
margin-bottom: 0.3em;
}
.db-table h2 {
margin-top: 1em;
margin-bottom: 0;
}

View file

@ -13,6 +13,23 @@
<p><a href="/{{ database }}-{{ database_hash }}.db">download {{ database }}.db</a></p> <p><a href="/{{ database }}-{{ database_hash }}.db">download {{ database }}.db</a></p>
{% endif %} {% endif %}
{% for table in tables %}
<div class="db-table">
<h2><a href="/{{ database }}-{{ database_hash }}/{{ table.name }}">{{ table.name }}</a></h2>
<p><em>{% for column in table.columns[:9] %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}{% if table.columns|length > 9 %}...{% endif %}</em></p>
<p>{{ "{:,}".format(table.table_rows) }} row{% if table.table_rows == 1 %}{% else %}s{% endif %}</p>
</div>
{% endfor %}
{% if views %}
<h2>Views</h2>
<ul>
{% for view in views %}
<li><a href="/{{ database }}-{{ database_hash }}/{{ view }}">{{ view }}</a></li>
{% endfor %}
</ul>
{% endif %}
<table> <table>
<thead> <thead>
<tr> <tr>