TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python / Software Development

How to Use Flask, a Lightweight Python Framework

This tutorial shows how to use Flask, a lightweight, minimalistic Python framework.
May 20th, 2024 4:00am by
Featued image for: How to Use Flask, a Lightweight Python Framework

Flask is a super lightweight, minimalistic Python framework that allows developers of all skill levels to spin their apps up on the fly. You can have a live Flask development server up in about thirty seconds with just a few lines of code. Flask’s place in production-level applications lies mainly in testing new modules and architectures. Flask is also great for the newer development community because it takes much of the heavy lifting and boilerplate out of building new applications. This tutorial will focus on building a Flask development server. Once we have a live server, we’ll build out functionality by coding a to-do list application.

First, let’s install Flask. You can do that by running the following command in your terminal” pip install Flask. After you install Flask, start a new project folder in your code editor. The first file we’ll work with is called app.py.

Similarly to other frameworks, we’ll need to import Flask and create an instance of the application before we can run anything. Type the following import statement at the top of the page:
from flask import Flask

You can create the instance of the application with the following code:

app = Flask(__name__)

Once you create the app instance, you can write the following code at the bottom of your page (this code will always live at the bottom of your page) to run the app instance.

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

We don’t have routing yet so if you run python app.py in your terminal and check the dev server, you’ll get a 404 error but that’s all the Flask boilerplate you’ll need to get moving.

To get a page live, we’ll need to add routing. Routing is the communication channels between different parts of an application. For a more robust explanation, check out this link.

Flask uses Python’s decorator method for routing. Defining a route will look like this:

The first parameter in app.route() will help define the URL. You can use any keyword or no keyword. The route below illustrates how to create a route without adding a keyword.

The function below it will render the page:

Your code file should look like this and you’re ready to run python app.py in your terminal and see a live page.

To see more of what Flask can do, let’s get started on our to-do list.

Adding Many Routes

The first thing you can do is remove the def hello() function. For this app, we’re going to render out an HTML page. The next thing we’ll do is add a package to our code file. You can do this by typing pip install requests in your terminal. Once the package is added, we can go ahead and add to our import statement. Let’s add request, redirect, and url_for. request allows Flask to handle the requests we’re going to make. redirect sends the user to the right URL. url_for prepares the URL for a function dynamically so the application doesn’t need to change URLs.

The import statement will now look like this:

Next, let’s create our tasks list. We can do this by creating an empty list called tasks under our app instance. At this time your code file should look like this:

Let’s get back into app.py so we can create the function that renders out our HTML front end. In place of our hello() function, let’s create the following:

We’re going to build our task-add mechanism first. We’ll pass two arguments to app.route(). The first will be our URL keyword and the second will inform app.route() that it’s a POST function. The updated code will look like this:

The add_task() function will need to do a few things. First, the function will retrieve the task from the front end form when submitted by the user. Then the function will add the task to the tasks list. Lastly the function will redirect the user back to the home page. The code looks like this:

Lastly we’ll need to set up our completion route and function. Similarly to the add_task() route, the delete_task() route will include the URL keywords and identify the route as a POST request. The completion route will require the unique task to be included in the route so the function knows which task to delete. The route will look like this:

The function will capture the task argument, remove it from the task list, then send the user back to the home page.

The complete app.py will look like this:

Run python app.py and you’ll have a complete to-do list!

Thanks to Flask, this is quite a bit easier than the React to-do list I built for my first project in coding school. This is also just a start. For more learning on routing in Flask, try adding the weather to your to-do list. You can access the weather from a free weather API.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.