Allow JupyterHub.default_url to be a callable based on user

This commit is contained in:
Dan Lester
2020-08-05 11:59:25 +01:00
parent b11c02c6e0
commit d915cc3ff2
2 changed files with 23 additions and 4 deletions

View File

@@ -55,6 +55,7 @@ from traitlets import (
Instance,
Bytes,
Float,
Union,
observe,
default,
validate,
@@ -1314,12 +1315,23 @@ class JupyterHub(Application):
"""
).tag(config=True)
default_url = Unicode(
default_url = Union(
[Unicode(), Callable()],
help="""
The default URL for users when they arrive (e.g. when user directs to "/")
By default, redirects users to their own server.
"""
Can be a Unicode string (e.g. '/hub/home') or a callable based on the user object:
def default_url_fn(user):
if user:
if user.admin:
return '/hub/admin'
return '/hub/home'
c.JupyterHub.default_url = default_url_fn
""",
).tag(config=True)
user_redirect_hook = Callable(

View File

@@ -638,8 +638,15 @@ class BaseHandler(RequestHandler):
)
if not next_url:
# custom default URL
next_url = default or self.default_url
# custom default URL, usually passed because user landed on that page but was not logged in
if default:
next_url = default
else:
# As set in jupyterhub_config.py
if callable(self.default_url):
next_url = self.default_url(user)
else:
next_url = self.default_url
if not next_url:
# default URL after login