Flask for beginners

Flask for beginners

Web Development with Flask

Welcome to your one stop solution for all-things-Flask.

By the time you reach the bottom of this post, you'll have a basic Flask app set up and be ready to start building simple basic apps on your own.

Let's dive right in.

What is Flask?

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It uses extensions to provide your app with a great deal of features, like form validation, upload handling etc. It needs almost no dependencies and gets the job done in the least amount of extra baggage.

It is lightweight, compact and gets your app going in just 3 lines of code. You can even create custom endpoints for your app and have them serve requests from the frontend and not worry about the lower levels of the process.

The ever growing community has further given rise to extensions like SQL-Alchemy and flask-mail, which come in handy and make your life much simpler.

Why I prefer Flask?

One gets to code it all in Python (and if you know enough HTML, CSS and JS, you can throw in a good mix of it all) and you'd already have a full stack app running for you in almost no time.

It is super simple. From the very first Hello World app, you can make the simplest of apps using the most basic concepts in Python. If you're into Web development and you want to dish out super quick stuff real fast, this is what you need.

Fire up your text editors, this is going to be fun.

Getting started:

First and foremost, the main prerequisites are that you have Python and a decent text editor installed on your device.

Note: Any version of Python older than Python 3.3 is not supported by Flask, so you might want to consider upgrading.

Create your project directory and open the directory in your text editor of choice.

Now, in your terminal navigate to your project directory and enter the following command:

pip install virtualenv

This will install the virtual environment in which the Flask server will run. When this is completed, enter the next three commands in the terminal.

virtualenv env

This creates the env folder in your project directory. The documentation of Flask says it all:

In order to set the environment and debug mode reliably, Flask uses environment variables. The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in.

After this, we need to activate the virtual environment that we just created. We do that with (still in the Terminal):

source env/bin/activate

Now enter this command when the previous step is completed. This step is the most crucial one of them all; this is the one where you install Flask and get your project almost ready to go.

pip install flask

Great, you are now set up with a project directory that is ready to run a Flask app. However, we still need the Python code for it to run. Go ahead and create a .py file in the project directory. Convention dictates that you name it app.py but go crazy, knock yourself out, get creative dammit.

In this app.py file, paste the following code:

from flask import Flask

app = Flask(__name__)

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

if __name__ = "__main__":
      app.run(debug = True)

So, that was a lot to take in, huh? Don't worry, this is as tough as it gets anyway. Let me break down what you're looking at.

Note: This part might feel a little confusing if your basic Python is rusty, but we'll brush it up right here as we go along.

First, we imported Flask. __name__ is just a convenient way to get the import name of the place the app is defined. Flask uses the import name to know where to look up resources, templates, static files, instance folder, etc. After that we added the decorator which specifies the route and beyond that is the function that handles the output to be given. This function will later hold a lot of the code and there will be many such routes handling different processes.

(I will post somethign on project restructuring as well to keep your code clean, but that comes later when the number of routes start to become overwhelming) Finally, we add the app.run command which sets the debug value to True and therefore gives us details about any errors that we might encounter.

Finish it off by running the code and Flask will fire up the development server for you, where you should see the output.

Congratulations, you have built a Flask app. 🥳

Coming up next: Adding templates and Introduction to Jinja2

Till then, experiment with the basic app we just made, and try to build something for yourself.