templates/tasks.py: livereload: automatically open browser

When the livereload target is invoked, a web brower will be
automatically opened pointing to the locally-served website.

In case of 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>
This commit is contained in:
Romain Porte 2020-06-05 19:48:33 +02:00
commit 35bbda4507

View file

@ -13,6 +13,7 @@ from pelican import main as pelican_main
from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer
from pelican.settings import DEFAULT_CONFIG, get_settings_from_file from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
OPEN_BROWSER_ON_SERVE = True
SETTINGS_FILE_BASE = 'pelicanconf.py' SETTINGS_FILE_BASE = 'pelicanconf.py'
SETTINGS = {} SETTINGS = {}
SETTINGS.update(DEFAULT_CONFIG) SETTINGS.update(DEFAULT_CONFIG)
@ -124,6 +125,12 @@ def livereload(c):
for glob in watched_globs: for glob in watched_globs:
server.watch(glob, cached_build) 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']) server.serve(host=CONFIG['host'], port=CONFIG['port'], root=CONFIG['deploy_path'])
{% if cloudfiles %} {% if cloudfiles %}