Lesson 6
Screen 1 of 14
1) Components of a Web application
2) Web frameworks and Flask
3) Installing Python and packages
4) Creating your development environment
5) Building your first local Flask app
6) Running your first Flask app
7) Web application deployment
8) Deploying to PythonAnywhere
Lesson 6
Screen 2 of 14
This lesson prepares you to:
Lesson 6
Screen 3 of 14
c:\~\myproject\venv\Scripts
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Lesson 6
Screen 4 of 14
This lesson requires that you:
Recall the code you wrote Lesson 5:
Lesson 6
Screen 5 of 14
Python is an interpreted language. This means, when you run a Python file, the Python interpreter converts the code inside the file into something your computer can understand. This is why the process is called 'interpretation'.
To interpret a Python file sucessfully, the Python interpreter needs to know the location of the file you want to run.
The following command tells the interpreter (on the left) where your "app.py" is (on the right). Type the command into cmd and press return:
(venv) C:\Python32\python.exe C:\~\myproject\app\app.py
Use the cd command in cmd to change into your project's app directory "c:\~\myproject\app\" by typing the following, then press return:
Lesson 6
Screen 6 of 14
The message in cmd (shown below) indicates your Flask app is npw running on Flask's, built-in development Web server on port 5000.
(venv) cd C:\~\myproject\app
In cmd, tell Python to run the file "app.py" by typing the following command, then press return:
(venv) python ./app.py
(venv) * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Some important things to note about Flask's, built-in, development Web server, from Flask's Developers, include (Ronacher 2018b):
Lesson 6
Screen 7 of 14
Flask's built-in Web server is 'lightweight and easy to use', but it is 'not suitable for production' environments.
To verify that your Flask app is running, open a web browser and goto the URL http://127.0.0.1:5000/
Lesson 6
Screen 8 of 14
You can see the "Hello World!" message, displayed in your Web's browser window, sent from your Flask Web app.
Lesson 6
Screen 9 of 14
To reativate your venv, using the command "cd" in cmd to change to the location "c:\~\myproject\venv\Scripts\activate.bat". Enter the command "activate.bat" in cmd and press return.
(venv) C:\~\myproject\app>python ./app.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [00/Mar/2000 00:00:00] "GET / HTTP/1.1" 200 -
(venv) C:\~\myproject\app>deactivate
C:\~\myproject\app>
Please perform the following steps before you proceed to Lesson 7.
Refer to the previous slides if necessary.
Lesson 6
Screen 10 of 14
Lesson 6
Screen 11 of 14
Complete the quiz to test your knowledge of running a Flask app.
You have completed Lesson 6 on Running your Flask app. You can now:
Lesson 6
Screen 12 of 14
In Lesson 7 you will learn about hosting your Flask Web application online.
After you have reviewed the useful resources on the next screen, proceed to Lesson 7.
Lesson 6
Screen 13 of 14
You can learn more about Flask's built-in, development Web server from the 'Deployment options' section of the online Flask documentation (Ronacher 2018b).
Lesson 6
Screen 14 of 14