mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
-o now opens to most relevant page, closes #976
This commit is contained in:
parent
105a2c10fd
commit
f5dbe61a45
3 changed files with 47 additions and 4 deletions
|
|
@ -17,6 +17,7 @@ from .utils import (
|
||||||
parse_metadata,
|
parse_metadata,
|
||||||
ConnectionProblem,
|
ConnectionProblem,
|
||||||
SpatialiteConnectionProblem,
|
SpatialiteConnectionProblem,
|
||||||
|
initial_path_for_datasette,
|
||||||
temporary_docker_directory,
|
temporary_docker_directory,
|
||||||
value_as_boolean,
|
value_as_boolean,
|
||||||
SpatialiteNotFound,
|
SpatialiteNotFound,
|
||||||
|
|
@ -456,14 +457,19 @@ def serve(
|
||||||
return
|
return
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
|
url = None
|
||||||
if root:
|
if root:
|
||||||
url = "http://{}:{}{}?token={}".format(
|
url = "http://{}:{}{}?token={}".format(
|
||||||
host, port, ds.urls.path("-/auth-token"), ds._root_token
|
host, port, ds.urls.path("-/auth-token"), ds._root_token
|
||||||
)
|
)
|
||||||
print(url)
|
print(url)
|
||||||
else:
|
|
||||||
url = "http://{}:{}{}".format(host, port, ds.urls.instance())
|
|
||||||
if open_browser:
|
if open_browser:
|
||||||
|
if url is None:
|
||||||
|
# Figure out most convenient URL - to table, database or homepage
|
||||||
|
path = asyncio.get_event_loop().run_until_complete(
|
||||||
|
initial_path_for_datasette(ds)
|
||||||
|
)
|
||||||
|
url = "http://{}:{}{}".format(host, port, path)
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
ds.app(), host=host, port=port, log_level="info", lifespan="on", workers=1
|
ds.app(), host=host, port=port, log_level="info", lifespan="on", workers=1
|
||||||
|
|
|
||||||
|
|
@ -991,3 +991,18 @@ def find_spatialite():
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
return path
|
return path
|
||||||
raise SpatialiteNotFound
|
raise SpatialiteNotFound
|
||||||
|
|
||||||
|
|
||||||
|
async def initial_path_for_datasette(datasette):
|
||||||
|
"Return suggested path for opening this Datasette, based on number of DBs and tables"
|
||||||
|
if len(datasette.databases) == 1:
|
||||||
|
db_name = next(iter(datasette.databases.keys()))
|
||||||
|
path = datasette.urls.database(db_name)
|
||||||
|
# Does this DB only have one table?
|
||||||
|
db = next(iter(datasette.databases.values()))
|
||||||
|
tables = await db.table_names()
|
||||||
|
if len(tables) == 1:
|
||||||
|
path = datasette.urls.table(db_name, tables[0])
|
||||||
|
else:
|
||||||
|
path = datasette.urls.instance()
|
||||||
|
return path
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Tests for various datasette helper functions.
|
Tests for various datasette helper functions.
|
||||||
"""
|
"""
|
||||||
|
from datasette.app import Datasette
|
||||||
from datasette import utils
|
from datasette import utils
|
||||||
from datasette.utils.asgi import Request
|
from datasette.utils.asgi import Request
|
||||||
from datasette.filters import Filters
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
@ -554,3 +553,26 @@ def test_resolve_env_secrets(config, expected):
|
||||||
)
|
)
|
||||||
def test_display_actor(actor, expected):
|
def test_display_actor(actor, expected):
|
||||||
assert expected == utils.display_actor(actor)
|
assert expected == utils.display_actor(actor)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"dbs,expected_path",
|
||||||
|
[
|
||||||
|
(["one_table"], "/one/one"),
|
||||||
|
(["two_tables"], "/two"),
|
||||||
|
(["one_table", "two_tables"], "/"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_initial_path_for_datasette(tmp_path_factory, dbs, expected_path):
|
||||||
|
db_dir = tmp_path_factory.mktemp("dbs")
|
||||||
|
one_table = str(db_dir / "one.db")
|
||||||
|
sqlite3.connect(one_table).execute("create table one (id integer primary key)")
|
||||||
|
two_tables = str(db_dir / "two.db")
|
||||||
|
sqlite3.connect(two_tables).execute("create table two (id integer primary key)")
|
||||||
|
sqlite3.connect(two_tables).execute("create table three (id integer primary key)")
|
||||||
|
datasette = Datasette(
|
||||||
|
[{"one_table": one_table, "two_tables": two_tables}[db] for db in dbs]
|
||||||
|
)
|
||||||
|
path = await utils.initial_path_for_datasette(datasette)
|
||||||
|
assert path == expected_path
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue