\n.../scripts/mail_stuff.py\n\n
For example:
\nfrom flask import Flask\nfrom flask_mail import Mail, Message\napp = Flask(__name__)\napp.config["DEBUG"] = True\napp.secret_key='secret_key'\napp.config['MAIL_SERVER'] = "smtp.office365.com"\napp.config['MAIL_PORT'] = 587\napp.config['MAIL_USE_TLS'] = True\napp.config['MAIL_USERNAME'] = 'username'\napp.config['MAIL_PASSWORD'] = 'password'\nmail = Mail(app)\n
\n","author":{"@type":"Person","name":"Brad123"},"upvoteCount":1,"answerCount":1,"acceptedAnswer":null}}Reputation: 944
I'm trying save and pass 'app' data to other python scripts. Currently I'm just copying and pasting the same code into relevant python scripts, but I was wondering if I could have 1 copy where I import it to other python scripts.
Directory layout:
For example:
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config["DEBUG"] = True
app.secret_key='secret_key'
app.config['MAIL_SERVER'] = "smtp.office365.com"
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'username'
app.config['MAIL_PASSWORD'] = 'password'
mail = Mail(app)
Upvotes: 1
Views: 334
Reputation: 419
When initializing the flask app set your configuration variables through a class object, using the config.from_object method, instead of direct assignment. Then if you need that config data in other files you just import the Config object and you can get the data you need. This method allows you to decouple the config variables from the application object.
(flask_app.py file)
app = Flask(__name__)
app.config.from_object('config.Config')
(config.py file)
class Config:
MAIL_SERVER = "smtp.office365.com"
MAIL_PORT = 587
(mail_stuff.py file)
from config import Config
my_new_mail_port_variable = Config.MAIL_PORT
Upvotes: 1