Create a new directory called flask_blog within the python-scripts folder on the Desktop.
mkdir Desktop/flask_blog
Create a venv in that directory,
python -m venv venv
to activate on Mac
source ./venv/bin/activate
To activate on windows I think double check other lessons
./venv/scripts/activate
install flask in venv
with pip
pip install flask
Run
python
Then run,
import flask
if no error show, flask is installed correctly
This can be done in vs code or the command prompt
Open the directory is vscode
and make a script called
flaskblog.py
Go to the flask.com website copy and paste the simple app into the script
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>Hello, World!</p>"
The __name__
is the same as __main__
which contains the static
variables relating to flask.
@app.route("/")
is a decorator that allows the function below to run
on a website, it's complicated ('go back and learn later'),
the /
is the root page of the site.
Copy and paste this code into your flask.py
script
if __name__ == '__main':
& @app.route('/')
set FLASK_APP
& flask run
set FLASK_APP=flaskblog.py
export FLASK_APP=flaskblog.py
Then try,
flask run
See next step, make sure you run script with code and make sure
you import flask
into
venv
as seen in Part 1 - step 4
set FLASK_APP=flaskblog.py
and flask run
Don't work.
**Remember Just add the line of code below at the end of your script to skip using the steps above to see changes on page
if __name__ == '__main__': app.run(debug=True)
** Also if you get any port errors just close liver servers on any vs code sessions and reopen the flask project again in vs code.
The above doesn't work because you must be sure the code below is in the script, run the script, then copy the link into a browser
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>Hello, World!</p>" if __name__ == '__main__': app.run(debug=true)
Run the script and copy paste link into browser
Copy and paste the url from above into a browser and you will see the return value in the flaskblog.py script
http://127.0.0.1:5000
We can replace the http://127.0.0.1:5000
with localhost
http://localhost:5000
set FLASK_DEBUG=1
Whenever changing the HTML like h1
you must stop the server in command prompt, then repeat the command
flask run
Then reload the page in the web browser
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<h1>Hello, World!</h1>\n<p>Somehitng sdj</p>"
the above is a huge pain in the ass , so do the following so you don't have to shut down the server and restart
Quit the server and set a new enviorment variable Debug
set FLASK_DEBUG=1
Then re-run the flask run
command
To avoid setting up the enviorment variables, this is another reason why we use
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<h1>Yo </h1>"
if __name__ == '__main__':
app.run(debug=True)
now we can run the script and refresh the page in the broswer to see the changes
@app.route
Add another decorator and function below for /about
.
Then in The url change the URL at the end by adding /about
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<h1>Home Page</h1>'
@app.route('/about')
def about():
return '<h1>About</h1>'
if __name__ == '__main__':
app.run(debug=True)
We can add multiple decorators for the same page
from flask import Flask
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def home():
return '<h1>Home</h1>'
@app.route('/about')
def about():
return '<h1>About</h1>'
if __name__ == '__main__':
app.run(debug=True)