Hide FTS-created tables on index pages

Closes #129
This commit is contained in:
Simon Willison 2017-11-22 12:12:15 -08:00
commit 1c8638c30a
No known key found for this signature in database
GPG key ID: FBB38AFE227189DB
4 changed files with 43 additions and 24 deletions

View file

@ -267,18 +267,23 @@ class IndexView(HTTPMethodView):
async def get(self, request, as_json): async def get(self, request, as_json):
databases = [] databases = []
for key, info in sorted(self.ds.inspect().items()): for key, info in sorted(self.ds.inspect().items()):
tables = [t for t in info['tables'].values() if not t['hidden']]
hidden_tables = [t for t in info['tables'].values() if t['hidden']]
database = { database = {
'name': key, 'name': key,
'hash': info['hash'], 'hash': info['hash'],
'path': '{}-{}'.format(key, info['hash'][:7]), 'path': '{}-{}'.format(key, info['hash'][:7]),
'tables_truncated': sorted( 'tables_truncated': sorted(
info['tables'].items(), tables,
key=lambda p: p[1]['count'], key=lambda t: t['count'],
reverse=True reverse=True
)[:5], )[:5],
'tables_count': len(info['tables'].items()), 'tables_count': len(tables),
'tables_more': len(info['tables'].items()) > 5, 'tables_more': len(tables) > 5,
'table_rows': sum([t['count'] for t in info['tables'].values()]), 'table_rows': sum(t['count'] for t in tables),
'hidden_table_rows': sum(t['count'] for t in hidden_tables),
'hidden_tables_count': len(hidden_tables),
'views_count': len(info['views']),
} }
databases.append(database) databases.append(database)
if as_json: if as_json:
@ -313,26 +318,17 @@ class DatabaseView(BaseView):
async def data(self, request, name, hash): async def data(self, request, name, hash):
if request.args.get('sql'): if request.args.get('sql'):
return await self.custom_sql(request, name, hash) return await self.custom_sql(request, name, hash)
tables = [] info = self.ds.inspect()[name]
table_inspect = self.ds.inspect()[name]['tables'] tables = list(info['tables'].values())
for table_name, info in table_inspect.items(): tables.sort(key=lambda t: (t['hidden'], t['name']))
rows = await self.execute(
name,
'PRAGMA table_info([{}]);'.format(table_name)
)
tables.append({
'name': table_name,
'columns': [r[1] for r in rows],
'table_rows': info['count'],
})
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,
'tables': tables, 'tables': tables,
'views': [v[0] for v in views], 'hidden_count': len([t for t in tables if t['hidden']]),
'views': info['views'],
}, { }, {
'database_hash': hash, 'database_hash': hash,
'show_hidden': request.args.get('_show_hidden'),
} }
async def custom_sql(self, request, name, hash): async def custom_sql(self, request, name, hash):
@ -760,12 +756,14 @@ class Datasette:
m.update(data) m.update(data)
# List tables and their row counts # List tables and their row counts
tables = {} tables = {}
views = []
with sqlite3.connect('file:{}?immutable=1'.format(path), uri=True) as conn: with sqlite3.connect('file:{}?immutable=1'.format(path), uri=True) as conn:
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
table_names = [ table_names = [
r['name'] r['name']
for r in conn.execute('select * from sqlite_master where type="table"') for r in conn.execute('select * from sqlite_master where type="table"')
] ]
views = [v[0] for v in conn.execute('select name from sqlite_master where type = "view"')]
for table in table_names: for table in table_names:
count = conn.execute( count = conn.execute(
'select count(*) from {}'.format(escape_sqlite_table_name(table)) 'select count(*) from {}'.format(escape_sqlite_table_name(table))
@ -810,6 +808,8 @@ class Datasette:
'hash': m.hexdigest(), 'hash': m.hexdigest(),
'file': str(path), 'file': str(path),
'tables': tables, 'tables': tables,
'views': views,
} }
return self._inspect return self._inspect

View file

@ -82,6 +82,10 @@ table a:visited {
margin-bottom: 0; margin-bottom: 0;
} }
h2 em {
font-style: normal;
font-weight: lighter;
}
form.sql textarea { form.sql textarea {
border: 1px solid #ccc; border: 1px solid #ccc;
width: 70%; width: 70%;

View file

@ -65,13 +65,19 @@
{% endif %} {% endif %}
{% for table in tables %} {% for table in tables %}
{% if show_hidden or not table.hidden %}
<div class="db-table"> <div class="db-table">
<h2><a href="/{{ database }}-{{ database_hash }}/{{ table.name|quote_plus }}">{{ table.name }}</a></h2> <h2><a href="/{{ database }}-{{ database_hash }}/{{ table.name|quote_plus }}">{{ table.name }}</a>{% if table.hidden %}<em> (hidden)</em>{% endif %}</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><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> <p>{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}</p>
</div> </div>
{% endif %}
{% endfor %} {% endfor %}
{% if hidden_count and not show_hidden %}
<p>... and <a href="/{{ database }}-{{ database_hash }}?_show_hidden=1">{{ "{:,}".format(hidden_count) }} hidden table{% if hidden_count == 1 %}{% else %}s{% endif %}</a></p>
{% endif %}
{% if views %} {% if views %}
<h2>Views</h2> <h2>Views</h2>
<ul> <ul>

View file

@ -25,8 +25,17 @@
{% for database in databases %} {% for database in databases %}
<h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"><a href="{{ database.path }}">{{ database.name }}</a></h2> <h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"><a href="{{ database.path }}">{{ database.name }}</a></h2>
<p>{{ "{:,}".format(database.table_rows) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}</p> <p>
<p>{% for table, count in database.tables_truncated %}<a href="{{ database.path }}/{{ table|quote_plus }}" title="{{ count }} rows">{{ table }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p> {{ "{:,}".format(database.table_rows) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}{% if database.tables_count and database.hidden_tables_count %}, {% endif %}
{% if database.hidden_tables_count %}
{{ "{:,}".format(database.hidden_table_rows) }} rows in {{ database.hidden_tables_count }} hidden table{% if database.hidden_tables_count != 1 %}s{% endif %}
{% endif %}
{% if database.views_count %}
{% if database.tables_count or database.hidden_tables_count %} - {% endif %}
{{ "{:,}".format(database.views_count) }} view{% if database.views_count != 1 %}s{% endif %}
{% endif %}
</p>
<p>{% for table in database.tables_truncated %}<a href="{{ database.path }}/{{ table.name|quote_plus }}" title="{{ table.count }} rows">{{ table.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p>
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}