Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add flask-admin to a Blueprint?

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?

like image 288
Shirley Avatar asked Feb 27 '14 09:02

Shirley


4 Answers

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/

like image 92
sadaf2605 Avatar answered Sep 17 '22 18:09

sadaf2605


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
like image 34
freethrow Avatar answered Sep 18 '22 18:09

freethrow


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).

like image 21
Martijn Pieters Avatar answered Sep 18 '22 18:09

Martijn Pieters


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.

like image 23
GeoMeteoMe Avatar answered Sep 17 '22 18:09

GeoMeteoMe