New -o option for opening Datasette in your browser, closes #970

This commit is contained in:
Simon Willison 2020-09-22 07:26:47 -07:00
commit a980199e61
4 changed files with 15 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import shutil
from subprocess import call from subprocess import call
import sys import sys
from runpy import run_module from runpy import run_module
import webbrowser
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
from .utils import ( from .utils import (
check_connection, check_connection,
@ -353,6 +354,7 @@ def uninstall(packages, yes):
@click.option("--version-note", help="Additional note to show on /-/versions") @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("--help-config", is_flag=True, help="Show available config options")
@click.option("--pdb", is_flag=True, help="Launch debugger on any errors") @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( def serve(
files, files,
immutable, immutable,
@ -375,6 +377,7 @@ def serve(
version_note, version_note,
help_config, help_config,
pdb, pdb,
open,
return_instance=False, return_instance=False,
): ):
"""Serve up specified SQLite database files with a web UI""" """Serve up specified SQLite database files with a web UI"""
@ -450,7 +453,12 @@ def serve(
# Start the server # Start the server
if root: 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") uvicorn.run(ds.app(), host=host, port=port, log_level="info", lifespan="on")

View file

@ -39,4 +39,5 @@ Options:
--version-note TEXT Additional note to show on /-/versions --version-note TEXT Additional note to show on /-/versions
--help-config Show available config options --help-config Show available config options
--pdb Launch debugger on any errors --pdb Launch debugger on any errors
-o, --open Open Datasette in your web browser
--help Show this message and exit. --help Show this message and exit.

View file

@ -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/ This will start a web server on port 8001 - visit http://localhost:8001/
to access the web interface. 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 Use Chrome on OS X? You can run datasette against your browser history
like so: like so:

View file

@ -105,6 +105,7 @@ def test_metadata_yaml():
get=None, get=None,
help_config=False, help_config=False,
pdb=False, pdb=False,
open=False,
return_instance=True, return_instance=True,
) )
client = _TestClient(ds.app()) client = _TestClient(ds.app())