MyFitnessPal to Grafana


Thrusday, June 04, 2020
Posted by Tushar Nitave, Dept. of Computer Science, Illinois Tech, Chicago


image source: https://grafana.com/

MyFitnessPal is a smartphone app and website that tracks diet and exercise. The app uses gamification elements to motivate users. To track nutrients, users can either scan the barcodes of various food items or manually find them in the app's large pre-existing database. It exports all the saved data in CSV format.

Grafana is a open source analytics and visualization web application. We will use it to visualize the data exported from MyFitnessPal.

In this post we will design a simple website which imports data in CSV format and visualizes in Grafana dashboard.

First we will write a python code to export data from MyFitnessPal. You can do this from the app/website as well but this feature is only available for premium users. So, we will use a open-source library which can be used to export the data for free of cost. Checkout the library on GitHub here.


Extracting data from MyFitnessPal


In the first step we will import all the necessary libraries. myfitnesspal is a library we need to get our data from MyFitnessPal, datetime is used to get the days from each month. It will be more clear when will actually use it, pandas library is required to extract data from CSV file, sqlalchemy is used to store the extracted data into mysql database.

# import necessary libraries
import myfitnesspal
import datetime
from calendar import monthrange
import pandas as pd
import sqlalchemy as db
import pymysql


Replace username with your MyFitnessPal account username. The following fig shows the output when the code is executed.

client = myfitnesspal.Client(username)

# extracting data for a specific day
day = client.get_date(2020, 3, 16)

user@user:~$ 03/16/20 {'calories': 1140, 'carbohydrates': 126, 'fat': 24, 'protein': 65, 'sodium': 471, 'sugar': 33}


Now we will iterate over all the days of each and every month to get the data and write it to the csv file.

with open("filename.csv", "w") as file:
  for month in range(1, 13):
    for day in range(1, monthrange(2020, month)[1]+1):
      macros = client.get_date(2020, month, day)
        if len(macros.totals) != 0:
          macros = macros.totals date = "2020/"+str(month)+"/"+str(day)
          file.write(date)
          file.write("\t")
          for i, j in macros.items():
            file.write(str(j))
            file.write("\t")
          file.write("\t")
          date = ""
file.close()

Now we will save our extracted data into MySQL database. Here data is the database and name of the table is macros. Below is the screenshot of the final MySQL table.

engine = db.create_engine("mysql://myfitnesspal:root@localhost:3306/data")
data = pd.read_csv("nutrition_data.csv", sep="\t")
data.to_sql(name="macros", con=engine, if_exists="replace")


Visualizing on Grafana


Follow this to install Grafana on your system. Once installed check the version of Grafana using the following command.

user@user:~$ grafana-server -v
Version 6.6.2 (commit: 3fa63cfc34, branch: HEAD)


Start the grafana server by executing the following command and access it from the brower at localhost:3000/. The default username and password is "admin" (without quotes).

user@user:~$ sudo systemctl start grafana-server.service



Once you login into Grafana, click on configuration icon from the panel located on left side and select Data Sources as shown.