How to share the global app object in flask?

后端 未结 6 667
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 16:28

I am using flask and trying to the following.

I have defined a main.py file through which I want to run my app ie python main.py -

from flask import Fla         


        
相关标签:
6条回答
  • 2021-01-30 17:01

    You can import current_app from flask. It stores a reference to the global application object.

    from flask import current_app as app
    
    def home():
        return render_template('base.html', name=app.name)
    
    0 讨论(0)
  • 2021-01-30 17:06

    First, I would suggest to take a look at Blueprints http://flask.pocoo.org/docs/blueprints/ This will help to organize the app easily.

    Also take a look at http://flask.pocoo.org/docs/api/#flask.current_app flask.current_app, the way how to get your app instance in other modules.

    This link also could be helpful on how to organize and build flask app (it is not ideal for sure, but can give you some ideas) - Large-app-how-to.md

    Have fun :)

    0 讨论(0)
  • 2021-01-30 17:08

    If you have a file AppName.py in which you define app, and then you have another file Foobar.py that needs it, you can always say in AppName.py:

    import Foobar
    Foobar.app = app
    

    Then in Foobar.py you should be able to use app in your functions. One thing you want to be careful of is that you can't have code in Foobar.py that runs immediately when the file is called the depends on the app variable which is passed in after the import.

    0 讨论(0)
  • 2021-01-30 17:18

    One way is to create an overall package and adding a __init__.py file under that where you declare all global variables. In your case for example, you can create something like:

    myapplication/
        *        __init__.py
        *        myviews/
            *         __init__.py
            *         view.py
            *         tags.py
    

    etc

    Now you add the following code in the __init__.py file:

    app = Flask(__name__)
    

    You can now use this app variable anywhere as long as you import the package myapplication.

    import myapplication.myviews.view
    
    0 讨论(0)
  • 2021-01-30 17:24

    Just import it from your other files. Perhaps the best way to do this is to put your app object in one single file and have everything else import from it.

    For example, your main.py could still have:

    from flask import Flask
    from view import tags
    
    app = Flask(__name__)
    

    And then in other files, you could do:

    from .main import app
    

    or, if outside your package, just use the complete string

    from mypackagename.main import app
    

    One thing to be careful of is circular imports. The easiest way to handle this issue is to create your app first and then import whatever else you need to from your base file after you create it.

    So for example:

    from flask import Flask
    app = Flask(__name__)
    
    # do some stuff with app
    from .views import view1, view2
    from .server import run
    

    So long as you put the imports after you've created app, like the above, you shouldn't have an issue with circular imports.

    0 讨论(0)
  • 2021-01-30 17:25

    Regarding import and use of current_app from flask in a "helper" python function in a separate source file, this works as long as a current app context has already been set up (E.g. web request received). I have a case where, during application initialization (app.run not yet invoked), app.logger is invoked in the helper function.

    Before I fixed it (see below), I got a stack trace punctuated by "RuntimeError: Working outside of application context".

    Sample solution:
    main.py:

    import helper
    ...
    app = Flask(__name__.split('.')[0],
            template_folder="templates",
            static_folder="static")
    ... 
    # Fix: Create an app context
    with app.app_context():
        helper.dbopen(...)
    ...
    app.run(...)
    

    helper.py:

    from flask import current_app as app
    ...
    def dbopen():
        app.logger.info(...)
        ...
    
    0 讨论(0)
提交回复
热议问题