Skip to content

Commit

Permalink
Give pserve the ability top open server in browser
Browse files Browse the repository at this point in the history
E.g.:

    pserve app.ini --webbrowser-open

or:

    pserve app.ini -w
  • Loading branch information
msabramo committed Jan 20, 2015
1 parent 731a8e8 commit 5296842
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pyramid/scripts/pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import threading
import time
import traceback
import webbrowser

from paste.deploy import loadserver
from paste.deploy import loadapp
from paste.deploy.loadwsgi import loadcontext, SERVER

from pyramid.compat import PY3
from pyramid.compat import WIN
Expand Down Expand Up @@ -121,6 +123,11 @@ class PServeCommand(object):
dest='monitor_restart',
action='store_true',
help="Auto-restart server if it dies")
parser.add_option(
'-w', '--webbrowser-open',
dest='webbrowser_open',
action='store_true',
help="Open a web browser to server url")
parser.add_option(
'--status',
action='store_true',
Expand Down Expand Up @@ -334,6 +341,17 @@ def serve():
msg = ''
self.out('Exiting%s (-v to see traceback)' % msg)

if self.options.webbrowser_open:
def open_browser():
context = loadcontext(SERVER, app_spec, name=app_name, relative_to=base,
global_conf=vars)
url = 'http://{host}:{port}/'.format(**context.config())
time.sleep(1)
webbrowser.open(url)
t = threading.Thread(target=open_browser)
t.setDaemon(True)
t.start()

serve()

def loadapp(self, app_spec, name, relative_to, **kw): # pragma: no cover
Expand Down

0 comments on commit 5296842

Please sign in to comment.