summaryrefslogtreecommitdiff
path: root/pelican
diff options
context:
space:
mode:
authorRomain Porte <microjoe@microjoe.org>2021-04-19 15:59:41 +0200
committerGitHub <noreply@github.com>2021-04-19 15:59:41 +0200
commita00284f744450623f20e0409a98b07c2d5ccc2e6 (patch)
tree8eac62074a5f29597db6832af9d577c33ebaea11 /pelican
parent88953d45d55d55ee7e79804749a40d47aa08c412 (diff)
downloadpelican-a00284f744450623f20e0409a98b07c2d5ccc2e6.tar.gz
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 <microjoe@microjoe.org>
Diffstat (limited to 'pelican')
-rw-r--r--pelican/tools/templates/tasks.py.jinja212
1 files changed, 12 insertions, 0 deletions
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 %}