How to store environment variables in a Python Flask application?

I have a Python Flask application that accesses the Github API. To do this, I need to save the access token. What is the common practice of storing this data and how do I access it in my application?

from flask import Flask, request

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/',methods=['POST'])
def foo():
   ...
+6
source share
2 answers

The easiest way is to put it in the configuration module (a regular python file .py), and then importuse it in your code, as suggested by this snippet on the Flask website.

+3
source

The flag has its own context for storing application variables:

http://flask.pocoo.org/docs/0.10/appcontext/

g- :

from flask import g
g.github_token = 'secret'

:

from flask import g
token = g.github_token
+5

All Articles