This repository contains a simple "To-Do List" web application built in Python using the Flask framework. The primary goal of this project is to provide a clear, real-world example of the Model-View-Controller (MVC) architectural pattern, as described in "Patterns of Enterprise Application Architecture."
The application's code is intentionally and clearly separated into three distinct components to demonstrate the "Separation of Concerns" principle:
- Model: Represents the application's data and business logic.
- View: Represents the User Interface (UI) that the user interacts with.
- Controller: Acts as the intermediary, receiving user input, interacting with the Model, and updating the View.
The file structure is designed to make the MVC separation explicit:
model.py(Model): Contains theTaskModelclass. It handles all data and business logic (getting tasks, adding tasks, completing tasks). It has no knowledge of Flask, HTTP, or HTML.templates/index.html(View): The HTML template that defines the UI. It uses Jinja2 syntax to display data it receives from the controller. It contains no business logic.app.py(Controller): Uses Flask to define web routes. It handles user requests (like form submissions), calls methods on theTaskModel, and then renders theindex.htmlView, passing it the necessary data.
- Python 3
- Flask: A micro web framework used to handle the Controller's routing and serve the View.
- Jinja2: A templating engine (installed with Flask) used by the View.
Follow these instructions to get the project running on your local machine.
- Python 3.x
pip(Python package installer)
-
Clone the repository:
git clone [https://github.com/Brunoenr02/Model-View-Controller-MVC-Pattern](https://github.com/Brunoenr02/Model-View-Controller-MVC-Pattern)
(Remember to replace
your-usernamewith your actual GitHub username.) -
Create and activate a virtual environment (Recommended):
# For macOS/Linux python3 -m venv venv source venv/bin/activate # For Windows python -m venv venv venv\Scripts\activate
-
Install the required package (Flask):
pip install Flask
(Alternatively, you can create a
requirements.txtfile, addFlaskto it, and runpip install -r requirements.txt) -
Run the application:
python app.py
-
View the app in your browser: Open your web browser and navigate to:
http://127.0.0.1:5000
You will see the To-Do List application running. You can add new tasks and complete existing ones.
This project is open-source and available under the MIT License.