Skip to content

Commit

Permalink
New -o option for opening Datasette in your browser, closes #970
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 22, 2020
1 parent a258339 commit a980199
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion datasette/cli.py
Expand Up @@ -10,6 +10,7 @@
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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"""
Expand Down Expand Up @@ -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")


Expand Down
1 change: 1 addition & 0 deletions docs/datasette-serve-help.txt
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions docs/getting_started.rst
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions tests/test_cli.py
Expand Up @@ -105,6 +105,7 @@ def test_metadata_yaml():
get=None,
help_config=False,
pdb=False,
open=False,
return_instance=True,
)
client = _TestClient(ds.app())
Expand Down

0 comments on commit a980199

Please sign in to comment.