How do I add flask-admin to a Blueprint?
Asked Answered
U

5

8

for example:

from flask import Flask
from flask.ext.admin import Admin, BaseView, expose

class MyView(BaseView):
    @expose('/')
    def index(self):
        return self.render('index.html')

app = Flask(__name__)

admin = Admin(app)
admin.add_view(MyView(name='Hello'))

app.run()

but, if I need a new file, called 'views.py', how can I add a view into views.py to admin? Do I need to use a blueprint?

Underfeed answered 27/2, 2014 at 9:54 Comment(0)
P
8

For my project I actually made a child class of Blueprint that supports flask admin:

from flask import Blueprint
from flask_admin.contrib.sqla import ModelView
from flask_admin import Admin

class AdminBlueprint(Blueprint):
    views=None


    def __init__(self,*args, **kargs):
        self.views = []
        return super(AdminBlueprint, self).__init__('admin2', __name__,url_prefix='/admin2',static_folder='static', static_url_path='/static/admin')


    def add_view(self, view):
        self.views.append(view)

    def register(self,app, options, first_registration=False):
        admin = Admin(app, name='microblog', template_mode='adminlte')

        for v in self.views:
            admin.add_view(v)

        return super(AdminBlueprint, self).register(app, options, first_registration)

For details you may like to read my blog here: http://blog.sadafnoor.me/blog/how-to-add-flask-admin-to-a-blueprint/

Predilection answered 21/9, 2016 at 13:55 Comment(5)
Thanks @sadaf2605, your code works perfectly, again.. I say thank you.. you have structured my code :)Vociferous
Still works fine as of today, but i needed to change the template_mode admin = Admin(app, name='microblog', template_mode='bootstrap3') and remove the print app stuff as this it not python 3.7 compatibleLisp
@max, glad that it helped, thank you for pointing out. removed print statement.Predilection
Thanks @sadaf2605. Your blog article URL 404s - is it available anywhere else now?Tandy
Archive of the blog article. web.archive.org/web/20190908233339/http://blog.sadafnoor.me/…Amazon
C
7

I am very late for this question, but anyway... My guess is that you want to use the Application Factory pattern and use the Flask-Admin. There is a nice discussion about the underlying problems. I used a very ugly solution, instantiating the Flask-Admin in the init.py file:

from flask_admin.contrib.sqla import ModelView

class UserModelView(ModelView):

    create_modal = True
    edit_modal = True
    can_export = True

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    db.init_app(app)


    # import models here because otherwise it will throw errors    
    from models import User, Sector, Article

    admin.init_app(app)
    admin.add_view(UserModelView(User, db.session))      

    # attach blueprints
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app
Cloaca answered 19/11, 2015 at 13:12 Comment(0)
H
5

You don't need a blueprint for that. In views.py add an import for the admin object you defined in your main project:

from projectmodule import admin
from flask.ext.admin import BaseView, expose

class MyView(BaseView):
    @expose('/')
    def index(self):
        return self.render('index.html')

admin.add_view(MyView(name='Hello'))

and in your main projectmodule file use:

from flask import Flask
from flask.ext.admin import Admin


app = Flask(__name__)
admin = Admin(app)

# import the views
import views

app.run()

e.g. you add import views after the line that sets admin = Admin(app).

Hymnal answered 27/2, 2014 at 22:46 Comment(0)
F
3

I have an flask app with one blueprint (and login/logout to admin). This is the best solution I found to implement flask admin with some custom features.

My structure as follows:

my_app
  main
    __init__.py
    routes.py
  static
  templates
  __init__.py
  config.py
  models.py
  run.py

Customized admin index view from models.py

from flask_admin import AdminIndexView

class MyAdminIndexView(AdminIndexView):
    def is_accessible(self):
        return current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        return redirect(url_for('main.home'))

Main init.py as follows:


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.menu import MenuLink
from my_app.config import Config

# create extensions
db = SQLAlchemy()
admin = Admin()

def create_app(config_class=Config): # default configutation

    app = Flask(__name__)
    app.config.from_object(Config)

    #initialize extensions
    db.init_app(app)
    ...

    # customized admin views
    from my_app.models import MyAdminIndexView
    admin.init_app(app,index_view=MyAdminIndexView())
    admin.add_link(MenuLink(name='Home', url='/'))
    admin.add_link(MenuLink(name='Logout', url='/logout'))

    #blueprint
    from my_app.main.routes import main
    app.register_blueprint(main)

    return app

I think this is the most elegant solution I came up so far.

Flaxseed answered 31/3, 2020 at 0:2 Comment(0)
F
0

In order to keep clean the __init__.py root file:

# app/__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from app.config import Config
from flask_admin import Admin

db = SQLAlchemy()
admin = Admin(template_mode="bootstrap3")

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    admin.init_app(app)

    from app.admin import bp
    app.register_blueprint(bp, url_prefix='/admin')
    
    return app

Then in the __init__.py of the Blueprint admin app

# app/admin/__init__.py

from flask import Blueprint
from app import admin, db
from app.models import User
from flask_admin.contrib.sqla import ModelView

bp = Blueprint('admin_app', __name__)

admin.name = 'Admin panel'
admin.add_view(ModelView(User, db.session))
# all the rest stuff
Fairleigh answered 23/5, 2022 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.