UNIT C LESSON C2
Running your Flask app
Unit C Lesson C2
Screen 1 of 11
Course progress review
This is lesson 6 of 8
1) Installing Python
2) Creating a virtual environment
3) Installing Flask
4) Configuring Flask
5) Building your first local Flask app
6) Running your first Flask app
7) Understanding web servers
8) Deploying to PythonAnywhere
Unit C Lesson C2
Screen 2 of 11
Lesson outcomes
This lesson prepares you to:
- Start a Python application from the command prompt
- Start your first local Flask app
- Connect with a web browser to the Flask built-in web server
- Stop the Flask application
Unit C Lesson C2
Screen 3 of 11
- have an open command window
- have your virtual environment activated
- are in folder ~\myproject\venv\Scripts
Remember the code you have written in Lesson C1:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Unit C Lesson C2
Screen 4 of 11
From Unit C Lesson C1 you
Start a Python application
Unit C Lesson C2
Screen 5 of 11
Python is an interpreted language: When running a Python script or application, the interpreter converts the Python program into something that the computer can understand.
In Windows, the path of the Python program must be passed to the interpreter as an argument. Example:
C:\Python32\python.exe C:\pathto\myPythonApp.py
Start your Flask app 1/2
Change to folder c:\~\myproject\app
Run "python ./app.py":
Note the message *Running on http://127.0.0.1:5000/.
This message refers to the Flask built-in web server which is running on port 5000 by default.
(venv) C:\~\myproject\app>python ./app.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Unit C Lesson C2
Screen 6 of 11
Start your Flask app 2/2
You will learn more about web servers in general during the next lesson, but here are some important notes about the Flask built-in web server:
- The official Flask doc says that while lightweight and easy to use, the built-in server is not suitable for production.
- It is convenient for development but it doesn't scale well.
- By default it is single threaded, i.e. it can serve only one request at a time.
- It should be replaced (e.g. by Apache) for productive use.
Unit C Lesson C2
Screen 7 of 11
Connect to the web server
Open a web browser and connect to http://127.0.0.1:5000/ (or http://localhost:5000/)
Observe the "Hello World!" message inside the browser window.
Unit C Lesson C2
Screen 8 of 11
Stop the application
- Select the running command window and enter "ctrl +c"
- Deactivate your running venv by typing "deactivate"
Note that you can open your venv again by running "activate.bat"
(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>
Unit C Lesson C2
Screen 9 of 11
Lesson review
Having completed Unit C Lesson C2 you should now successfully be able to:
- Start a Python application from the command prompt
- Start a local Flask app
- Connect with a web browser to the Flask built-in web server
- Stop a Flask application
Unit C Lesson C2
Screen 10 of 11
Now it is your turn
Please perform the following steps before you proceed to Unit D:
- Start the app you created in lesson 1
- Open a web browser and connect to the Flask internal web server
- Stop the app and deactivate your virtual environment
Refer to the previous slides if necessary. Complete the assignment C2 once you are finished with above steps.
Unit C Lesson C2
Screen 11 of 11
Copy of UNIT C LESSON 2:
By dwjk
Copy of UNIT C LESSON 2:
- 40