35
loading...
This website collects cookies to deliver better user experience
#
for single line and """
for multi-line commentsEvery expert was once a beginner.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World, dear Friend!'
@app.route('/page/<int:page_num>')
def content(page_num):
return f'<h1>Yoo.. It is your page {page_num}</h1>'
if __name__ == '__main__':
app.run(host='',port='',debug=True)
export FLASK_APP=app.py
flask run
export command
above. flask run
export command
are known as environment variables. These variables are used by your flask application to serve your project. from flask import Flask
app = Flask(__name__)
app.config[MYSQL_USER]="your_username"
app.config[MYSQL_HOST]="localhost"
app.config[MYSQL_DB]="my_appdb"
app.config[MYSQL_PASSWORD]="your_password"
@app.route('/')
def index():
return {'Message': 'Hello World'}
export command
. Am going to help you do just that.pip install python-dotenv
touch .env
touch .flaskenv
//this is the .flaskenv file
FLASK_ENV - Controls the environment.
FLASK_DEBUG - Enables debug mode.
FLASK_RUN_EXTRA_FILES - A list of files that will be watched by the re-loader in addition to the Python modules.
FLASK_RUN_HOST - The host you want to bind your app to.
FLASK_RUN_PORT - The port you want to use.
FLASK_RUN_CERT - A certificate file for so your app can be run with HTTPS.
FLASK_RUN_KEY - The key file for your cert.
FLASK_APP=app.py
//This is the .env file
MYSQL_USER=root
MYSQL_PASSWORD=my_mysql_password
MYSQL_DB=userdb
MYSQL_HOST=localhost
SECRET_KEY=topsecretkey
API_KEY=donotsharethisapikeywithanyone
settings.py
file)//This is the settings.py file
from os import environ
SECRET_KEY=environ.get('SECRET_KEY)
API_KEY=environ.get('API_KEY')
MYSQL_USER=environ.get('MYSQL_USER')
//add any more variables you have
app.py
, the file that has your flask object.app.config.from_pyfile('settings.py')
# loading all environment variables from settings.py
from_pyfile
sub-method, the app will have access to the secretly defined variables in a secure way.