coreyShafer

1 - Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started

tutorial

Part 1 - Create venv & Install flask

01

Create a new directory called flask_blog within the python-scripts folder on the Desktop.

mkdir Desktop/flask_blog

02

create and activate

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

03

install flask in venv with pip

pip install flask

04

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

05

Open the directory is vscode and make a script called
flaskblog.py

Part 2 - Visit the Flask website & copy code

01

Go to the flask.com website copy and paste the simple app into the script

02

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

Part 3 - if __name__ == '__main': & @app.route('/')

set FLASK_APP & flask run

01

On Windows
set FLASK_APP=flaskblog.py

On MAC

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

02

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.

03

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

04

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

05

We can replace the http://127.0.0.1:5000 with localhost

http://localhost:5000

Part 4 Change HTML set FLASK_DEBUG=1

01

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>"

02

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

Windows

set FLASK_DEBUG=1

Then re-run the flask run command

03

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

Part 5 - add another route @app.route

01

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)
    

02

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)