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 app.py. 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
app.run(host='0.0.0.0', 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 app.py 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. '0.0.0.0') 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__)
@app.route('/')
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
app.run(host='0.0.0.0', 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:
python app.py
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.