Creating a Habit Tracking App with Python
- David Patrick Philippe Lupau
- Jul 22, 2024
- 4 min read
Updated: Dec 1, 2024
April 2024
This project has been done as part of the module "Object Oriented and Functional Programming with Python" in my BSc. Data Science. It allowed me to get familiar with the concepts of classes and also practice skills in SQL.

Context and problem
This project is about developing a basic Python backend for a habit tracking app, targeting the core functionalities without the need for a graphical user interface. The aim was to design an application where users could define habits with specific tasks and periodicity, track their completion, and analyse their performance over time. The project required a robust solution using object-oriented and functional programming principles in Python, addressing essential features like habit creation, task completion, streak tracking, and data persistence.
Approach and methodology
The Habit Tracker app was designed to provide users with a comprehensive tool for managing and analyzing their daily habits. The project employed object-oriented programming principles in Python to ensure modularity and maintainability. Key classes such as DataHandler, Habit, Analysis, and ErrorHandler were developed to encapsulate distinct functionalities and responsibilities within the application. The use of these classes facilitated a clear separation of concerns, enhancing the application's robustness and scalability.
A SQLite database was chosen for data management due to its lightweight and efficient nature. The database schema was carefully normalized to eliminate redundancy, ensure data integrity, and optimize query performance. This structure supported various operations such as creating, updating, deleting, and analyzing habits. The application also incorporated Pytest for unit testing to guarantee reliable performance and accuracy of functionalities.
The development process began with a thorough analysis of functional and non-functional requirements, followed by the creation of detailed use cases. These use cases helped in visualizing user interactions and system responses, ensuring that the application met user needs effectively. Additionally, conceptual and class diagrams were crafted to provide a clear blueprint of the system architecture, guiding the implementation phase.
Implementation
The implementation phase of the Habit Tracker app involved translating the design into a functional application. The main Python file, main.py, served as the entry point, delegating core operations to the auxiliary modules. The DataHandler class managed interactions with the SQLite database, including CRUD operations and data analysis. Methods within DataHandler were designed to handle both user-created and predefined habits, promoting code reusability and efficiency.
The Habit class encapsulated the attributes and behaviors of individual habits, while the Analysis class focused on processing and displaying user data. The ErrorHandler class centralized error handling, providing users with clear feedback on any issues encountered during app usage. Implementing a command-line interface (CLI) ensured a user-friendly experience, allowing users to navigate and interact with the app seamlessly.
Key features of the implementation included the use of efficient SQL queries to manage habit data and the development of a versatile menu system. This menu system allowed users to loop back to the main interface after completing tasks, enhancing the overall usability of the application. Additionally, the decision to separate streaks and checkoffs into distinct database tables improved the efficiency of data retrieval and analysis, supporting the generation of meaningful user insights.
Overall, the implementation adhered closely to the initial design, with adjustments made as necessary to address challenges and optimize performance. The resulting application provided a robust and intuitive tool for habit tracking, backed by a solid foundation of well-structured code and comprehensive testing.
Results
The result of this work is an application called MindMold, a user-friendly command-line interface (CLI) application designed to help individuals cultivate and maintain beneficial habits. It simplifies the process of habit tracking, enabling users to create, monitor, and analyse their habits over time. With MindMold, users can easily manage their daily routines and work towards achieving their personal growth goals.
Features
Create Habits: Add new habits with names and periodicities.
View Habits: See a list of all your habits and their details.
Update Habits: Change the details of an existing habit.
Complete Habits: Mark habits as completed and track progress.
Analyse Habits: Analyse habits with comprehensive streak data.
Predefined Demos: View demo analyses using predefined habits.
Screenshots
Reflections, obstacles and challenges
Storing error messages:
Initially, the idea was to create a table with error messages however, the number of error messages being small and static, a database table would have been an over-complication for this need. Therefore, I decided to directly include error messages in error_handler class for simplicity and direct access, reducing database queries and improving performance.
Handling predefined habits
I evaluated the possibility to give predefined habits their own table and functions. In the end, the app stores predefined and user-created habits in one table, distinguished by 'created_by' and 'is_active' fields and uses
same functions to reduce complexity and streamline function calls. This unified approach allows the use of a single set of functions for habit retrieval and manipulation, facilitating a cleaner codebase and easier maintenance.
Functions in main.py:
To simplify the main.py file and improve code clarity, independent functions were moved to a separate file. This made the main code cleaner and easier to update.
Tables Streaks and Checkoffs:
Separating streaks and checkoffs into distinct database tables was a strategic choice to allow more focused queries and to have efficient data retrieval. The Streaks table records the ongoing streaks for each habit, capturing the duration and continuity of user engagement, while the Checkoffs table logs specific instances when users mark habits as completed, each associated with a precise date.
Habit deleted by user:
I decided against permanent deletion to preserve data for analytics and user flexibility. Habits are marked 'inactive' instead, retaining history and allowing reactivation if desired.
Lesson learned - take-aways
The key take-away from this project has been the profound importance of the conception phase. It has become clear to me that laying a solid foundation through meticulous planning is of highest importance. Listing application requirements, writing use cases and building diagrams seems to be the best practice. However, this project has also been a reminder that no plan is resistant to the unexpected; flexibility and adaptability are indispensable virtues in software development. Being able to pivot and tackle unexpected challenges without compromising the project's integrity has been a vital learning curve and a testament to the robustness of the initial design.
Tools used
Python with Pycharm
SQLite
Comments