dpgaspar / Flask-AppBuilder

Simple and rapid application development framework, built on top of Flask. includes detailed security, auto CRUD generation for your models, google charts and much more. Demo (login with guest/welcome) - http://flaskappbuilder.pythonanywhere.com/
BSD 3-Clause "New" or "Revised" License
4.7k stars 1.36k forks source link

adding back-end code and passing 'calculated' value to a view template - how to? #2230

Open ljgww opened 7 months ago

ljgww commented 7 months ago

This is not bug report, it's cry for a how-to explanation, best practice or recommended way of doing things in FAB.

Must admit framework looks great, and all system with models and views looks nice and dandy and there is a lot of effort making all these things together working, but, I miss (cannot discover or figure out) how to do a very simple thing in FAB (I know how to do it with template rendering in flask - see example below)

I was toying with https://flask-appbuilder.readthedocs.io/en/latest/customizing.html#changing-the-index explanation and made working template and binds the execution, all working fine.

However, I god stuck on something that I could call a 'calculated' value.

Class as taken from the documentation, says as follows:

    class MyIndexView(IndexView):
        index_template = 'my_index.html'

so, how I do add 'controller' to the class (as in model/view/controller pattern) - essentially add code in this class using FAB?

for example I want variable:

a = 5

first - i do not understand where do I put code to define the variable - presumably it would be some method - which one? How to bind method to execution?

Looked at @actions but that seems to be actions connected to a 'record' in a "model" - I do not have record - my var is 'calculated' value.

and then, I want to pass variable 'a' to my template (my_index.html) so that i can use it as {{a}} in the resulted front end and it will show '5' on the page.

Say that specified variable does not come from database/model, whatever - it is populated by getting information from other source or calculated in some way.

So how I do create 'controller' (add function and back-end code to this view) and pass values to a 'view' template?

Some sample code would be greatly appreciated (also it would be nice to have that in the documentation - maybe there is, but I failed to find or understand)

in flask this is simply done as:

    @app.route("/try")
    def trt():
        a = 5
        flask.render_template('my_index.html', a=a)

I am sure that there is some simple way to do this in FAB too.

Environment

Flask-Appbuilder version: latest I presume (on Linux Ubuntu)

ljgww commented 7 months ago

You may leave, close or discard the 'issue/request'. Found example in the part of the manual called: 'Read the source, Luke'

passing arguments to the template: method3 in: https://github.com/dpgaspar/Flask-AppBuilder/blob/master/examples/simpleview2/app/views.py code lines 25 - 31