Python Flask Web Development: Hello World, & Flask Application Structure
Welcome to the Python Flask Web Development series! If you’re new, you can go to the very first post in this series over here to start from the very beginning.
To get started with Flask, the first thing we are going to build is a very simple “Hello World” application with Flask. I’ll also be going over how Flask works, and go a bit more into depth with how the request/response cycle works as well. In the following tutorial, we’ll be going over:
- How to Initialize an application with Flask
- Overview of the Hello World application
- How Route and View functions work in Flask
- Running your Server (using Flask’s built in server for development purposes)
How to Initialize an application with Flask
Table of Contents
When it comes to Flask applications, you have to always create an instance of the application in order to actually get started. You’re basically creating an instance of the ‘Flask’ class, which is going to be your “flask object” or application instance.
Also note, that the ‘__name__’ parameter that is passed into the Flask() function is an argument that determines what the root path of this application is going to be. The root path of this application tells Flask where all the resource files are going to be, which is relative to where the application is located. These resource files include other files that are needed for the application, such as view templates for your application. If you don’t know what view templates are, we are going to be covering them more in depth in the following series.
Overview of the Hello World application
Now we’re going to build our “Hello World” application. It’s all contained in a single file, and we’ll be breaking down each part into more detail. There’s a lot more going on in the background of this script, so it’s definitely very important we cover some theoretical ground in terms of what is going on with this simple example.
How Route and View functions work in Flask
Now if you don’t know exactly how requests are sent to web servers, and how web servers service a client’s requests, then this section is extremely important so you have a general idea of how Flask works in the background.
Communication between the client and host occurs through a request/response cycle. The client (web browser) sends a request to the web server, which then the server processes the request, and then sends back a response to the client.
Flask needs to process this request, so in order to do so, the application needs to know which code needs to be executed for the URL that the client requests. In order to process this request, Flask keeps a mapping of URLs to Python functions. The reason Flask keeps a url map is so it knows which code needs to be executed for the URL the client requests. The link between a Python function and a URL is called a route.
A route in Python is defined using the “app.route” decorator which registers the decorated function as a route. So in our “Hello World” application, we can see that the index() function gets executed when a client requests a URL to the root directory. The index() function would be considered a view function because that is the response which is returned after the client sends a request. A response from a view function is often HTML content, but it can be a simple string with HTML content as well, as seen from our “Hello World” application. By the way, writing code with strings directly in the Python script is bad practice, it makes code difficult to maintain, however this was just a simple introduction to seeing how a simple Flask application works.
Running your Server
To actually run your Flask application, go into your terminal:
Now in our “Hello World” Flask application, we have a run() method that we are using to actually launch the application. The “__name__ == ‘__main__’” is used there to ensure that the web server is started only when you actually go into your terminal and run the script directly. Otherwise, whenever the script is imported by another file, it will assume that the parent script will launch the server so the app.run() call could be skipped.
Now that you have started the server, once it is up, it will actually go into a loop that will wait for requests to service. Your application will keep running until you shutdown your server, so in your terminal, simply hit Ctrl+C to stop your server.
In our “Hello World” application you can see we have also passed in an argument into our run() function, and this was specifically done so that we can have our web application running in debug mode. This activates a debugger and reloader, so that every time we save our web application with new changes, it will automatically reload them.
To see your web application live, go into your web browser and type “http://127.0.0.1:5000/” so you can see your Hello World application in action. Now obviously this application isn’t doing anything special, but this post was simply written to show you the basics of how Flask applications work and the Hello World example is the easiest way to get started. In the next few posts, I will be going over the request/response cycle in more detail in regards to Flask, and templates in Flask.
You can check out the previous post here.
Additional Links & Helpful Resources
- HTTP Wiki: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
- Overview of HTTP (recommended): https://www.youtube.com/watch?v=iy6xhPB12V8
- CS50 HTTP Study Guide (recommended): https://study.cs50.net/http
- Flask Quickstart: http://flask.pocoo.org/docs/0.12/quickstart/