Skip to content

Commit

Permalink
Improve handling of index argument (#3221)
Browse files Browse the repository at this point in the history
* Improve handling of index argument

* Fix flake
  • Loading branch information
philippjfr authored Mar 3, 2022
1 parent 0b457a8 commit a068ccb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
6 changes: 2 additions & 4 deletions examples/user_guide/Deploy_and_Export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,11 @@
" A list of request cookies to make available in the\n",
" session context (by default all cookies are included).\n",
" --session-ids MODE One of: unsigned, signed, or external-signed\n",
" --index INDEX Path to a template to use for the site index\n",
" --index INDEX Path to a template to use for the site index or\n",
" an app to serve at the root.\n",
" --disable-index Do not use the default index on the root path\n",
" --disable-index-redirect\n",
" Do not redirect to running app from root path\n",
" --default-app FILENAME\n",
" The app to redirect to from the root. When enabled\n",
" no index is served.\n",
" --num-procs N Number of worker processes for an app. Using 0 will\n",
" autodetect number of cores (defaults to 1)\n",
" --num-threads N Number of threads to launch in a ThreadPoolExecutor which\n",
Expand Down
33 changes: 27 additions & 6 deletions panel/command/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ class Serve(_BkServe):
))
)

# Supported file extensions
_extensions = ['.py']

def customize_applications(self, args, applications):
if args.index and not args.index.endswith('.html'):
index = args.index.split(os.path.sep)[-1]
for ext in self._extensions:
if index.endswith(ext):
index = index[:-len(ext)]
if f'/{index}' in applications:
applications['/'] = applications.pop(f'/{index}')
return super().customize_applications(args, applications)

def customize_kwargs(self, args, server_kwargs):
'''Allows subclasses to customize ``server_kwargs``.
Expand All @@ -211,12 +224,20 @@ def customize_kwargs(self, args, server_kwargs):
else:
files.append(f)

if args.index and not args.index.endswith('.html') and not any(f.endswith(args.index) for f in files):
raise ValueError("The --index argument must either specify a jinja2 "
"template with a .html file extension or select one "
"of the applications being served as the default. "
f"The specified application {args.index!r} could "
"not be found.")
if args.index and not args.index.endswith('.html'):
found = False
for ext in self._extensions:
index = args.index if args.index.endswith(ext) else f'{args.index}{ext}'
if any(f.endswith(index) for f in files):
found = True
if not found:
raise ValueError(
"The --index argument must either specify a jinja2 "
"template with a .html file extension or select one "
"of the applications being served as the default. "
f"The specified application {index!r} could not be "
"found."
)

# Handle tranquilized functions in the supplied functions
if args.rest_provider in REST_PROVIDERS:
Expand Down
16 changes: 1 addition & 15 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
from bokeh.embed.util import RenderItem
from bokeh.io import curdoc
from bokeh.server.server import Server
from bokeh.server.urls import per_app_patterns, toplevel_patterns
from bokeh.server.urls import per_app_patterns
from bokeh.server.views.autoload_js_handler import AutoloadJsHandler as BkAutoloadJsHandler
from bokeh.server.views.doc_handler import DocHandler as BkDocHandler
from bokeh.server.views.root_handler import RootHandler as BkRootHandler
from bokeh.server.views.static_handler import StaticHandler

# Tornado imports
Expand Down Expand Up @@ -272,19 +271,6 @@ async def get(self, *args, **kwargs):

per_app_patterns[3] = (r'/autoload.js', AutoloadJsHandler)

class RootHandler(BkRootHandler):

@authenticated
async def get(self, *args, **kwargs):
if self.index and not self.index.endswith('.html'):
prefix = "" if self.prefix is None else self.prefix
redirect_to = prefix + '.'.join(self.index.split('.')[:-1])
self.redirect(redirect_to)
await super().get(*args, **kwargs)

toplevel_patterns[0] = (r'/?', RootHandler)
bokeh.server.tornado.RootHandler = RootHandler

def modify_document(self, doc):
from bokeh.io.doc import set_curdoc as bk_set_curdoc
from ..config import config
Expand Down

0 comments on commit a068ccb

Please sign in to comment.