This course will become read-only in the near future. Tell us at if that is a problem.

Configure Your App

Construct the basic framework for your Flask app

Finally, we get to write some Python.

I'm stoked.  Are you stoked?

As we indicated earlier in the challenge, we are going to be using a popular web microframework called Flask to write our first Python website.  Now we can go ahead and get started by writing our first Flask app.  

1) Install Flask

First thing we need to do is install the Flask module in our Python environment.  This is easily accomplished using pip, a tool for installing Python packages and modules.  With pip installed, we can just type the following in our terminal window:

pip install flask

2) Create Our App

The advantage of Flask for our first application is that we can contain most of our application in a single file which we will call We'll open the file in a text editor and add a little boilerplate code to make our first app go:

from flask import Flask
import os

app = Flask(__name__)

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))

    if port == 5000:
        app.debug = True'', port=port)

Let's take a bit and look at what is going on here.  First, we import two modules that we need to run our app - one for the Flask framework we are using and another for operating system ("os") which we'll use later.  Next, we define a Flask app using the name of the module we are creating ("app").  

Next we define a chunk of code to run if is executed directly by Python (e.g. when the __name__ variable is "__main__").  In this chunk of code, we define a TCP port to run our Flask app on with the default being 5000.  If the port is 5000, we then assume we are running the app on our computer instead of Heroku and thusly turn on debug mode.  Then at the end, we run the app using localhost (a.k.a. '') as the host and the port we get at the beginning of the block at the port.

3) Add Our First Controller

Next we'll add a quick message to make sure our application is working.  To do this, we'll create a default route to our app with a simple function that returns your answer to this timeless joke: "Why did the robot cross the road."  Here's an example of what I came up with:

from flask import Flask
import os

app = Flask(__name__)

def index():
    return "To conquer the human civilization on the other side."

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))

    if port == 5000:
        app.debug = True'', port=port)

So what is going on here?  We added three new lines of code after we define our Flask app with the name app.  We use a Flask decorator to define our route, then follow it with a function that return a string that is the answer to the joke we posed at the beginning of the step.

4) See If It Works

Let's see if our code works.  To do this, save the file and run the following command in Python:


We should now see our joke when we open a web browser to http://localhost:5000. If not, we should see a descriptive error message.

5) Share With Your Peers

Share the result of your work with your peers.  Post a screenshot of your punchline successfully working in your browser.  For extra bonus points, commit the new file to source control and post a copy of your git log.

Remember your email address may appear in your git log - be sure to scrub.

Task Discussion

  • gushan said:

    $ python
     * Restarting with stat
     * Debugger is active!
     * Running on (Press CTRL+C to quit) - - [04/May/2016 11:34:01] "GET / HTTP/1.1" 200 - - - [04/May/2016 11:34:01] "GET /favicon.ico HTTP/1.1" 404 - - - [04/May/2016 11:34:01] "GET /favicon.ico HTTP/1.1" 404 -

    on May 4, 2016, 12:32 a.m.
  • Fer said:

    er@debian:~/webpython$ python
     * Running on
     * Restarting with reloader - - [14/Nov/2013 13:13:50] "GET / HTTP/1.1" 200 - - - [14/Nov/2013 13:13:51] "GET /favicon.ico HTTP/1.1" 404 - - - [14/Nov/2013 13:13:51] "GET /favicon.ico HTTP/1.1" 404 -

    on Nov. 14, 2013, 7:20 a.m.
  • joepep said:


    commit 5b8496c1c16efdf5d5b89cda149f2c024a743ede
    Author: nana <nana@olympus.(none)>
    Date:   Mon Jun 10 13:47:52 2013 +0000
        Created using Flask
    commit 0f89c413f7426f22549ffd397a8f8cce7d31f08d
    Author: Joepep
    Date:   Mon Jun 10 12:49:20 2013 +0000
        Added a README file
    on June 10, 2013, 9:49 a.m.
  • Wouter van den Bos said:


    Author: Wouter van den Bos <>
    Date:   Fri Feb 22 13:14:59 2013 -0800
        Added a .py  file
    commit 7570ab681729d9650e3a9b74eb3a9e763fb9f830
    Author: Wouter van den Bos <>
    Date:   Fri Feb 22 00:02:05 2013 -0800
        Added a README file
    on Feb. 22, 2013, 4:16 p.m.
  • saravanan said:

    commit f24ee5dc470e17ef30314ab2ba9585aac4b6b159
    Author: saravanan <>
    Date:   Thu Aug 9 15:18:09 2012 +0530 is added

    commit 91a49c5b241ba476bfede059c02754cf88399583
    Author: magi <magi@magi-Satellite-C640.(none)>
    Date:   Mon Aug 6 11:12:47 2012 +0530

        Added a README file

    on Aug. 9, 2012, 6:17 a.m.