How to import from config file in Flask?

前端 未结 3 873
自闭症患者
自闭症患者 2020-12-23 02:13

I have followed the layout of my Flask project from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world.

I have the following structure:

相关标签:
3条回答
  • 2020-12-23 02:32

    I created a config.json file in my flask project - root folder, like below:

    config.json

    {
        "mail_settings":{
            "MAIL_SERVER": "smtp.gmail.com",
            "MAIL_PORT": 465,
            "MAIL_USE_TLS": "False",
            "MAIL_USE_SSL": "True",
            "MAIL_USERNAME": "your_mail@gmail.com",
            "MAIL_PASSWORD": "your_password"
        },
        "database":{
            "MYSQL_HOST":"localhost",
            "MYSQL_USER": "user_name",
            "MYSQL_PASSWORD":"password",
            "MYSQL_DB":"database_name"
        }
    }
    

    and just add below code in where we want to access configuration values. for example, i am going to use in app.py like below:

    first import flask json library:

    import json
    

    second open the file to read and store the json in some variable like below:

    with open('config.json') as config_file:
        config_data = json.load(config_file)
    

    add below code after creates a Flask application, named app app = Flask(__name__) like below, for access the config.json - configuration values.

    # mail configuration
    mail_settings = config_data['mail_settings']
    app.config.update(mail_settings)
    
    # database configuration
    db_settings = config_data['database']
    app.config.update(db_settings)
    
    0 讨论(0)
  • 2020-12-23 02:49

    When people talk about configs in Flask, they are generally talking about loading values into the app's configuration. In your above example you could have something like app.config.from_object('config') in your init.py file. Then all the configuration values will be loaded into the app.config dictionary.

    Then in any of your files you could just import the app object to gain access to that dictionary. I tend to access that app object by doing from flask import current_app as app then just app.config['MY_SETTING'] to get the value I care about. Read up more in the documenation.

    0 讨论(0)
  • 2020-12-23 02:50

    After a little bit of fiddling (and a little help from the 'net), I could improve this further, by changing the code to include the config to:

    app.config.from_object('config.ProductionConfig')
    

    This enables this cool pattern for configurations:

    class Config(object):
        DEBUG = True
        DEVELOPMENT = True
        SECRET_KEY = 'do-i-really-need-this'
        FLASK_HTPASSWD_PATH = '/secret/.htpasswd'
        FLASK_SECRET = SECRET_KEY
        DB_HOST = 'database' # a docker link
    
    class ProductionConfig(Config):
        DEVELOPMENT = False
        DEBUG = False
        DB_HOST = 'my.production.database' # not a docker link
    

    What's left now is to see how to integrate testing configs into this, but at least it feels less clumsy.

    0 讨论(0)
提交回复
热议问题