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 --browser

or:

    pserve app.ini -b
  • Loading branch information
msabramo committed Jan 20, 2015
1 parent 64f9cfa commit 8dd9708
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Features
possible to return bytes or unicode.
See https://github.com/Pylons/pyramid/pull/1417

- ``pserve`` can now take a ``-b`` or ``--browser`` option to open the server
URL in a web browser. See https://github.com/Pylons/pyramid/pull/1533

Bug Fixes
---------

Expand Down
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(
'-b', '--browser',
dest='browser',
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.browser:
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 8dd9708

Please sign in to comment.