Given that python is available on iOS, I decided to see what I could do with it. In this post, I’ll describe one experiment that I tried — the purpose of this post is to give you an idea of what you can do with python, I hope it inspires you to try new ideas of your own.

iOS devices contain a wealth of health data, and while the built in health app does a good job of presenting the data in a readable manner, and there are many third party apps that provide more detailed analysis, the hacker in me would not rest until I could extract the data from the app and do my own personal analytics with custom programs.

There are probably many different ways to do this, but I chose to extract the data with the Shortcuts app and then send the data to python for processing. Here is a screenshot of the shortcut that does this for me.

As can be seen, the shortcut collects the number of steps for the last two years, places this data on a variable and then calls a python script. Below is the code for the python script:

import clipboard
import sys
import numpy as np
import matplotlib.pyplot as plt
def main():
	tot = len(sys.argv)
	x = []
	for i in range(tot-1):
		text = sys.argv[i+1]
		if (int(text)!=0):
			print(text)
			x.append(int(text))
	arr = np.array(x)
	
	max = np.amax(arr)
	print('Max number of steps %s' % max)
	plt.plot(arr)
	plt.show()
	
if __name__ == '__main__':
	main()

As can be seen, the code takes the input from the shortcut and places it into an array. It then uses matplotlib to draw a simple plot of the data.

That’s it, those few lines of code allow me to draw a time series of my steps each day for the last two years. Given the wealth of data in the Health App, and the expressive power of python, the only limit on what you can do is your imagination. Below is the plot of this shortcut.

That spike in the chart was when I trained and ran for a marathon. As I play around more with the app, I’m sure that I’ll find more interesting things to do. I’ll post anything I find interesting on this blog.

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.