Flask 101: Writing and Understanding Your First Flask Code
An introduction into the world of Flask micro-framework with "hello-world" style code.

I have experience and a background in electronics. I started out building embedded systems and my passion for robotics made me branch out into artificial intelligence.
Now I'm fully into machine learning and computer vision, I love getting my hands dirty with cool and interesting projects. In the future, I plan on my merging my knowledge of electronics and AI into the field of robotics.
I studied Computer Engineering at the Obafemi Awolowo University, Ile-Ife, Nigeria. When I'm not coding or doing chores, you would find me playing video games or enjoying some rock music.
When you hear flask, the first thing that might come to your mind is a water bottle but if a software engineer hears it, the first thing in their mind is web development. If you did think of a water bottle you're not so far from what the Flask framework was all about.
Originated as an April fool's joke back in 2004, flask became serious enough that the developers continued work on it, the name was a play on a similar earlier framework called the bottle framework.

Prerequisites: It is assumed that you have some knowledge of python programming and also some basic knowledge of HTML.
This article will cover concepts on:
- What Flask and micro-framework is
- How to install flask
- How to write a simple flask code
- What dynamic and static routing is and how to apply them
So, What is Flask?
Flask is a web development framework that makes a developer's life easier when building reliable, scalable, and maintainable web applications.
Flask is frequently called a micro-framework and rightfully so. It allows its use without the need for dependencies that distracts from the primary function of the application. If such dependencies are needed, they can be installed separately without necessarily becoming boiler-plate code.
The best way to learn is by doing and even though we're going to be learning the basic fundamentals of how flask code works, we'll be doing it hands-on. So it's best to get your IDE running and code-along or you could just keep reading, I'm not the boss of you. 😅
First things first, we have to install the flask library. It can be done using this cmd/shell.
pip install Flask
On your computer, you can make a new directory and name it 'flask_101', this is where our flask app will be situated. Using your ide or notepad application (whichever floats your boat), create a python file and name it app.py as is the usual naming convention for flask applications.
Now, let's write some code, we have to first import the Flask module from our flask library into our code.
from flask import Flask
Next, we make an object with the imported Flask module. We initialize it with the variable, 'app'.
app = Flask(__name__)
Now, to run our application, it has to show some sort of output in a GUI. Flask can help us to achieve this using HTML. It's done using a function called the view function. As the name implies, the view function dictates how we want our output to be viewed. The view function usually comes accompanied by another line of code called the route function. The route function is usually declared just before its accompanying view function. The route function provides a URL route that we can use to call the specified view function.
Let's start with the route function. The specified route function below takes in the route we want to use. '/' is usually used to refer to the home page on a website so we will be doing that here.
@app.route("/")
...Then the view function. The output returned is what will be shown in our browser.
def hello():
return "Hello World!"
We're almost done. Now, we need to specify how our app should be run. The basic line of code to run is app.run(), this function takes in 3 basic arguments;
- debug: This specifies if we want the app to run in debug mode or not.
- host: Specifies the host address on the network
- port: Specifies the port number on the network
Since we will be running this on our local network (our own computer), the host address and port need not be specified as it will be automatically set to the local address and port:
if __name__ = __main__:
app.run(debug=True)
Now that our code is ready, we should run it. The easiest way to do so is to just run flask run in the command prompt if you're on windows or bash shell if you're on mac/linux. The only problem is that our app will not run in debug mode as specified in the code, we can change this by running the app itself.
If you haven't, change your working directory in the terminal to flask_101 then run app.py using python app.py. The output response will look something like this:
This shows us the details of the running session, copy the host address along with the port number (https://127.0.0.1:5000/) and paste it into your browser, run it and voila! You just ran your first flask code. You should see the following output:

If you run into any error while trying to run your code, check your code again to make sure you followed every step.
In the route function, we used an argument that was a simple string, this form of routing is called Static Routing and this is because the contents being viewed is always static and can't be changed.
There is another form of routing called Dynamic Routing, this form of routing allows us to dynamically change the contents being viewed just by routing. This is done by using variables, instead of simple strings, as the argument of the routing function and the view function. Let's add some dynamic routing to our website. Right under the return statement of the hello() view function, let's add another route and view function.
@app.route("/<my_name>") #variable name: my_name
def name(my_name): #variable name: my_name
return "Hello world, my name is "+my_name+"!"
Run the app again from your cmd/shell using python app.py, then go to browser and type in the address http://127.0.0.1:5000/Toba (Try replacing 'Toba' with your name or any name you want)
The variable my_name is taken from the URL and converted into a string that is displayed on the HTML page. Not only strings can be used in dynamic routing, but we can also use int, float, path and even UUIDs (read more on UUIDs here) can be used. The way dynamic routing works is to use the following format: <converter:variable_name>. By default, the converter is String, so the converter doesn't have to be specified like we did when we added the name variable but if you want to work with the other converters, they do have to be specified.
For example, let's add the square of our age to the website. We will use the int converter.
Add a new route and view function just like before, below the return statement of the name() view function.
@app.route("/square/<int:age>")
def squared_age(age):
square = age * age
return "The square of my age is "+square+"!"
Now, let's run it. Go to cmd/shell and run python app.py, then in your browser enter http://127.0.0.1:5000/square/24 (you can replace 24 with your age or any number you want). The output should be something like this.

Do you feel that? That's the pride that comes with developing your own flask server. There's a lot more to learn and a lot to cover in the world of flask, if this excited you, you can take up courses to learn how it works and how to use it efficiently along with the database, forms and other dependencies that can be used along with it.
Thank you for reading, you deserve an ice cream or whatever she's offering...




