How to cache Flask-App-Builder views?

738 views Asked by At

I have a working Flask app with pretty extensive SQLite database (~100k entries). I'm struggling to bootstrap some ModelViews with Flask-App-Builder (FAB) in simplest way according to docs:

class MyModelView(ModelView):
  datamodel = SQLAInterface(MyModel)
  list_columns = ['col1', 'col2']

than enabling them like this:

appbuilder.add_view(MyModelView, 'Models')

It works fine. The problem is, every time I visit list view, it is generated from scratch - DB query, some FAB processing, template generation. And it is subjectively very slow (tens of seconds by wall clock; I'm not sure how to properly profile Flask application).

I think caching FAB views at some point may help speed up things, but I'm unsure how to go about it. For ordinary views I use Flask-Cache:

@app.route('/some_route')
@cache.cached()
def view_function():
    data = time_consuming_operation()
    return render_template('template.html', data=data)

So, question:

How one would use cache with FAB ModelViews? Is there a way to speed up work of FAB ModelViews?

Thank you!

Result

Ok, after exploring FAB source I see that it is possible to redefine methods of ModelView or it's parent to get desired behavior (in my case - limiting query for List view and caching it with Flask-Cache). However it will take a good deal of code and that defies whole purpose of using FAB. As a result I moved to Flask-Admin. It has simpler look and does not include for instance in-built authentication, but it works so much faster in my case with so little configuration - it solved my issue.

I still recommend to give FAB a try in case of small amount of data (~1000 entries) it is richer in features and it's further development might give us a really neat product.

By the way, still I haven't found a way to cache of FAB ModelView response, but for me for now this question is closed.

0

There are 0 answers