Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add content to the index page using Flask-Admin

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?

like image 401
nycynik Avatar asked Mar 23 '16 02:03

nycynik


People also ask

Does Flask have an admin panel?

In a world of micro-services and APIs, Flask-Admin solves the boring problem of building an admin interface on top of an existing data model. With little effort, it lets you manage your web service's data through a user-friendly interface.


2 Answers

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
like image 74
nycynik Avatar answered Sep 30 '22 17:09

nycynik


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'))
like image 40
Vahid Kharazi Avatar answered Sep 30 '22 17:09

Vahid Kharazi