Categories We Write About

Build a command-line habit tracker

A command-line habit tracker is a simple yet powerful way to track daily habits and maintain consistency over time. Below is a Python-based command-line habit tracker using a local JSON file for storage. This script supports adding, viewing, completing, and deleting habits.


Features

  • Add new habits

  • Mark habits as completed for the day

  • View status of habits

  • Delete habits

  • Store data persistently in a JSON file


Requirements

  • Python 3.x

  • No external libraries needed (uses built-in modules)


Code: habit_tracker.py

python
import json import os from datetime import datetime DATA_FILE = "habits.json" def load_data(): if os.path.exists(DATA_FILE): with open(DATA_FILE, "r") as file: return json.load(file) return {} def save_data(data): with open(DATA_FILE, "w") as file: json.dump(data, file, indent=4) def add_habit(name): data = load_data() if name in data: print(f"Habit '{name}' already exists.") else: data[name] = {"created": str(datetime.today().date()), "log": []} save_data(data) print(f"Habit '{name}' added.") def complete_habit(name): data = load_data() if name not in data: print(f"No such habit: {name}") return today = str(datetime.today().date()) if today in data[name]["log"]: print(f"Habit '{name}' already marked as complete for today.") else: data[name]["log"].append(today) save_data(data) print(f"Habit '{name}' marked as complete for {today}.") def view_habits(): data = load_data() if not data: print("No habits found.") return today = str(datetime.today().date()) for habit, info in data.items(): is_done = today in info["log"] status = "✅" if is_done else "❌" print(f"{habit}: {status} (Started on {info['created']})") def delete_habit(name): data = load_data() if name in data: del data[name] save_data(data) print(f"Habit '{name}' deleted.") else: print(f"No such habit: {name}") def show_menu(): print("nHabit Tracker") print("1. Add a new habit") print("2. Mark habit as completed") print("3. View habits") print("4. Delete a habit") print("5. Exit") def main(): while True: show_menu() choice = input("Choose an option: ").strip() if choice == "1": name = input("Enter habit name: ").strip() add_habit(name) elif choice == "2": name = input("Enter habit name to mark as completed: ").strip() complete_habit(name) elif choice == "3": view_habits() elif choice == "4": name = input("Enter habit name to delete: ").strip() delete_habit(name) elif choice == "5": print("Goodbye!") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()

How to Use

  1. Save the code in a file named habit_tracker.py

  2. Run it in terminal:

    nginx
    python habit_tracker.py
  3. Follow the interactive menu to manage your habits.


This tracker is designed for local use. If you want to extend its functionality later, consider adding:

  • Weekly/monthly stats

  • Notifications/reminders

  • Integration with Google Sheets or a database

  • A web or GUI interface

Let me know if you’d like an extended version with any of these features.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About