Flask Tutorial Python — Basics of The Flask Framework

Image for post
Image for post
https://en.wikipedia.org/wiki/Flask_(web_framework)

Let us move on with the journey of creating our web application using this
flask tutorial. This python flask tutorial is in continuation with my last post on How To Start Web Development With Flask too.

In this tutorial series, you will learn how Flask becomes part of a web
development workflow and what additional details as a web developer you need to know other than knowing the components of the Flask framework.

We are using Python version 3 and are already aware of the following
concepts.

1. Installation of Python
2. Installation and activation of Python’s virtual environment.

Let us move ahead with an assumption that all commands will be executed inside the command prompt or a terminal in an activated virtual environment and all file edits will be done in the editor of your choice.

Moreover, if you want some extensions to build their wheel distributions in the virtual environment then you can install Python’s wheel package using the below command.

However, if you want to have the look at the code examples from these tutorials then you can visit the repository at https://github.com/theflaskbooks/flask-tutorial.

If required you can clone the repository using the below-given command.

What is Flask?

Flask is a micro-framework written in Python and was developed by Armin
Ronacher. Its initial release was approx 10 years ago on April 1, 2010.

Flask is classified as a micro framework because it does not come with built-in tools and utilities for tasks such as database interaction, form validation, or other similar components or libraries that provide common functions.

However, the Flask community has contributed a lot by providing these common functions in the form of Flask extensions. These Flask extensions are installed with the help of Python’s package manager — Pip.

Development of Flask Application for complex projects require us to make use of Flask extensions. I am going to cover some great flask extensions while explaining common functions.

Create Your First Flask Application Project

Let us create our first professional-looking Flask Application project with most common features of Flask framework already present.

Install Flask-Appbuilder

Use the below-given command to install Flask-Appbuilder.

Installation of the Flask-Appbuilder extension makes fab sub-command available for use. Now create the skeleton of your next big Flask project using the below-given command.

Follow the prompts in this interactive command-line application to create the skeleton of the first application. Leave the default value for “Your engine type”.

Once you complete the prompts of the create-app command, you get an output like the one shown below.

Moreover, you can have a look at the directory structure of the project. It should look like the one shown below.

I kept the name of my Flask application as “myapp”. The created application structure is suitable for developing a web application using the MVC pattern of web development.

MVC stands for Model-View-Controller. It is a design pattern that divides the
program logic into three interconnected elements. This form of internal
information representation is different from the way information is presented to and accepted from the user.

1. Model — The code concerning the interaction with the database in the file models.py.

2. View — The Application code concerning the routes and views are written in views.py.

3. Controller — Controllers or methods that correspond to one route are also written in this file. However, note that it is not best practice to have complex business logic inside of the view.py file. It is best to keep such logic outside in a separate Python module or a package.

Configuration values that are required application-wide are to be kept in the file `config.py`.

For running the development server for this application, you need to use the command line or terminal to go into the project’s root directory and use the below-given command.

Have a look at the code inside the `run.py` file.

The output from the development server is quite verbose. Points to note here is that you can specify the values for host, port, and debug while running the development server.

For the host, you need to give the IP address of the network card that you want to run your application on. Here “0.0.0.0” address means that the application is listening on all network interfaces on your computer.

It means that the application can be accessed inside the internal network from any other local machine. This is quite useful if you want other teammates to test your application from their computers.

If you specify True for debug parameter then the development server will run in DEBUG mode and will automatically reload the files whenever you make any changes to them.

For port, you can specify any value you want between 0 and 65535. However, it is a convention to keep this value greater than 4000 so that, during development, your application doesn’t interfere with other applications running on your computer.

What is a Flask template?

Files concerning the representation layer of the web application are kept under the templates directory. These templates are called Jinja2 templates.

Flask uses Jinja2 libraries to render templates. These files under template directory contain static data as well as placeholders for dynamic data.

Summary

In this tutorial, I explained the steps of installing a Flask extension called
Flask-Appbuilder, which is a simple and rapid application development framework created on top of Flask.

Additionally, I mentioned the details on Flask Appbuilder’s create-app command and the files inside the skeleton it creates for us to use while developing the Flask Application.

Read my previous tutorial How to Start Web Development With Flask. In my next tutorial, I will cover Flask Templates. Meanwhile, you can try warming up with the development server and different configuration values.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store