LESSON 6

 

Running your Flask app

Lesson 6

Screen 1 of 14

Course progress review

This is lesson 6 of 8

 

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

Lesson 6 outcomes

This lesson prepares you to:

 

  • Start a Flask 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

Lesson 6

Screen 3 of 14

  • have an open command prompt window (cmd)
  • have your virtual environment activated
  • your cmd is open in the directory

       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

Requirements

This lesson requires that you:

Recall the code you wrote Lesson 5:

Start a Python application

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 

Start your Flask app 1/2

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)

Start your Flask app 2/2

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.

  • It is convenient for development, but it is not designed to handle a high volume of HTTP requests or large amount of data (called 'traffic').
  • By default, Flask's built-in server is made to serve one HTTP request at-a-time.
  • A fully featured Web server (e.g. Apache, nGinx) should be used for Flask Web apps in production environments.

Connect to the web server

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.

Stop the application

  • Select the cmd window running your Flask application.  Press the keys "ctrl + c" to stop your app.

Lesson 6

Screen 9 of 14

  • You can deactivate your venv by typing "deactivate" into cmd and presssing return (as shown).

To recativate 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>

Now it is your turn

Please perform the following steps before you proceed to lesson 7:

 

  1. Start the app you created in lesson 5
  2. Open a Web browser
  3. Goto the URL http://127.0.0.1:5000
  4. Stop your app
  5. Deactivate your virtual environment

 

Refer to the previous slides if necessary.

Lesson 6

Screen 10 of 14

Running Flask quiz

Lesson 6

Screen 11 of 14

Complete the quiz to test your knowledge of running a Flask app. Move forward to reveal the anwsers.

Fill in the missing word (A, B or C).  The Python ____ converts code to something  a computer understands, when you run a Python file

Fill in the missing word (A, B or C). To stop a Flask application, you select the running cmd window and  ___.

Which one of the following statements is true (A or B)?

Flask's built-in Web server:

A. Web server    B. Package    C. Interpreter

A. press ctrl + c    B. press quit        C.  press exit

A. is for production    B. runs on port 5000

1.

2.

3.

C. Interpreter

A. press ctrl + c

B. runs on port 5000

Lesson 6 review

You have completed Lesson 6 on Running your Flask app.  You can now:

 

  • Start a Python application from the command prompt
  • Run a local Flask app
  • Connect a Web browser to Flask's built-in Web server
  • Stop a Flask application

Lesson 6

Screen 12 of 14

Next lesson

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

Useful resources

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

Copy of Lesson 6

By mar5us

Copy of Lesson 6

First Flask App on PythonAnywhere Lesson 6

  • 40