assign a variable into `g` once and only once for application in flask

  • Last Update :
  • Techknowledgy :

g is the global object for that request only. Have you considered using a caching mechanism?

> pip search flask | grep "cache" | sort
Flask - Cache - Adds cache support to your Flask application
Flask - Cache - PyLibMC - PyLibMC cache
for Flask - Cache, supports multiple operations
Flask - Memsessions - A simple extension to drop in memcached session support
for Flask.

Can you try putting that function call in __init__.py of your project package? I usually use __init__.py to initialize the app = Flask(__name__) etc. So lets say you can try the following:

# __init__.py:

   from flask
import Flask
from somemodule
import take_long_time_to_do
app = Flask(__name__)
rec = take_long_time_to_do()

Then you can use the rec variable in any views as long as you import it:

# views.py

from myproject
import rec
@app.route('/test/')
def test():
   return render_template('index.html', var_rec = rec)

Suggestion : 2

The built-in developmental web server provided by Flask is not meant for use in production. I will show you how to run a Flask app under Apache web server later.,You can register more than one URLs to the same route handler (or view function). For example, modify hello_flask.py as follows:,The app.run() launches the Flask's built-in development web server. It then waits for requests from clients, and handles the requests via the routes and route handlers.,Flask handles HTTP requests via the so-called routes. The decorator @app.route(url) registers the decorated function as the route handler (or view function) for the url.

.wsgi
sys.path.append('/path/to/eclipse/plugins/org.python.pydev_x.x.x.x/pysrc')
WSGIDaemonProcess myflaskapp user = flaskuser group = www - data threads = 5 python - path = /path/to / eclipse / plugins / org.python.pydev_x.x.x.x / pysrc
/etc/environment
PYTHONPATH = '/path/to/eclipse/plugins/org.python.pydev_x.x.x.x/pysrc'
True
app = Flask(__name__)
app.debug = True # Enable reloader and debugger
   ......

   if __name__ == '__main__':
   app.run()
app.run()
app = Flask(__name__)
   ......
   if __name__ == '__main__':
   app.run(debug = True) # Enable reloader and debugger
 * Running on http: //127.0.0.1:5000/ (Press CTRL+C to quit)
    *
    Restarting with stat *
    Debugger is active!
    *
    Debugger pin code: xxx - xxx - xxx
    ......

Suggestion : 3

The FLASK_APP environment variable is the name of the module to import at flask run. In case that module is incorrectly named you will get an import error upon start (or if debug is enabled when you navigate to the application). It will tell you what it tried to import and why it failed.,To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:,The request object is documented in the API section and we will not cover it here in detail (see Request). Here is a broad overview of some of the most common operations. First of all you have to import it from the flask module:,To enable all development features (including debug mode) you can export the FLASK_ENV environment variable and set it to development before running the server:

from flask
import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello, World!'
$
export FLASK_APP = hello.py
$ flask run
   *
   Running on http: //127.0.0.1:5000/
C: \path\ to\ app > set FLASK_APP = hello.py
PS C: \path\ to\ app > $env: FLASK_APP = "hello.py"
$
export FLASK_APP = hello.py
$ python - m flask run *
   Running on http: //127.0.0.1:5000/
$ flask run--host = 0.0 .0 .0

Suggestion : 4

To share data that is valid for one request only from one function to another, a global variable is not good enough because it would break in threaded environments. Flask provides you with a special object that ensures it is only valid for the active request and that will return different values for each request. In a nutshell: it does the right thing, like it does for request and session.,Just store on this whatever you want. For example a database connection or the user that is currently logged in.,Starting with Flask 0.10 this is stored on the application context and no longer on the request context which means it becomes available if only the application context is bound and not yet a request. This is especially useful when combined with the 1.7   Faking Resources and Context pattern for testing.,Additionally as of 0.10 you can use the get() method to get an attribute or None (or the second argument) if it’s not set. These two usages are now equivalent:

user = getattr(flask.g, 'user', None)
user = flask.g.get('user', None)