pythonflaskflask-appbuilder

How can a flask Addon overwrite a template?


I created a flask addon using "flask fab create-addon".

I would like to change the template appbuilder/general/security/login_oauth.html so I have:

templates
  appbuilder
     general
       security
          login_oauth.html

But when I load the host application my version of login_oauth.html is not loaded. I tried registering a blueprint as in this post with the following code:

from flask import Blueprint
bp = Blueprint('fab_addon_fslogin', __name__, template_folder='templates')

class MyAddOnManager(BaseManager):
    def __init__(self, appbuilder):
        """
        Use the constructor to setup any config keys specific for your app.
        """
        super(MyAddOnManager, self).__init__(appbuilder)
        self.appbuilder.get_app.config.setdefault("MYADDON_KEY", "SOME VALUE")
        self.appbuilder.register_blueprint(bp)

    def register_views(self):
        """
        This method is called by AppBuilder when initializing, use it to add you views
        """
        pass

    def pre_process(self):
        pass

    def post_process(self):
        pass

But register_blueprint(bp) return:

  File "/home/cquiros/data/projects2017/personal/software/superset/addons/fab_addon_fslogin/fab_addon_fslogin/manager.py", line 24, in __init__
    self.appbuilder.register_blueprint(bp)
  File "/home/cquiros/data/projects2017/personal/software/superset/env_superset/lib/python3.8/site-packages/Flask_AppBuilder-3.3.0-py3.8.egg/flask_appbuilder/base.py", line 643, in register_blueprint
    baseview.create_blueprint(
AttributeError: 'Blueprint' object has no attribute 'create_blueprint'

Not much information out there on how to do this. Any clue is appreciated


Solution

  • If you want to customize login_oauth.html, The easiest way is that adding it into your app directly not addon. That means the login_oauth.html should be put in this path.

    YourApp
    - app
    -- templates
    --- appbuilder
    ---- general
    ----- security
    ------ login_oauth.html
    

    For solution of addons, the function self.appbuilder.register_blueprint is for views not for the object which Blueprint fuction returns. It should be replaced with

    self.appbuilder.get_app.register_blueprint(Blueprint('fab_addon_fslogin', __name__, template_folder='templates')) 
    

    Keep the blueprint registration and try to rename the login_oauth.html to login_oauth_xxxx.html which at

    {your python packages root path}\site-packages\flask_appbuilder\templates\appbuilder\general\security
    

    That will let the template be overwrote as you need. I guess that the template searching order of app-builder package is greater than addons. The searching order of blueprints depend on order of registrations

    Finally, I have found a trick without renaming the file, you could try the following

    class MyAddOnManager(BaseManager):
    
    
        def __init__(self, appbuilder):
            """
                 Use the constructor to setup any config keys specific for your app. 
            """
            super(MyAddOnManager, self).__init__(appbuilder)
            self.appbuilder.get_app.config.setdefault('MYADDON_KEY', 'SOME VALUE')
            self.static_bp = Blueprint('fab_addon_fslogin', __name__, template_folder='templates')
    
        def register_views(self):
            """
                This method is called by AppBuilder when initializing, use it to add you views
            """
            pass
    
        def pre_process(self):
            self.appbuilder.get_app.register_blueprint(self.static_bp)
            blueprint_order = self.appbuilder.get_app._blueprint_order
            # move blueprint order of addon to top
            blueprint_order.insert(0, blueprint_order.pop())
    
        def post_process(self):
            pass
    

    Reference: flask-appbuilder Customizing