mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
f96e55bce5
commit
1c8638c30a
4 changed files with 43 additions and 24 deletions
|
|
@ -267,18 +267,23 @@ class IndexView(HTTPMethodView):
|
|||
async def get(self, request, as_json):
|
||||
databases = []
|
||||
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 = {
|
||||
'name': key,
|
||||
'hash': info['hash'],
|
||||
'path': '{}-{}'.format(key, info['hash'][:7]),
|
||||
'tables_truncated': sorted(
|
||||
info['tables'].items(),
|
||||
key=lambda p: p[1]['count'],
|
||||
tables,
|
||||
key=lambda t: t['count'],
|
||||
reverse=True
|
||||
)[:5],
|
||||
'tables_count': len(info['tables'].items()),
|
||||
'tables_more': len(info['tables'].items()) > 5,
|
||||
'table_rows': sum([t['count'] for t in info['tables'].values()]),
|
||||
'tables_count': len(tables),
|
||||
'tables_more': len(tables) > 5,
|
||||
'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)
|
||||
if as_json:
|
||||
|
|
@ -313,26 +318,17 @@ class DatabaseView(BaseView):
|
|||
async def data(self, request, name, hash):
|
||||
if request.args.get('sql'):
|
||||
return await self.custom_sql(request, name, hash)
|
||||
tables = []
|
||||
table_inspect = self.ds.inspect()[name]['tables']
|
||||
for table_name, info in table_inspect.items():
|
||||
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"')
|
||||
info = self.ds.inspect()[name]
|
||||
tables = list(info['tables'].values())
|
||||
tables.sort(key=lambda t: (t['hidden'], t['name']))
|
||||
return {
|
||||
'database': name,
|
||||
'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,
|
||||
'show_hidden': request.args.get('_show_hidden'),
|
||||
}
|
||||
|
||||
async def custom_sql(self, request, name, hash):
|
||||
|
|
@ -760,12 +756,14 @@ class Datasette:
|
|||
m.update(data)
|
||||
# List tables and their row counts
|
||||
tables = {}
|
||||
views = []
|
||||
with sqlite3.connect('file:{}?immutable=1'.format(path), uri=True) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
table_names = [
|
||||
r['name']
|
||||
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:
|
||||
count = conn.execute(
|
||||
'select count(*) from {}'.format(escape_sqlite_table_name(table))
|
||||
|
|
@ -810,6 +808,8 @@ class Datasette:
|
|||
'hash': m.hexdigest(),
|
||||
'file': str(path),
|
||||
'tables': tables,
|
||||
'views': views,
|
||||
|
||||
}
|
||||
return self._inspect
|
||||
|
||||
|
|
|
|||
|
|
@ -82,6 +82,10 @@ table a:visited {
|
|||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
h2 em {
|
||||
font-style: normal;
|
||||
font-weight: lighter;
|
||||
}
|
||||
form.sql textarea {
|
||||
border: 1px solid #ccc;
|
||||
width: 70%;
|
||||
|
|
|
|||
|
|
@ -65,13 +65,19 @@
|
|||
{% endif %}
|
||||
|
||||
{% for table in tables %}
|
||||
{% if show_hidden or not table.hidden %}
|
||||
<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>{{ "{:,}".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>
|
||||
{% endif %}
|
||||
{% 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 %}
|
||||
<h2>Views</h2>
|
||||
<ul>
|
||||
|
|
|
|||
|
|
@ -25,8 +25,17 @@
|
|||
|
||||
{% for database in databases %}
|
||||
<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>{% 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>
|
||||
<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 %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue