From a00284f744450623f20e0409a98b07c2d5ccc2e6 Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Mon, 19 Apr 2021 15:59:41 +0200 Subject: [PATCH] Automatically open browser when Invoke task starts web server (#2764) When the `serve` and `livereload` targets are invoked, a web browser will be automatically opened, pointing to the locally-served website. If no web browser can be found by the module, the `open()` call returns `False`, but no exception is raised. This means that it is still possible to call livereload on a remote machine and access it without any error being triggered. Signed-off-by: Romain Porte --- pelican/tools/templates/tasks.py.jinja2 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pelican/tools/templates/tasks.py.jinja2 b/pelican/tools/templates/tasks.py.jinja2 index ff8e526e..861dee37 100644 --- a/pelican/tools/templates/tasks.py.jinja2 +++ b/pelican/tools/templates/tasks.py.jinja2 @@ -13,6 +13,7 @@ from pelican import main as pelican_main from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer from pelican.settings import DEFAULT_CONFIG, get_settings_from_file +OPEN_BROWSER_ON_SERVE = True SETTINGS_FILE_BASE = 'pelicanconf.py' SETTINGS = {} SETTINGS.update(DEFAULT_CONFIG) @@ -81,6 +82,11 @@ def serve(c): (CONFIG['host'], CONFIG['port']), ComplexHTTPRequestHandler) + if OPEN_BROWSER_ON_SERVE: + # Open site in default browser + import webbrowser + webbrowser.open("http://{host}:{port}".format(**CONFIG)) + sys.stderr.write('Serving at {host}:{port} ...\n'.format(**CONFIG)) server.serve_forever() @@ -124,6 +130,12 @@ def livereload(c): for glob in watched_globs: server.watch(glob, cached_build) + + if OPEN_BROWSER_ON_SERVE: + # Open site in default browser + import webbrowser + webbrowser.open("http://{host}:{port}".format(**CONFIG)) + server.serve(host=CONFIG['host'], port=CONFIG['port'], root=CONFIG['deploy_path']) {% if cloudfiles %}