Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document address argument to pn.serve #1435

Merged
merged 4 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion examples/user_guide/Deploy_and_Export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,38 @@
")\n",
"```\n",
"\n",
"The ``pn.serve`` function accepts the same arguments as the `show` method."
"The ``pn.serve`` accepts a number of arguments:\n",
"\n",
" panel: Viewable, function or {str: Viewable or function}\n",
" A Panel object, a function returning a Panel object or a\n",
" dictionary mapping from the URL slug to either.\n",
" port: int (optional, default=0)\n",
" Allows specifying a specific port\n",
" address: str\n",
" The address the server should listen on for HTTP requests.\n",
" websocket_origin: str or list(str) (optional)\n",
" A list of hosts that can connect to the websocket.\n",
"\n",
" This is typically required when embedding a server app in\n",
" an external web site.\n",
"\n",
" If None, \"localhost\" is used.\n",
" loop: tornado.ioloop.IOLoop (optional, default=IOLoop.current())\n",
" The tornado IOLoop to run the Server on\n",
" show: boolean (optional, default=False)\n",
" Whether to open the server in a new browser tab on start\n",
" start: boolean(optional, default=False)\n",
" Whether to start the Server\n",
" title: str or {str: str} (optional, default=None)\n",
" An HTML title for the application or a dictionary mapping\n",
" from the URL slug to a customized title\n",
" verbose: boolean (optional, default=True)\n",
" Whether to print the address and port\n",
" location: boolean or panel.io.location.Location\n",
" Whether to create a Location component to observe and\n",
" set the URL location.\n",
" kwargs: dict\n",
" Additional keyword arguments to pass to Server instance"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions panel/io/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ def show_server(panel, notebook_url, port):
else:
origin = _origin_url(notebook_url)
server_id = uuid.uuid4().hex
server = get_server(panel, port, origin, start=True, show=False,
server_id=server_id)
server = get_server(panel, port=port, websocket_origin=origin,
start=True, show=False, server_id=server_id)

if callable(notebook_url):
url = notebook_url(server.port)
Expand Down
40 changes: 24 additions & 16 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ def unlocked():
curdoc.unhold()


def serve(panels, port=0, websocket_origin=None, loop=None, show=True,
start=True, title=None, verbose=True, location=True, **kwargs):
def serve(panels, port=0, address=None, websocket_origin=None, loop=None,
show=True, start=True, title=None, verbose=True, location=True,
**kwargs):
"""
Allows serving one or more panel objects on a single server.
The panels argument should be either a Panel object or a function
Expand All @@ -123,11 +124,13 @@ def serve(panels, port=0, websocket_origin=None, loop=None, show=True,

Arguments
---------
panel: Viewable, function or {str: Viewable}
panel: Viewable, function or {str: Viewable or function}
A Panel object, a function returning a Panel object or a
dictionary mapping from the URL slug to either.
port: int (optional, default=0)
Allows specifying a specific port
address : str
The address the server should listen on for HTTP requests.
websocket_origin: str or list(str) (optional)
A list of hosts that can connect to the websocket.

Expand All @@ -152,8 +155,8 @@ def serve(panels, port=0, websocket_origin=None, loop=None, show=True,
kwargs: dict
Additional keyword arguments to pass to Server instance
"""
return get_server(panels, port, websocket_origin, loop, show, start,
title, verbose, location, **kwargs)
return get_server(panels, port, address, websocket_origin, loop,
show, start, title, verbose, location, **kwargs)


class ProxyFallbackHandler(RequestHandler):
Expand Down Expand Up @@ -195,9 +198,9 @@ def get_static_routes(static_dirs):
return patterns


def get_server(panel, port=0, websocket_origin=None, loop=None,
show=False, start=False, title=None, verbose=False,
location=True, static_dirs={}, **kwargs):
def get_server(panel, port=0, address=None, websocket_origin=None,
loop=None, show=False, start=False, title=None,
verbose=False, location=True, static_dirs={}, **kwargs):
"""
Returns a Server instance with this panel attached as the root
app.
Expand All @@ -209,6 +212,8 @@ def get_server(panel, port=0, websocket_origin=None, loop=None,
dictionary mapping from the URL slug to either.
port: int (optional, default=0)
Allows specifying a specific port
address : str
The address the server should listen on for HTTP requests.
websocket_origin: str or list(str) (optional)
A list of hosts that can connect to the websocket.

Expand All @@ -217,24 +222,24 @@ def get_server(panel, port=0, websocket_origin=None, loop=None,

If None, "localhost" is used.
loop : tornado.ioloop.IOLoop (optional, default=IOLoop.current())
The tornado IOLoop to run the Server on
The tornado IOLoop to run the Server on.
show : boolean (optional, default=False)
Whether to open the server in a new browser tab on start
Whether to open the server in a new browser tab on start.
start : boolean(optional, default=False)
Whether to start the Server
title: str or {str: str} (optional, default=None)
Whether to start the Server.
title : str or {str: str} (optional, default=None)
An HTML title for the application or a dictionary mapping
from the URL slug to a customized title
from the URL slug to a customized title.
verbose: boolean (optional, default=False)
Whether to report the address and port
Whether to report the address and port.
location : boolean or panel.io.location.Location
Whether to create a Location component to observe and
set the URL location.
static_dirs: dict (optional, default={})
A dictionary of routes and local paths to serve as static file
directories on those routes
directories on those routes.
kwargs: dict
Additional keyword arguments to pass to Server instance
Additional keyword arguments to pass to Server instance.

Returns
-------
Expand Down Expand Up @@ -286,6 +291,9 @@ def get_server(panel, port=0, websocket_origin=None, loop=None,
if 'index' not in opts:
opts['index'] = INDEX_HTML

if address is not None:
opts['address'] = address

if websocket_origin:
if not isinstance(websocket_origin, list):
websocket_origin = [websocket_origin]
Expand Down
2 changes: 1 addition & 1 deletion panel/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_server_static_dirs():
loop = IOLoop()
server = StoppableThread(
target=html._get_server, io_loop=loop,
args=(5008, None, loop, False, True, None, False, None),
args=(5008, None, None, loop, False, True, None, False, None),
kwargs=dict(static_dirs={'tests': os.path.dirname(__file__)}))
server.start()

Expand Down
21 changes: 12 additions & 9 deletions panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ def _modify_doc(self, server_id, title, doc, location):
state._servers[server_id][2].append(doc)
return self.server_doc(doc, title, location)

def _get_server(self, port=0, websocket_origin=None, loop=None,
def _get_server(self, port=0, address=None, websocket_origin=None, loop=None,
show=False, start=False, title=None, verbose=False,
location=True, **kwargs):
return get_server(self, port, websocket_origin, loop, show,
start, title, verbose, **kwargs)
return get_server(self, port, address, websocket_origin, loop,
show, start, title, verbose, **kwargs)

def _on_msg(self, ref, manager, msg):
"""
Expand Down Expand Up @@ -301,15 +301,19 @@ def servable(self, title=None, location=True):
self.server_doc(title=title, location=True)
return self

def show(self, title=None, port=0, websocket_origin=None, threaded=False,
verbose=True, open=True, location=True, **kwargs):
def show(self, title=None, port=0, address=None, websocket_origin=None,
threaded=False, verbose=True, open=True, location=True, **kwargs):
"""
Starts a Bokeh server and displays the Viewable in a new tab.

Arguments
---------
title : str
A string title to give the Document (if served as an app)
port: int (optional, default=0)
Allows specifying a specific port
address : str
The address the server should listen on for HTTP requests.
websocket_origin: str or list(str) (optional)
A list of hosts that can connect to the websocket.
This is typically required when embedding a server app in
Expand All @@ -318,8 +322,6 @@ def show(self, title=None, port=0, websocket_origin=None, threaded=False,
threaded: boolean (optional, default=False)
Whether to launch the Server on a separate thread, allowing
interactive use.
title : str
A string title to give the Document (if served as an app)
verbose: boolean (optional, default=True)
Whether to print the address and port
open : boolean (optional, default=True)
Expand All @@ -339,12 +341,13 @@ def show(self, title=None, port=0, websocket_origin=None, threaded=False,
loop = IOLoop()
server = StoppableThread(
target=self._get_server, io_loop=loop,
args=(port, websocket_origin, loop, open, True, title, verbose, location),
args=(port, address, websocket_origin, loop, open,
True, title, verbose, location),
kwargs=kwargs)
server.start()
else:
server = self._get_server(
port, websocket_origin, show=open, start=True,
port, address, websocket_origin, show=open, start=True,
title=title, verbose=verbose, location=location, **kwargs
)
return server
Expand Down