VOOZH about

URL: https://thenewstack.io/how-to-use-flask-a-lightweight-python-framework/

⇱ How to Use Flask, a Lightweight Python Framework - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
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%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-05-20 04:00:48
How to Use Flask, a Lightweight Python Framework
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 Jessica Wachtel
👁 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.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.