mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
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>
This commit is contained in:
parent
88953d45d5
commit
a00284f744
1 changed files with 12 additions and 0 deletions
|
|
@ -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 %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue