A Python project – A To-Do web application in the Flask framework

Lesson 11 of 15 · Python projects
Who this project is for:
an advanced project for students who have finished the basic course and the Python for the web lessons – usually secondary school. For grades 5–8, projects with graphics and the micro:bit are a better choice.

In this project we will make a simple To-Do web application using Python and the Flask framework.

A To-Do application lets the user add tasks they need to do.

In this project you will learn:
  • how to make a Flask web application
  • how to use an HTML template
  • how to send data from a form
  • how to show a list of tasks

Project structure


project/

app.py

templates/
    index.html

The application's Python code


from flask import Flask, render_template, request

app = Flask(__name__)

tasks = []

@app.route("/", methods=["GET","POST"])
def home():

    if request.method == "POST":

        task = request.form["task"]
        tasks.append(task)

    return render_template("index.html", tasks=tasks)


app.run(debug=True)

The HTML template

templates/index.html


<html>

<head>
<title>ToDo list</title>
</head>

<body>

<h1>My ToDo list</h1>

<form method="POST">

<input type="text" name="task">

<button type="submit">Add</button>

</form>

<ul>

{% for t in tasks %}

<li>{{ t }}</li>

{% endfor %}

</ul>

</body>

</html>

How this application works

  • the user enters a task in the form
  • Flask receives the data with request.form
  • the task is added to the list
  • the list of tasks is shown on the page
You can extend this project by adding a way to delete tasks or by saving the data in a database.

Possible improvements to the project

  • add a button for deleting tasks
  • add an SQLite database
  • add user accounts
  • improve the design of the page

Related lessons


Next project

How to make the project your own:
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.