Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access app config variables in Flask app

Tags:

python

flask

I have a flask app that looks like this:

myapp
-- application.py
-- models.py
-- queries.py
-- routes.py
-- settings.py

application.py looks like this:

from flask import Flask
from myapp import routes

def create_app(config_object):

    app = Flask(__name__)
    app.config.from_object(config_object)

    app.register_blueprint(routes.main)

    return app

queries.py looks like this

from myapp.models import User

class QueryConnection(DATABASE_URI):
    self.engine = create_engine(DATABASE_URI)
    self.session = sessionmaker(bind=self.engine)
    self.Session = self.session()

    def get_all_users(self):
        return self.Session.query(User).all()

routes.py looks like this:

from flask import current_app, Blueprint, render_template
from myapp import queries
main = Blueprint('main', __name__, url_prefix='/')

query_connection = queries.QueryConnection(current_app.config['DATABASE_URI'])

@main.route("/")
def index():
    return render_template('index.jinja2', list=query_connection.get_all_users())

models.py looks like this:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id           = Column(Integer, primary_key=True)
    name         = Column(String)

I want to get the current_app.config['DATABASE_URI'] in routes.py so I can query the database but I don't know how to pass the app context from application.py to queries.py. I tried using this in application.py

with app.app_context():
    app.register_blueprint(main)

But I get the error "working outside of application context". Anything else gives the same error.

I want to encapsulate the queries in its own class so that I can pass the database uri in different contexts like in tests.

like image 531
pspencer Avatar asked Jan 17 '16 21:01

pspencer


People also ask

How do I access my config variable in Flask?

# app.py or app/__init__.py from flask import Flask app = Flask(__name__) app. config. from_object('config') # Now we can access the configuration variables via app. config["VAR_NAME"].

How do I use config in Flask?

Independent of how you load your config, there is a config object available which holds the loaded configuration values: The config attribute of the Flask object. This is the place where Flask itself puts certain configuration values and also where extensions can put their configuration values.

What is app config [' Secret_key ']?

SECRET_KEY: Flask "secret keys" are random strings used to encrypt sensitive user data, such as passwords. Encrypting data in Flask depends on the randomness of this string, which means decrypting the same data is as simple as getting a hold of this string's value.

How do I set environment variables in Flask?

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.

How do I use .ENV files in Flask?

env file inside the Python Flask application. Reads the key-value pair from the . env file and adds them to the environment variable. It is great for managing app settings during development and in production using 12-factor principles.


1 Answers

you need to import the file, from where you are calling the create_app method. My guess is that it is the application.py.

--- D_file.py

app = create_app(your_config)

--- queries.py

from D_file import app

DB_URI = app.config['DATABASE_URI']
like image 67
CESCO Avatar answered Dec 07 '22 07:12

CESCO