Table of Contents
Introduction
In this tutorial, we will learn how to build a Python app for scheduling and task management. The app will allow users to create, update, and delete tasks, as well as set due dates and priorities. By the end of this tutorial, you will have a functional task management app that you can use in your own projects.
Prerequisites
Before starting this tutorial, you should have a basic understanding of Python programming language. Familiarity with object-oriented programming (OOP) concepts will be helpful but not mandatory. Additionally, you should have Python installed on your machine.
Setup
To get started, we need to set up our development environment. Follow the steps below:
- Open your terminal or command prompt.
- Create a new directory for your project:
mkdir task_management_app
. - Navigate to the project directory:
cd task_management_app
. - Create a Python virtual environment:
python -m venv venv
. - Activate the virtual environment:
- On macOS and Linux:
source venv/bin/activate
. - On Windows:
venv\Scripts\activate.bat
.
- On macOS and Linux:
- Install the required dependencies:
pip install flask
.
Now that we have set up our environment, let’s start creating the task management app.
Creating the Task Management App
- Create a new Python file called
app.py
in your project directory. - Import the necessary modules:
from flask import Flask, render_template, request import os import json
- Create an instance of the Flask class:
app = Flask(__name__)
- Define the route for the home page. This route will display the list of tasks:
@app.route('/') def index(): tasks = load_tasks() return render_template('index.html', tasks=tasks)
- Create a function to load the tasks from a JSON file:
def load_tasks(): if os.path.exists('tasks.json'): with open('tasks.json', 'r') as f: tasks = json.load(f) return tasks else: return []
- Create a template file called
index.html
in a new directory calledtemplates
. Add the following code to display the tasks:<!DOCTYPE html> <html> <head> <title>Task Management App</title> </head> <body> <h1>Task List</h1> <ul> </ul> </body> </html>
- Run the app:
- On macOS and Linux:
export FLASK_APP=app.py && flask run
. - On Windows:
set FLASK_APP=app.py && flask run
.
- On macOS and Linux:
- Open your web browser and visit
http://127.0.0.1:5000
to see the task list.
Congratulations! You have successfully created a basic task management app. Now, let’s enhance it by adding functionality to create, update, and delete tasks.
To be continued…