DEV Community

Abdul Rehman Nadeem
Abdul Rehman Nadeem

Posted on

Creating a Simple To-Do List Application with Flask

In this tutorial, we will build a simple To-Do List web application using Flask, a lightweight and powerful Python web framework. This project will help you understand the fundamentals of web development with Flask and provide you with a practical example of building a web application from scratch.

Prerequisites

Before we begin, make sure you have the following prerequisites installed on your system:

  1. Python 3.x
  2. Flask
  3. Virtualenv (optional but recommended)

Setting Up the Project

Let's start by setting up our project structure:

mkdir flask-todo-app
cd flask-todo-app
Enter fullscreen mode Exit fullscreen mode

Next, create a virtual environment and activate it (optional but recommended):

virtualenv venv
source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install Flask:

pip install Flask
Enter fullscreen mode Exit fullscreen mode

Now, let's create our project files:

touch app.py templates/index.html
Enter fullscreen mode Exit fullscreen mode

Building the To-Do List

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    # Add your to-do list logic here
    tasks = ["Task 1", "Task 2", "Task 3"]
    return render_template('index.html', tasks=tasks)

if __name__ == '__main__':
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>To-Do List</title>
</head>
<body>
    <h1>To-Do List</h1>
    <ul>
        {% for task in tasks %}
            <li>{{ task }}</li>
        {% endfor %}
    </ul>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Running the Application

To run the application, execute the following command:

python app.py
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:5000 in your web browser, and you should see your simple To-Do List application in action!

Conclusion

Congratulations! You've successfully created a basic To-Do List application with Flask. This is just the beginning, and you can expand and enhance this project by adding features like task addition, deletion, and more.

In future posts, we'll explore more advanced Flask topics and build on this foundation. Stay tuned for more Flask tutorials!

I hope you found this tutorial helpful. If you have any questions or suggestions, please feel free to leave a comment below. Happy coding!

Top comments (0)