Home>

How do I modify xxx.py to use the constant string set in the config file in app.config?

init.py

def init_db (app):
    db.init_app (app)
    Migrate (app, db)

def create_app (config_name):
    app = Flask (__ name__)
    app.config.from_object (config [config_name])
return app


xxx.py

import create_app
impoet modulename
x = app.config ["TOKEN"]
...
...
...


config.py

Class config:
   TOKEN = 'token'
   ...
   ...
  • Answer # 1

    From flask import current_app and currnt_app.config [""] can be accessed by writing it in a function instead of globally.

  • Answer # 2

    How to useapp.config ["TOKEN"]shouldn't be a problem, so if that does n’t work, there ’s something wrong elsewhere. I think.
    (In fact, from the sources cited here,app.config.from_object (config [config_name])isn't sure that the settings would have been done properly. strong>)


    import create_app # Since it is a module that can be written here, it cannot be the create_app function in __init__.py
    impoet modulename
    x = app.config ["TOKEN"] # The quoted source has no part that is bound or imported to the name app

    It looks like you are importing amodulenamed

    create_app, but thecreate_appin the code above is aname defined in the moduleso it should be something else. What are you importing here?

    Where did thisappcome from?


    def create_app (config_name):
        app = Flask (__ name__)
        app.config.from_object (config [config_name]) # The quoted source has no parts that are bound or imported to the names config and config_name
        return app # Maybe

    Where did thisconfigorconfig_namecome from?

    I haven't returned myapp, but just throw it away?


    Everything

    is a mysterious code, so I can't answer it.
    I just think this isn't working code.
    Maybe it's a problem when quoting ...


    Before I wrote a note that I checked whatconfig.from_objectdoes.

    If the argument is a string

    First convert:'to..

    __ import __ (argument)and check if it succeeds. If it succeeds (because it is loaded), returnsys.modules [argument]and finish.

    Split with the first., the first part is the module name and the second part is the object name.

    First,__ import __ (module name, None, None, [object name])If it fails,__ import __ (module name). If either succeeds, the module can be retrieved andgetattr (module, object name)to retrieve the object.

    Pass the returned value to the dir function to extract the attribute.

    If the argument is not a string, pass the argument itself to dir to retrieve the namespace attribute.

    Select only attribute names that are all uppercase from the extracted attributes (exactlyisupper ()returns true) and import them into the config as keys/values.

    Did you understand this?


    That means that this config.py is located in theproject root (same level as the running script),

    app.config.from_object ('config.config')
    If

    is running, it will be included in config.


    If the other party toapp.config.from_objectdynamically changes depending on the environment, what is actually read infrom_objecthere as an argument? I think it's a good idea to check and see if the settings can be taken out immediately.
    You will also see ifapp.config.from_objectis called.