The objective of this post is to explain how to create a simple “Hello world” application with Python and Flask.
Flask is a web micro framework for Python  which allows us to create and deploy simple web applications very easily.
The installation of Flask is very simple if we use pip. We just need to type the following:
pip install Flask
The hello world application
The code for the “Hello world” application is very straightforward. First of all, we need to import the Flask class from the flask module, so all the functionality we need becomes available.
from flask import Flask
You can read more about the Flask class here. Once the class is imported, we will create an instance of it. The first parameter of the constructor should be the name of the package or module of our application. Since we are using a single module, we can use __name__, which is a global variable that holds the current module’s name as a string .
app = Flask(__name__)
Now, we will define a route. A route is basically a decorator that allows to specify an URL associated with a Python function. When a request is done to that URL, the corresponding function is executed.
For our simple example, we will specify an URL called “/hello” that will trigger a function that returns a greeting message. The code is shown bellow.
@app.route('/hello') def helloWorldHandler(): return 'Hello World from Flask!'
As seen, in the route decorator we pass the URL we want (“/hello”) and then we define a function that will handle the HTTP request on that URL. We can name the function whatever we want. Then, we just specify our greeting sentence as the return value of the function.
To tell our application to run, we just call the run method on the Flask object we instantiated before. You can read more about the run method here.
We can specify as arguments of the run method the host and the port where the server will be listening. The host defaults to 127.0.0.1, which is the loopback address, and the port to 5000 .
Nevertheless, if we use those default settings, our application is only available on our machine. In this case, we will specify other values, so it will be available to other machines on the same network.
To to so, we specify the host IP as 0.0.0.0, so it is externally available in the network. Although we could have kept the default port, we will change it to 8090, just to exemplify how to do it.
app.run(host='0.0.0.0', port= 8090)
The complete code for this tutorial is shown bellow.
from flask import Flask app = Flask(__name__) @app.route('/hello') def helloWorldHandler(): return 'Hello World from Flask!' app.run(host='0.0.0.0', port= 8090)
Testing the code
To test the application, just run it, for example, on IDLE, the Python IDE. A message similar to the one shown in figure 1 should be printed to the command line, indicating that the server is listening to incoming HTTP requests.
Figure 1 – Running the Flask application from IDLE.
Now, to test it, just open your web browser of choice and type the following on the address bar:
You should get the greeting message that we defined before, as shown in figure 2.
Figure 2 – Output of the hello world application.
If we check the command line again, a debugging message has been received, indicating the server received a HTTP request, as shown in figure 3.
Figure 3 – Output of the command line after receiving a request.
We can see the GET request on the “/hello” route, with the success HTTP code (200). Additionally, in my case, there is a request for a favicon.ico route, which is a default request from the browser to get the small icon that it presents on the left side of the website tab . Naturally, since we didn’t define a handler to this route, it returns a 404 HTTP code, which corresponds to the “Not Found” code. You may not get this call, depending on your browser.
In this case, we used the loopback IP address just for the sake of testing the response without any external variables that may lead to problems, since this request is performed internally on the same machine. You should try doing a request from other machine in the same network to check if the server is available.
So, from the other machine, just open a web browser and type the same address as before, but now with the IP found with the previous commands. You can also test this from the web browser of a smartphone or tablet, as long as they are connected to the same network of the machine that is running the Flask application.
You should get the same output message. If you are not able to access the Flask app, then you may have used the wrong IP or the firewall of your computer may be blocking the connection.
Also, just keep in mind that you are not going to be able to connect to the Flask app from outside your router’s network with this method. To do it, you would need to do port forwarding, so the server would become accessible from the Internet. Port forwarding a router depends on the model of the device, so explaining how to do it is outside the scope of this post.
As seen through this tutorial, deploying a web application with Flask is really simple. Personally, I’ve been using Flask and other micro frameworks when I need to do some IoT quick proof of concepts, as the temperature logger described in a previous post.
For those who like to work with IoT devices, such as the ESP8266 or the LinkIt Smart, Flask offers the possibility to quickly deploy a custom web server, giving us much more freedom that when we use a IoT server with predefined rules.
- Python version:3.4.2
- Flask library: 0.10.1