Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store environment variables in a Python Flask app?

I have a Python Flask app that accesses the Github API. For this I need to store an access token. What is the common practice to store that data and how do I access that inside my app?

from flask import Flask, request

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


@app.route('/',methods=['POST'])
def foo():
   ...
like image 645
ustroetz Avatar asked Feb 24 '15 13:02

ustroetz


People also ask

How do I set environment variables in Flask app?

Command-line: Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.


1 Answers

Flask has a custom context to store app variables:

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

You can use g object to store your variables:

from flask import g
g.github_token = 'secret'

And after initialization:

from flask import g
token = g.github_token
like image 165
Eugene Soldatov Avatar answered Sep 20 '22 04:09

Eugene Soldatov