Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Last active January 1, 2019 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/e63363836aedf54ffc50c71c0b57c73e to your computer and use it in GitHub Desktop.
Save dustinknopoff/e63363836aedf54ffc50c71c0b57c73e to your computer and use it in GitHub Desktop.
Pythonista compatible python script for keeping track of and visualizing your numerical answer to daily questions.

Daily Tracker

How to Install

This script is intended to run in Pythonista on iOS. It works equally well on desktop OSs. Installation instructions will be solely for Pythonista.

  1. Copy the contents of this
  2. Open a new file in Pythonista called dailytracker.py and paste the contents.
  3. Click the wrench icon.
  4. Click Home Screen
  5. Choose your own Title, Icon, Background Color, but keep it on Run Script
  6. This opens a new tab in Safari. Click the Share icon and click Add to Home Screen.
  7. Follow steps to add to home screen.
  8. Done!

Usage

The app provides statements describing most of the functionality provided.

import numpy as np
import json
from matplotlib import pyplot as plt
from os import path
from datetime import datetime, timedelta
consts = {
1: 2,
2: 7,
3: 30,
4: 365,
5: 1000
}
def set_questions():
"""
Writes input text to a file `questions.txt`, separated by a new line.
This will represent the questions to be asked
"""
print("Please enter each of your questions separated by a new line, enter just 'q' to finish")
with open('./questions.txt', 'w') as f:
while 1:
question = input()
if question is 'q':
break
else:
f.write(question)
f.write("\n")
with open('./data.json', 'w') as f:
f.write("{}")
def enter_scores(check):
"""
For every question in questions.txt, ask for an integer value and add to data.json
:return: dictionary of questions to list of responses
"""
if check:
if (datetime.today() - timedelta(1)).strftime("%d%m%y") != datetime.fromtimestamp(
path.getmtime('./data.json')).strftime("%d%m%y"):
print("Whoops, you tried to enter a value twice in one day!")
exit(1)
print("Please enter your scores for today")
with open("./data.json", 'r') as f:
# Define 10 questions within the triple quotes separated by a new line
questions = []
with open('./questions.txt', 'r') as qs:
questions = qs.read().split('\n')
kv = json.load(f)
for line in questions:
if line is not '':
answer = int(input(line))
while answer < 0 or answer > 10:
print("Please enter a number between 0-10")
answer = int(input(line))
if line not in kv:
kv[line] = [answer]
else:
kv[line].append(answer)
with open('./data.json', 'w') as f:
json.dump(kv, f)
print("Awesome! Thanks for adding your scores today")
return kv
def show_graph(freq, kv):
"""
Given a dictionary of questions and frequency, output a matplotlib graph of all questions
:param freq: Number of data points to show
:param kv: dictionary of questions to list of answers
"""
plt.title('Scores')
lst = []
ln = 0
for key in kv.keys():
plt.subplot(111)
if freq != 1000:
ln = len(kv[key][:freq])
mx = len(kv[key][:freq])
rng = range(0, len(kv[key][:freq]))
most = list(reversed(kv[key][:freq]))
else:
ln = len(kv[key])
rng = range(0, len(kv[key]))
mx = len(kv[key])
most = list(reversed(kv[key]))
for i in rng:
lst.append((datetime.now() - timedelta(days=i)).strftime('%b, %d %Y'))
x = np.arange(0, mx)
y = np.array(most)
plt.plot(x, y, label=key.replace("?", ""))
plt.legend()
plt.xticks(range(0, ln), list(reversed(lst)), rotation=45)
plt.xlabel('Day')
plt.show()
should_save = input("Would you like to save this graph?\n1) yes\n2) no\n")
if should_save is '1':
plt.savefig('latest.png')
if __name__ == '__main__':
print("Welcome! This is a program to help keep track of and visualize things you find important"
" everyday")
yes = False
while 1:
print("Is this your first time?\n1) yes\n2) no")
is_first = input()
if int(is_first):
if int(is_first) == 1:
yes = True
set_questions()
break
else:
print("Please enter 1 or 0")
kv = enter_scores(yes)
while 1:
print("How would you like to see this data?\n"
"1) Past 2 Days\n"
"2) This Week\n"
"3) This Month\n"
"4) This Year\n"
"5) All Time")
freq = input()
if int(freq):
show_graph(consts[int(freq)], kv)
break
else:
print("Please enter 1 or 0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment