Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share the global app object in flask?

Tags:

python

flask

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 Flask from view import tags  app = Flask(__name__)  @app.route('/') def hello_world():     return 'Hello World!'  if __name__ == '__main__':     app.run() 

I have defined a package named view in which I will be declaring my different view modules, each having its own routes. view.tags.py -

from flask import Flask app = Flask(__name__)  @app.route('/e') def hello_world2():     return 'Hello World!' 

So I need to have the global app object in my main.py for running the server, as well as in the view classes of my package for registering the routes. So how do I create the global app object and share it between all classes ?

Thanks, Murtaza

like image 681
murtaza52 Avatar asked Nov 08 '12 08:11

murtaza52


People also ask

How do you share a project Flask?

You can activate your project by double-clicking the script file created with the name of your main flask file and can also share the build folder with anyone, without worrying that whether they have installed essential libraries on their system or not.

How do I make my Flask app accessible?

Today, there are two ways to expose your flask application to the internet. Deploy the web application in your office server which has a public IP address and domain name. Deploy the web application in the cloud such as AWS, MS Azure, GCP or web hosting companies like GoDaddy, SiteGround, A2Hosting etc.

How do I export my Flask app?

Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .

Why is current_app imported from flask instead of flask globals?

You will often see current_app imported directly from flask instead of flask.globals. These imports are equivalent because flask is simply importing current_app from flask.globals. g , request , and session are several other callables with code examples from the same flask.globals package.

What is flask application context?

A corresponding application context is pushed when a request context is pushed. Purpose of the Context¶ The Flaskapplication object has attributes, such as config, that are useful to access within views and CLI commands. However, importing the appinstance within the modules in your project is prone to circular import issues.

What is G object in flask?

g is an object for storing data during the application context of a running Flask web app. g can also be imported directly from the flask module instead of flask.globals, so you will often see that shortcut in example code. current_app , request , and session are several other callables with code examples from the same flask.globals package.

What is the use of GObject in flask?

Flask provides the gobjectfor this purpose. It is a simple namespace object that has the same lifetime as an application context. Note The gname stands for “global”, but that is referring to the data being global within a context. The data on gis lost after the context ends, and it is not an appropriate place to store data between requests.


1 Answers

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) 
like image 130
Juan Odicio Avatar answered Sep 19 '22 05:09

Juan Odicio