In today's fast-paced world, staying organized is essential. Whether you're managing daily chores or planning a major project, a personalized To-Do List is your best companion. In this guide, we’ll walk through creating a lightweight web application using Python's Flask framework.
Step 1: Setting Up Your Workspace
First, create a dedicated folder for your project. Inside, you'll need three main components: a Python file named app.py for the logic, a templates folder containing index.html for the user interface, and a static folder with a style.css file to make your app look great.
Step 2: Initializing the Application
Open app.py and set up your Flask application. You'll need to import the necessary modules to run the server and render your HTML page. This forms the backbone of your web app, listening for requests and serving your interface.
Step 3: Managing Tasks with Backend Logic
To store your tasks, initialize an empty list in your Python code. Next, create a specific route that listens for 'POST' requests from your form. This function will capture the new task text, add it to your list, and seamlessly redirect the user back to the home page.
Step 4: Rendering Tasks Dynamically
Finally, open your index.html file. Inside your unordered list tags, use Jinja2 looping syntax to iterate through your task list. This ensures that every time a user adds a new item, it dynamically appears on the screen in real time.
HTML code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>To-Do List</title> <link rel="stylesheet" href="style.css"> </head> <body> <main class="todo-card"> <h1>My Tasks</h1> <form class="todo-form" method="post" action="/add"> <input class="todo-input" type="text" name="task" placeholder="What needs to be done?" aria-label="New task" required> <label class="todo-time-label" for="todo-time">Due time (optional)</label> <input class="todo-time" id="todo-time" type="time" name="dueTime" aria-label="Task due time"> <button type="submit">Add task</button> </form> <ul class="todo-list"> {% for task in tasks %} <li>{{ task }}</li> {% endfor %} </ul> </main> <script src="app.js" defer></script> </body> </html> CSS code:
* { box-sizing: border-box; } body { min-height: 100vh; margin: 0; display: grid; place-items: center; padding: 24px; background: linear-gradient(135deg, #eef2ff, #dbeafe); color: #1f2937; font-family: Arial, sans-serif; } .todo-card { width: min(420px, 100%); padding: 28px; border-radius: 18px; background: #ffffff; box-shadow: 0 18px 45px rgba(30, 64, 175, 0.15); } h1 { margin: 0 0 20px; text-align: center; color: #3730a3; } .todo-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; } .todo-form .todo-input { grid-column: 1 / -1; } .todo-time-label { align-self: center; color: #4b5563; font-size: 14px; } .todo-form .todo-time { width: 140px; } .todo-form input { min-width: 0; height: 44px; border: 2px solid #e5e7eb; border-radius: 10px; padding: 0 14px; outline: none; font-size: 16px; } .todo-form input:focus { border-color: #6366f1; box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12); } .todo-form button { border: 0; border-radius: 10px; padding: 0 18px; background: #4f46e5; color: #ffffff; font-weight: 700; cursor: pointer; } .todo-form button:hover { background: #4338ca; } .todo-form button:active { transform: scale(0.98); } .todo-list { margin: 20px 0 0; padding: 0; list-style: none; } .todo-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; padding: 12px 14px; border: 1px solid #e5e7eb; border-radius: 10px; background: #f9fafb; } .todo-task { overflow-wrap: anywhere; } .todo-due-time { flex-shrink: 0; color: #4f46e5; font-size: 14px; font-weight: 700; } PYTHON code:
from flask import Flask, render_template, request, redirect app = Flask(__name__) tasks = [] @app.route('/add', methods=['POST']) def add_task(): task = request.form.get("task") tasks.append(task) return redirect("/") @app.route('/') def home(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True) 
