Skip to content

Instantly share code, notes, and snippets.

@d9k
Created January 13, 2022 07:44
Show Gist options
  • Save d9k/a638071ce7146ef01c27779a51d96f2b to your computer and use it in GitHub Desktop.
Save d9k/a638071ce7146ef01c27779a51d96f2b to your computer and use it in GitHub Desktop.
Superset welcome page redirect
import logging
import pprint
from flask import Flask, redirect
from flask_appbuilder import expose, IndexView
from superset.typing import FlaskResponse
logger = logging.getLogger()
logger.warn("Loading config override for Uniweb");
WELCOME_PAGE_REDIRECT_ADMIN="/superset/dashboard/1/"
WELCOME_PAGE_REDIRECT_DEFAULT="/dashboard/list/"
WELCOME_PAGE_REDIRECT_BY_ROLE={
'Test': '/superset/dashboard/2/',
}
# Change welcome page
# https://stackoverflow.com/a/69930056/1760643
class SupersetDashboardIndexView(IndexView):
@expose("/")
def index(self) -> FlaskResponse:
from superset.views.base import is_user_admin, get_user_roles
user_roles = get_user_roles()
logger.warn('__DEBUG__ user roles: ' + pprint.pformat(user_roles))
if is_user_admin():
return redirect(WELCOME_PAGE_REDIRECT_ADMIN)
else:
for role in user_roles:
role_name = role.name
if role_name in WELCOME_PAGE_REDIRECT_BY_ROLE:
return redirect(WELCOME_PAGE_REDIRECT_BY_ROLE[role_name])
return redirect(WELCOME_PAGE_REDIRECT_DEFAULT)
FAB_INDEX_VIEW = f"{SupersetDashboardIndexView.__module__}.{SupersetDashboardIndexView.__name__}"
@ameen7626
Copy link

Hi,
This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

Where do I put this code? @Luiggi370z

Put this code in a new file superset_config.py and specify it in your docker file or there is another way you can go through this documentations

@visharavana
Copy link

Hi,
This works for me in superset v3:

from flask import redirect, g, request

from flask_appbuilder import expose, IndexView

from superset.extensions import (
    appbuilder,
)

from superset.utils.core import (
    get_user_id,
)

from superset.superset_typing import FlaskResponse

class SupersetIndexView(IndexView):
    @expose("/")
    def index(self) -> FlaskResponse:
        if not g.user or not get_user_id():
            # Do steps for anonymous user e.g.
            return redirect("/login")
        # Do steps for authenticated user e.g.
        return redirect("/dashboard/list")


FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"

Where do I put this code? @Luiggi370z

Put this code in a new file superset_config.py and specify it in your docker file or there is another way you can go through this documentations

@ameen7626 Thank you for your response; it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment