Todo list app using Flask | Python
There are many frameworks which allow to build your webpage using Python, like django, flask etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to write applications without writing the low-level codes such as protocols, thread management, etc.
In this article, we will learn how to make a todo list app using Flask framework. In this app, you can add your todo items and mark them as complete or incomplete.
Installation:
pip install Flask
1. Create the main python app file as app.py and write the code.
from flask import Flask, render_template, request, redirect, url_for from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) ####### Database####### db = SQLAlchemy(app) class Todo(db.Model): id = db.Column(db.Integer, primary_key = True) text = db.Column(db.String(200)) complete = db.Column(db.Boolean) ###### Route when nothing is specified in the url###### @app.route('/') def index(): incomplete = Todo.query.filter_by(complete = False).all() complete = Todo.query.filter_by(complete = True).all() return render_template('index.html', incomplete = incomplete, complete = complete) ###### Adding items###### @app.route('/add', methods =['POST']) def add(): todo = Todo(text = request.form['todoitem'], complete = False) db.session.add(todo) db.session.commit() ###### Makes to stay on the same home page###### return redirect(url_for('index')) ###### Complete items###### @app.route('/complete/<id>') def complete(id): todo = Todo.query.filter_by(id = int(id)).first() todo.complete = True db.session.commit() ###### Makes to stay on the same home page###### return redirect(url_for('index')) if __name__ == '__main__': app.run(debug = True) |
2. Create the user interface part using html. Give the layout to the app
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Todo App</title> <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='main.css')}}"> </head> <body> <h1>Todo List</h1> <div>Add a new todo item: <form action="{{ url_for('add') }}" method="POST"> <input type="text" name="todoitem"> <input type="submit" value="Add Item" class="button"> </form> </div> <div> <h2>Incomplete Items</h2> <ul> {% for todo in incomplete %} <li style="font-size: 30pt" class='mark'>{{ todo.text }} <a href="{{ url_for('complete', id=todo.id) }}">Mark As Complete</a></li> {% endfor %} </ul> <h2>Completed Items</h2> <ul> {% for todo in complete %} <li style="font-size: 30pt">{{ todo.text }}</li> {% endfor %} </ul> </div> </body> </html> |
3. Create the static files like css and any other styling part
body{ background:black; color:red; margin-top: 5px; } .button{ color:green; } .mark{font-size: 10px;} |
After creating the files, create the database using sqlite3. Open the .db file created in your project file using sqlite3.

Create table in the database using .tables command

Open python in terminal and import the db file created to create tables

Now run the app.py file on terminal.
python3 app.py

Output:

Recommended Posts:
- Python | ToDo webapp using Django
- Subdomain in Flask | Python
- Python | Using for loop in Flask
- Python | Build a REST API using Flask
- Python | 404 Error handling in Flask
- Python | Introduction to Web development using Flask
- Create a Weather app using Flask | Python
- Flask - (Creating first simple application)
- Deploy Machine Learning Model using Flask
- Setup API for GeeksforGeeks user data using WebScraping and Flask
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Filter list of strings based on the substring list
- Python | Replace elements in second list with index of same element in first list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



