Skip to content

Instantly share code, notes, and snippets.

@gkbrk
Last active December 30, 2023 18:11
Show Gist options
  • Save gkbrk/a610af06f5440836aa8f4844c596dec4 to your computer and use it in GitHub Desktop.
Save gkbrk/a610af06f5440836aa8f4844c596dec4 to your computer and use it in GitHub Desktop.
Todo.txt to graph
#!/usr/bin/python3
import matplotlib.pyplot as plt
import datetime
def get_stats(filename):
data = {}
with open(filename) as todofile:
for line in todofile:
date = line.split()[1]
if date in data:
data[date] += 1
else:
data[date] = 1
return data
def get_last_days(days):
for day in range(days)[::-1]:
date = datetime.datetime.today() - datetime.timedelta(days=day)
yield date.date().isoformat()
plt.grid(True)
plt.title("Todo.txt Progress")
plt.ylabel("Number of tasks done")
stats = get_stats("/home/leonardo/Sync/default/done.txt")
todoCounts = []
for date in get_last_days(15):
todoCounts.append(stats.get(date, 0))
plt.yticks(range(max(todoCounts) + 1))
plt.xticks(range(15), get_last_days(15), rotation=80)
plt.plot(todoCounts, marker="o")
plt.tight_layout()
plt.savefig("public/img/todo.jpeg", dpi=100)
@benevidesh
Copy link

Thanks for sharing! I made some tweaks into your script so that the path could be specified as an argument to the script. Check it out!

#!/usr/bin/python3
import matplotlib.pyplot as plt
import datetime
import sys
import os

def get_stats(filename):
    data = {}
    with open(filename) as todofile:
        for line in todofile:
            date = line.split()[1]
            if date in data:
                data[date] += 1
            else:
                data[date] = 1
    return data

def get_last_days(days):
    for day in range(days)[::-1]:
        date = datetime.datetime.today() - datetime.timedelta(days=day)
        yield date.date().isoformat()

plt.grid(False)
plt.title("Todo.txt Progress")
plt.ylabel("Number of tasks done")

if len(sys.argv) <=1:
    print("No file was provided")
    sys.exit()

if os.path.isfile(sys.argv[1]):
    stats = get_stats(sys.argv[1])
else:
    print("File does not exist")

todoCounts = []
for date in get_last_days(15):
    todoCounts.append(stats.get(date, 0))

plt.yticks(range(max(todoCounts) + 1))
plt.xticks(range(15), get_last_days(15), rotation=80)

plt.plot(todoCounts, marker="o")

plt.tight_layout()
plt.savefig("todo.jpeg", dpi=100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment