Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ljgww opened this issue Apr 24, 2024 · 1 comment

Comments

@ljgww
Copy link

ljgww commented Apr 24, 2024

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
Copy link
Author

ljgww commented Apr 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant