How to add content to the index page using Flask-Admin
Asked Answered
A

2

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?

Aland answered 23/3, 2016 at 2:1 Comment(0)
A
17

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
Aland answered 24/3, 2016 at 12:49 Comment(5)
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.Aland
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 #60864642Emulate
The app is the Flask app Parameters: app – Flask application instance when you first init your Flask app, it returns the app instance.Aland
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 instanceEmulate
Create your own template in "templates" folder, extend it like {% extends 'admin/master.html' %} then return self.render('mytemplate.html'), but nice to see this shorter way.Subcartilaginous
J
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'))
Jolandajolanta answered 23/3, 2016 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.