Get started with Flask, a Python-based framework for web development, and learn how you can use it in your IoT projects!
If youâve played around with Python and youâre confident writing code, then you may be ready to step it up and try developing your own Python-powered website!
While Python is well-known for its capabilities in data analysis and machine learning, it is also used to run some of the largest websites on the internet, including Reddit, Netflix, Pinterest, and Instagram.
Choosing a Framework: Django or Flask?
If you want to try out some Python web development, you first have to choose between two frameworks: Flask or Django. They both do the same thing â they convert your Python code into a fully-functioning web server.
Flask is a minimalist version in which everything is an optional extra you can choose, whereas Django comes with everything already âplugged inâ and has pre-defined libraries, techniques, and technologies that you use out-of-the-box.
Flask is good for beginners and for quickly building small/medium applications. Django is arguably better for large enterprise applications, but also requires a deeper understanding.
For this tutorial, weâll start with Flask, but you can always hop over to Django in the future once youâre comfortable with the basics.
As with most libraries in Python, installation is a dream â just install it with pip:
Thatâs it! Onto the next section.
Client and Server
Before we begin developing our first Flask web app, we need to clear up the roles of the client and the server. The client is a web browser, like Google Chrome or Firefox. When you type in a web address and press enter, youâll send a request to a server that will âserveâ the website you want.
Your First Flask Web App
Letâs begin with the time-honored tradition of a âHello Worldâ application. This example is a slightly modified version of the Hello World app in the Flask documentation.
from flask import Flask # Import Flask
app = Flask(__name__) # Create a Flask "app" instance
@app.route("/") # Instructs that the default url of http://127.0.0.1:5000/ should trigger this code
@app.route("/index") # Instructs that the url of http://127.0.0.1:5000/index should also trigger this code
return "<h1>Hello World!</h1>" # The HTML to return to the browser
if __name__ == '__main__': # Startup stuff
app.run(host='127.0.0.1', port=5000) # Specify the port and url
Run the code and then go to Google Chrome (or your browser of choice) and type in âhttp://127.0.0.1:5000/â, without the quotes. You should see a delightful âHello Worldâ appear, welcoming you to the world of web development.
A Scalable Structure for Flask Web Apps
While having all of the code in a single file is a quick and exciting introduction to Flask, you will probably want to split your code into separate files to make it more manageable.
To help you in this, Iâve created a simple structure you can use for your Flask applications. Head over to GitHub to grab the source code for yourself.
This structure has a âstart.pyâ script that you run to fire-up your webserver. All of the Python code is inside a folder called âcodeâ. This folder is where your âroutes.pyâ file lives. It contains the definitions for requesting web URLs and how your app responds when they are.
If you have any Python logic such as calculating stats or machine learning models, then these should also go inside the âcodeâ folder.
The âtemplatesâ folder houses the HTML files that will be returned by the server. Theyâre called templates because you can optionally insert values to change the text and graphics at runtime.
Setting Up Deployment
With what weâve discussed so far, youâll be able to create a web app that runs on your computer, but if you try and access your website from another computer, youâll run into difficulties. This is the topic of deployment.
First, we need to adjust the port on which your website presents itself. We want to set the host to âhost=0.0.0.0â and the port to âport=80â. Setting the host and port makes your website visible on the IP address of your host computer.
For example, if you run your Flask web app on a Raspberry Pi with an IP address of 192.168.1.10, then you can navigate to that IP address in the web browser of a separate computer, and youâll see your web page served up for you. This is a great foundation for IoT projects and live sensor dashboards.
We define that âport=80â as this is the default port for communication over the HTTP protocol. When you request a URL in your browser over HTTP, it knows to go to port 80, so you donât have to state it explicitly in the web address.
For all of this to work, youâll have to be on the same network as the computer running your web server, and youâll need to make sure that the firewall on your host computer (if you have one) is opened up to requests on port 80.
Next Steps and Going Further
If you want to take a deep-dive into Flask web development, then I strongly recommend the Flask Mega Tutorial by Miguel Grinberg. Itâs very detailed and is an invaluable source for learning Flask.