6

I am using flask-admin, and I want to add a dashboard to the home page. I found I can add a new page using:

admin = Admin(name='Dashboard', base_template='admin/my_master.html', template_mode='bootstrap3')

then:

admin.init_app(app)

and finally I added my_master.html, and added content. However, it is all static, how can I add custom data to that view?

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
nycynik
  • 7,371
  • 8
  • 62
  • 87

2 Answers2

16

I found the answer in the documentation: http://flask-admin.readthedocs.org/en/latest/api/mod_base/

It can be overridden by passing your own view class to the Admin constructor:

class MyHomeView(AdminIndexView):
    @expose('/')
    def index(self):
        arg1 = 'Hello'
        return self.render('admin/myhome.html', arg1=arg1)

admin = Admin(index_view=MyHomeView())

Also, you can change the root url from /admin to / with the following:

admin = Admin(
    app,
    index_view=AdminIndexView(
        name='Home',
        template='admin/myhome.html',
        url='/'
    )
)

Default values for the index page are:

  • If a name is not provided, ‘Home’ will be used.
  • If an endpoint is not provided, will default to admin Default URL route is /admin.
  • Automatically associates with static folder. Default template is admin/index.html
nycynik
  • 7,371
  • 8
  • 62
  • 87
  • also xhr/ajax might be the best solution for your project if you are reading this. I found that to be another option that worked well for my dashboard. – nycynik Jul 26 '17 at 18:55
  • Hi @nycynik... i am trying to override default page of airflow.. where are you getting the app object to pass in Admin constructor ? I have posted a question as well here https://stackoverflow.com/questions/60864142/override-airflows-default-admin-index-page – user3531900 Mar 30 '20 at 10:03
  • The app is the Flask app `Parameters: app – Flask application instance` when you first init your Flask app, it returns the app instance. – nycynik Mar 31 '20 at 15:16
  • app instance is already created when airflow starts... i am trying to override the default admin index page..so i will need the same app instance which was created at the starting... So not sure how to get that same app instance – user3531900 Mar 31 '20 at 15:33
2

According to flask-admin documentation use this:

from flask_admin import BaseView, expose

class AnalyticsView(BaseView):
    @expose('/')
    def index(self):
        return self.render('analytics_index.html', args=args)

admin.add_view(AnalyticsView(name='Analytics', endpoint='analytics'))
Vahid Kharazi
  • 5,723
  • 17
  • 60
  • 103