Show size of database file next to download link, closes #172

pull/413/head
Simon Willison 2019-02-05 20:53:44 -08:00
rodzic 195a5b3634
commit 4462a5ab28
7 zmienionych plików z 37 dodań i 1 usunięć

Wyświetl plik

@ -305,6 +305,7 @@ class Datasette:
self._inspect[name] = {
"hash": inspect_hash(path),
"file": str(path),
"size": path.stat().st_size,
"views": inspect_views(conn),
"tables": inspect_tables(conn, (self.metadata("databases") or {}).get(name, {}))
}

Wyświetl plik

@ -286,3 +286,8 @@ a.not-underlined {
display: inline-block;
box-shadow: 1px 2px 8px 2px rgba(0,0,0,0.08);
}
.download-sqlite em {
font-style: normal;
font-size: 0.8em;
}

Wyświetl plik

@ -57,7 +57,7 @@
{% endif %}
{% 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 %}
{% include "_codemirror_foot.html" %}

Wyświetl plik

@ -901,3 +901,15 @@ class StaticMount(click.ParamType):
if not os.path.exists(dirpath) or not os.path.isdir(dirpath):
self.fail("%s is not a valid directory path" % value, param, ctx)
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)

Wyświetl plik

@ -19,6 +19,7 @@ from datasette.utils import (
InterruptedError,
InvalidSql,
LimitedWriter,
format_bytes,
is_url,
path_from_row_pks,
path_with_added_args,
@ -102,6 +103,7 @@ class RenderMixin(HTTPMethodView):
"extra_js_urls": self._asset_urls(
"extra_js_urls", template, context
),
"format_bytes": format_bytes,
}
}
)

Wyświetl plik

@ -24,6 +24,7 @@ class DatabaseView(BaseView):
tables.sort(key=lambda t: (t["hidden"], t["name"]))
return {
"database": database,
"size": info["size"],
"tables": tables,
"hidden_count": len([t for t in tables if t["hidden"]]),
"views": info["views"],

Wyświetl plik

@ -374,3 +374,18 @@ def test_path_with_format(path, format, extra_qs, expected):
)
actual = utils.path_with_format(request, format, extra_qs)
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)