Skip to content

Commit

Permalink
Dispatching the ExplainerHub app to /{self.base_route}/index (#131)
Browse files Browse the repository at this point in the history
* Changes to ExplainerHub...
that dispatches the app to /{self.base_route}/hub
instead of "/index" and "/"

* reverting logout route change

* added bool parameter to ExlainerHub:
index_to_base_route -> only dispatching to base_route when this is True,
by default False. This way the default behaviour should stay the same.

* changing url path names for more clarity

* adds ValueError if db .name in __reserved_names

Co-authored-by: Tunay Okumus
Co-authored-by: Oege Dijk
  • Loading branch information
tunayokumus committed Jul 5, 2021
1 parent aeb4809 commit 5b1fa28
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions explainerdashboard/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ class ExplainerHub:
add and delete users.
"""
__reserved_names = {'login', 'logout', 'admin'}
__reserved_names = {'login', 'logout', 'admin', 'index', 'hub'}

def __init__(self, dashboards:List[ExplainerDashboard], title:str="ExplainerHub",
description:str=None, masonry:bool=False, n_dashboard_cols:int=3,
Expand All @@ -948,8 +948,8 @@ def __init__(self, dashboards:List[ExplainerDashboard], title:str="ExplainerHub"
dbs_open_by_default:bool=False, port:int=8050,
min_height:int=3000, secret_key:str=None, no_index:bool=False,
bootstrap:str=None, fluid:bool=True, base_route:str="dashboards",
max_dashboards:int=None, add_dashboard_route:bool=False,
add_dashboard_pattern:str=None, **kwargs):
index_to_base_route:bool=False, max_dashboards:int=None,
add_dashboard_route:bool=False, add_dashboard_pattern:str=None, **kwargs):
"""
Note:
Expand Down Expand Up @@ -1001,6 +1001,8 @@ def __init__(self, dashboards:List[ExplainerDashboard], title:str="ExplainerHub"
of the browser. Defaults to True.
base_route (str, optional): Base route for dashboard : /<base_route>/dashboard1.
Defaults to "dashboards".
index_to_base_route (bool, optional): Dispatches Hub to "/base_route/index" instead of the default
"/" and "/index". Useful when the host root is not reserved for the ExplainerHub
max_dashboards (int, optional): Max number of dashboards in the hub. Defaults to None
(for no limitation). If set and you add an additional dashboard, the
first dashboard in self.dashboards will be deleted!
Expand Down Expand Up @@ -1072,12 +1074,20 @@ def __init__(self, dashboards:List[ExplainerDashboard], title:str="ExplainerHub"
for dashboard in self.dashboards:
if not self.dbs_open_by_default or dashboard.name in self.dashboards_with_users:
self._protect_dashviews(dashboard.app, username=self.get_dashboard_users(dashboard.name))
if not self.no_index:
if not self.no_index:
if index_to_base_route:
self.hub_base_url = f"/{self.base_route}/index/"
self.index_route = f"/{self.base_route}/hub/"
else:
self.hub_base_url = "/index/"
self.index_route = "/"

self.index_page = self._get_index_page()
if self.users and not self.dbs_open_by_default:
self._protect_dashviews(self.index_page)
self._add_flask_routes(self.app)


def remove_dashboard(self, dashboard_name):
"""Remove a dashboard from the hub"""

Expand Down Expand Up @@ -1331,7 +1341,8 @@ def _instantiate_dashboards(self, dashboards, **kwargs):
dashboard_name = f"dashboard{i+1}"
else:
dashboard_name = dashboard.name

if dashboard_name in self.__reserved_names:
raise ValueError(f"ERROR! dashboard .name should not be in {self.__reserved_names}, but found '{dashboard_name}'!")
update_params = dict(
server=self.app,
name=dashboard_name,
Expand Down Expand Up @@ -1743,7 +1754,7 @@ def dashboard_decks(dashboards, n_cols):
else:
index_page = dash.Dash(__name__,
server=self.app,
url_base_pathname="/index/",
url_base_pathname=f"{self.hub_base_url}",
external_stylesheets=[self.bootstrap] if self.bootstrap is not None else None)
index_page.title = self.title

Expand All @@ -1770,7 +1781,7 @@ def _hub_page(self, route):
<body class="d-flex flex-column min-vh-100">
<div class="container{'-fluid' if self.fluid else ''}">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">{self.title}</a>
<a class="navbar-brand" href="{self.index_route}">{self.title}</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down Expand Up @@ -1814,10 +1825,10 @@ def _add_flask_routes(self, app):
app (flask.Flask): flask app to add routes to.
"""
if self.users and not self.dbs_open_by_default:
@app.route("/")
@app.route(f"{self.index_route}")
@login_required
def index_route():
return self._hub_page("/index")
return self._hub_page(f"{self.hub_base_url}")

def dashboard_route(dashboard):
def inner():
Expand All @@ -1828,9 +1839,9 @@ def inner():
for dashboard in self.dashboards:
app.route(f"/{self.base_route}/_{dashboard.name}")(login_required(dashboard_route(dashboard)))
else:
@app.route("/")
@app.route(f"{self.index_route}")
def index_route():
return self._hub_page("/index")
return self._hub_page(f"{self.hub_base_url}")

def dashboard_route(dashboard):
def inner():
Expand Down Expand Up @@ -1896,7 +1907,7 @@ def run(self, port=None, host='0.0.0.0', use_waitress=False, **kwargs):
"""
if port is None:
port = self.port
print(f"Starting ExplainerHub on http://{get_local_ip_adress()}:{port}", flush=True)
print(f"Starting ExplainerHub on http://{get_local_ip_adress()}:{port}{self.index_route}", flush=True)
if use_waitress:
import waitress
waitress.serve(self.app, host=host, port=port, **kwargs)
Expand Down

0 comments on commit 5b1fa28

Please sign in to comment.