From a980199e61fe7ccf02c2123849d86172d2ae54ff Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 22 Sep 2020 07:26:47 -0700 Subject: [PATCH] New -o option for opening Datasette in your browser, closes #970 --- datasette/cli.py | 10 +++++++++- docs/datasette-serve-help.txt | 1 + docs/getting_started.rst | 4 ++++ tests/test_cli.py | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/datasette/cli.py b/datasette/cli.py index 231ae8b7..5d8333c6 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -10,6 +10,7 @@ import shutil from subprocess import call import sys from runpy import run_module +import webbrowser from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm from .utils import ( check_connection, @@ -353,6 +354,7 @@ def uninstall(packages, yes): @click.option("--version-note", help="Additional note to show on /-/versions") @click.option("--help-config", is_flag=True, help="Show available config options") @click.option("--pdb", is_flag=True, help="Launch debugger on any errors") +@click.option("-o", "--open", is_flag=True, help="Open Datasette in your web browser") def serve( files, immutable, @@ -375,6 +377,7 @@ def serve( version_note, help_config, pdb, + open, return_instance=False, ): """Serve up specified SQLite database files with a web UI""" @@ -450,7 +453,12 @@ def serve( # Start the server if root: - print("http://{}:{}/-/auth-token?token={}".format(host, port, ds._root_token)) + url = "http://{}:{}/-/auth-token?token={}".format(host, port, ds._root_token) + print(url) + else: + url = "http://{}:{}/".format(host, port) + if open: + webbrowser.open(url) uvicorn.run(ds.app(), host=host, port=port, log_level="info", lifespan="on") diff --git a/docs/datasette-serve-help.txt b/docs/datasette-serve-help.txt index 536e43b0..ac3ca49f 100644 --- a/docs/datasette-serve-help.txt +++ b/docs/datasette-serve-help.txt @@ -39,4 +39,5 @@ Options: --version-note TEXT Additional note to show on /-/versions --help-config Show available config options --pdb Launch debugger on any errors + -o, --open Open Datasette in your web browser --help Show this message and exit. diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 44617b8b..2f0a7962 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -47,6 +47,10 @@ First, follow the :ref:`installation` instructions. Now you can run Datasette ag This will start a web server on port 8001 - visit http://localhost:8001/ to access the web interface. +Add ``-o`` to open your browser automatically once Datasette has started:: + + datasette path/to/database.db -o + Use Chrome on OS X? You can run datasette against your browser history like so: diff --git a/tests/test_cli.py b/tests/test_cli.py index b1c50282..093c84b5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -105,6 +105,7 @@ def test_metadata_yaml(): get=None, help_config=False, pdb=False, + open=False, return_instance=True, ) client = _TestClient(ds.app())