mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Show size of database file next to download link, closes #172
This commit is contained in:
parent
195a5b3634
commit
4462a5ab28
7 changed files with 37 additions and 1 deletions
|
|
@ -305,6 +305,7 @@ class Datasette:
|
||||||
self._inspect[name] = {
|
self._inspect[name] = {
|
||||||
"hash": inspect_hash(path),
|
"hash": inspect_hash(path),
|
||||||
"file": str(path),
|
"file": str(path),
|
||||||
|
"size": path.stat().st_size,
|
||||||
"views": inspect_views(conn),
|
"views": inspect_views(conn),
|
||||||
"tables": inspect_tables(conn, (self.metadata("databases") or {}).get(name, {}))
|
"tables": inspect_tables(conn, (self.metadata("databases") or {}).get(name, {}))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -286,3 +286,8 @@ a.not-underlined {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
box-shadow: 1px 2px 8px 2px rgba(0,0,0,0.08);
|
box-shadow: 1px 2px 8px 2px rgba(0,0,0,0.08);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.download-sqlite em {
|
||||||
|
font-style: normal;
|
||||||
|
font-size: 0.8em;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if config.allow_download %}
|
{% if config.allow_download %}
|
||||||
<p>Download SQLite DB: <a href="/{{ database }}-{{ database_hash }}.db">{{ database }}.db</a></p>
|
<p class="download-sqlite">Download SQLite DB: <a href="/{{ database }}-{{ database_hash }}.db">{{ database }}.db</a> <em>{{ format_bytes(size) }}</em></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% include "_codemirror_foot.html" %}
|
{% include "_codemirror_foot.html" %}
|
||||||
|
|
|
||||||
|
|
@ -901,3 +901,15 @@ class StaticMount(click.ParamType):
|
||||||
if not os.path.exists(dirpath) or not os.path.isdir(dirpath):
|
if not os.path.exists(dirpath) or not os.path.isdir(dirpath):
|
||||||
self.fail("%s is not a valid directory path" % value, param, ctx)
|
self.fail("%s is not a valid directory path" % value, param, ctx)
|
||||||
return path, dirpath
|
return path, dirpath
|
||||||
|
|
||||||
|
|
||||||
|
def format_bytes(bytes):
|
||||||
|
current = float(bytes)
|
||||||
|
for unit in ("bytes", "KB", "MB", "GB", "TB"):
|
||||||
|
if current < 1024:
|
||||||
|
break
|
||||||
|
current = current / 1024
|
||||||
|
if unit == "bytes":
|
||||||
|
return "{} {}".format(int(current), unit)
|
||||||
|
else:
|
||||||
|
return "{:.1f} {}".format(current, unit)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ from datasette.utils import (
|
||||||
InterruptedError,
|
InterruptedError,
|
||||||
InvalidSql,
|
InvalidSql,
|
||||||
LimitedWriter,
|
LimitedWriter,
|
||||||
|
format_bytes,
|
||||||
is_url,
|
is_url,
|
||||||
path_from_row_pks,
|
path_from_row_pks,
|
||||||
path_with_added_args,
|
path_with_added_args,
|
||||||
|
|
@ -102,6 +103,7 @@ class RenderMixin(HTTPMethodView):
|
||||||
"extra_js_urls": self._asset_urls(
|
"extra_js_urls": self._asset_urls(
|
||||||
"extra_js_urls", template, context
|
"extra_js_urls", template, context
|
||||||
),
|
),
|
||||||
|
"format_bytes": format_bytes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ class DatabaseView(BaseView):
|
||||||
tables.sort(key=lambda t: (t["hidden"], t["name"]))
|
tables.sort(key=lambda t: (t["hidden"], t["name"]))
|
||||||
return {
|
return {
|
||||||
"database": database,
|
"database": database,
|
||||||
|
"size": info["size"],
|
||||||
"tables": tables,
|
"tables": tables,
|
||||||
"hidden_count": len([t for t in tables if t["hidden"]]),
|
"hidden_count": len([t for t in tables if t["hidden"]]),
|
||||||
"views": info["views"],
|
"views": info["views"],
|
||||||
|
|
|
||||||
|
|
@ -374,3 +374,18 @@ def test_path_with_format(path, format, extra_qs, expected):
|
||||||
)
|
)
|
||||||
actual = utils.path_with_format(request, format, extra_qs)
|
actual = utils.path_with_format(request, format, extra_qs)
|
||||||
assert expected == actual
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"bytes,expected",
|
||||||
|
[
|
||||||
|
(120, '120 bytes'),
|
||||||
|
(1024, '1.0 KB'),
|
||||||
|
(1024 * 1024, '1.0 MB'),
|
||||||
|
(1024 * 1024 * 1024, '1.0 GB'),
|
||||||
|
(1024 * 1024 * 1024 * 1.3, '1.3 GB'),
|
||||||
|
(1024 * 1024 * 1024 * 1024, '1.0 TB'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_format_bytes(bytes, expected):
|
||||||
|
assert expected == utils.format_bytes(bytes)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue