简体   繁体   English

有没有办法在配置文件中配置烧瓶应用程序端口?

[英]Is there a way to config flask app port in config file?

I'm learning Python Flask and I'm codding a simple web app with comments section, login and create user section.我正在学习 Python Flask,我正在编写一个带有评论部分、登录和创建用户部分的简单网络应用程序。 I'm stuck after a few lessons.几节课后我被困住了。 I've created a config.py file to add all the app configs inside it like ENV name, DEBUG, SECRET_KEY but I don't find any way to set-up port number in this file.我创建了一个 config.py 文件来在其中添加所有应用程序配置,如 ENV 名称、DEBUG、SECRET_KEY,但我没有找到任何方法来在此文件中设置端口号。 I write in the config class PORT = 8000 but my app doesn't recognise this, I have to set-up it in run.py file "app.run(port = 8000)".我在配置类 PORT = 8000 中写入,但我的应用程序无法识别这一点,我必须在 run.py 文件“app.run(port = 8000)”中设置它。

Do you have any idea?你有什么主意吗? Thanks :)谢谢 :)

Config.py:配置文件:

import os

class Config(object):
    SECRET_KEY = 'secretkeyforsessions'

class DevelopmentConfig(Config):
    #PORT = 8000
    #port = 8000
    ENV = "development"
    DEBUG = True
    SQLAlCHEMY_DATABASE_URI = "mysql://root:root@localhost/flask"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

run.py:运行.py:

if __name__ == '__main__': 
    csrf.init_app(app)
    """
    db.init_app(app)
    with app.app_context():
        db.create_all() # Se encarga de crear todas las tablas que no sean creadas
    """
    app.run(port = 8000)

Flask offers app.config.from_object('...a config file') https://flask.palletsprojects.com/en/1.1.x/config/#configuring-from-files Flask 提供app.config.from_object('...a config file') https://flask.palletsprojects.com/en/1.1.x/config/#configuring-from-files

# config.py
ENV = "development"
PORT = "8000"
DEBUG = True

SECRET_KEY = 'secretkeyforsessions'

SQLAlCHEMY_DATABASE_URI = "mysql://root:root@localhost/flask"
SQLALCHEMY_TRACK_MODIFICATIONS = False

and

# run.py
# ...
import config

app.config.from_object(config)
print(app.config)

app.run(port=config.PORT)

# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM