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:
- Python 3.x
- Flask
- 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
Next, create a virtual environment and activate it (optional but recommended):
virtualenv venv
source venv/bin/activate
Install Flask:
pip install Flask
Now, let's create our project files:
touch app.py templates/index.html
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)
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>
Running the Application
To run the application, execute the following command:
python app.py
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)