Details for MyFitnessPal Data Export.ipynb

Published by madprime


Export your data from MyFitnessPal into a CSV file.


Tags & Data Sources

food log myfitnesspal username


Please log in to comment.

Last updated 7 months, 1 week ago

Retrieve MyFitnessPal diet logs

This notebook scrapes the MyFitnessPal website to get your food logs. It's a hack, but...

This runs in your personal, ephemeral virtual machine.

MyFitnessPal charges a monthly subscription for data export, but you can use this instead. ;)

Step 1: Import the library

This is another hack, but this installs the library you need if it's not already here.

In [1]:
def install_and_import(package):
    import importlib
    except ImportError:
        import subprocess
        import sys[sys.executable, "-m", "pip", "install", package])
        globals()[package] = importlib.import_module(package)


Step 2: Get username / password

After you enter it, your password is deliberately hidden below. We don't want it stored in the notebook!

In [2]:
mfp_username = input("What is your username? ")
mfp_password = input("What is your password? ")
numdays = int(input("How many days do you want to collect? "))

from IPython.display import clear_output


print("Username: {}".format(mfp_username))
print("Days to collect: {}".format(numdays))
Username: madprime
Days to collect: 14
In [3]:
import csv
import os

import arrow
import requests

print("Getting your Open Humans username, which we need for the filepath at the end...")
response = requests.get(
oh_username = response.json()['username']

print("Initiating a login to MyFitnessPal...")
client = myfitnesspal.Client(mfp_username, password=mfp_password)

row_items = ['date', 'meal', 'name', 'calories', 'carbohydrates', 'fat', 'protein', 'sodium', 'sugar']

def get_day(year, month, day):
    data = []
    day = client.get_date(year, month, day)
    for meal in day.keys():
        for entry in day[meal].get_as_list():
            row_data = [meal] + [entry['name']] + [
                entry['nutrition_information'][item] for item in
    return data

today = arrow.get()
filename = '{}-myfitnesspal-data.csv'.format(today.format('YYYYMMDD'))
out = open(filename, 'w')
csv_out = csv.writer(out)


for i in range(0, -1 * numdays, -1):
    target_day = today.shift(days=i)
    print("Retrieving {}-{}-{}...".format(target_day.year, target_day.month,
    data = get_day(target_day.year, target_day.month,
    for row in data:
        csv_out.writerow([target_day.format('YYYY-MM-DD')] + row)

print("\nDone! Click this link to download your data:")
print("{}/tree/{}".format(oh_username, filename))
Getting your Open Humans username, which we need for the filepath at the end...
Initiating a login to MyFitnessPal...
Retrieving 2019-6-14...
Retrieving 2019-6-13...
Retrieving 2019-6-12...
Retrieving 2019-6-11...
Retrieving 2019-6-10...
Retrieving 2019-6-9...
Retrieving 2019-6-8...
Retrieving 2019-6-7...
Retrieving 2019-6-6...
Retrieving 2019-6-5...
Retrieving 2019-6-4...
Retrieving 2019-6-3...
Retrieving 2019-6-2...
Retrieving 2019-6-1...

Done! Click this link to download your data: