7 minutes read

Flask is a micro-framework that can build awesome web applications. It was created in 2010 by Armin Ronacher, an Austrian programmer. This framework is open-source, so anyone can get the code and change it.

flask logo

Flask has undergone many changes during development; countless bugs have been fixed. GitHub reports that about 3000 changes have been made over the years. This colossal work has paved the way to fame, and Flask is now one of the most sought-after libraries for web application development.

Purpose

Flask is good when you want to create web applications that have no dynamic web pages. The easiest task for Flask, which can be completed in 10-15 minutes, is a one-page business card site that presents a company or a person. Sounds great, doesn't it?

Of course, not everything is that easy. You will need to take care of the outer look — make some pages using HTML, CSS, and JavaScript. This is usually done by front-end developers, as opposed to back-end programmers who use, for example, Flask to write website server part and web applications. Some may do both at once (back and front-end), they are called full-stack developers.

Other frameworks

Everywhere in life, you find tasks that may have more than one solution; web development is no exception. In addition to Flask, there are other web frameworks, Django being the most popular. This is a large module that overlaps Flask in some ways. It offers everything at once, while Flask allows programmers to configure everything as it is convenient for them. For example, Django immediately connects the admin panel, registration service, databases, and a lot of other things. In Flask, you need to do this manually. It may seem inconvenient at first glance, but it is actually useful as it allows us to work only with the modules that we actually need.

Django is useful when you need to create websites with complex animation that can adapt to particular user's needs. It would be an overstatement to say that Flask is inferior to Django — it may require more effort, but, in the end, you'll get a product that is more suitable for your needs.

Preparations

Now, when we understand the most common purpose of Flask use, let's move on to practice. We'll begin with creating a virtual environment -- one of the most important steps when building a new application.

After virtual environment is created and activated, we can install Flask using pip:

# Unix
python -m venv env
source env/bin/activate
pip install flask

# Windows
python -m venv env
env\Scripts\activate
pip install flask

Now, we are ready to move mountains (and browser tabs).

Create your app

It's time for us to write our first web app! It will take about 5 minutes, but I'm sure we can handle it. First, we need to import the main class of our framework:

from flask import Flask

Now let's create a variable that will initialize the application. The first (and the only required) parameter is used to resolve the directory where the Flask application is installed (so that Flask knows where to look up for resources, templates, static files etc). Sometimes you will see using the __name__ variable (which is basically equal to the module's the program runs in name) there, but in most cases it is recommended to hardcode the name of the application's package there. So we'll go with super-app:

app = Flask('super-app')

We're halfway there, don't give up! Let's tell our app what to do if a user accesses a specific URL:

@app.route('/')
def index():
   return 'Hello World!'

With the @app.route decorator, we're telling the program that the next function will be bound to a specific URL in the parentheses ('/' in our case). The function will show the following string: "Hello World!". Note that the function can have any name. What is important is that it follows the decorator.

Finally, let's launch our creation:

app.run()

Perfect! If everything is done correctly, the console will display several messages, the last of which will contain the address (the default is 127.0.0.1:5000) where the app is accessible. Enter this address in the browser and discover Hello World!

hello world page

Conclusions

We have started a series of topics that can help us master Flask. In this topic, we have covered some pros and cons of the framework: Flask is flexible, each module has to be connected individually. But it also means that you need to know about those modules beforehand, so Flask is not a ready-made solution. We have also created a virtual environment and compiled a small web application that displays certain messages to the person who uses it. It is beautiful, isn't it? If you are interested in more on Flask, take a look at the Official Flask Documentation. And good luck!

56 learners liked this piece of theory. 4 didn't like it. What about you?
Report a typo