Skip to content

Commit

Permalink
Update nutrient info after Nelson-Gon/rfdc#2 todo:tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-Gon committed Oct 3, 2021
1 parent f0635d0 commit 9bcbcf4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,25 @@ To get nutrient details:

my_search.get_food_details(fdc_id= 496446,target_field="nutrients")

# id number name rank unitName
#0 2045 951 Proximates 50 g
#1 1051 255 Water 100 g
#2 1008 208 Energy 300 kcal
#3 1062 268 Energy 400 kJ
#4 1003 203 Protein 600 g
#5 1004 204 Total lipid (fat) 800 g



# nutrient.id nutrient.unitName nutrient.name \
#0 1089 mg Iron, Fe
#1 1258 g Fatty acids, total saturated
#2 1253 mg Cholesterol
#3 1162 mg Vitamin C, total ascorbic acid
#4 1087 mg Calcium, Ca
#5 1104 IU Vitamin A, IU
# foodNutrientDerivation.description id amount \
#0 Calculated from a daily value percentage per s... 3992103 3.39
#1 Calculated from a daily value percentage per s... 3992107 0.47
#2 Calculated from a daily value percentage per s... 3992106 0.00
#3 Calculated from a daily value percentage per s... 3992105 0.00
#4 Calculated from a daily value percentage per s... 3992102 118.00
#5 Calculated from a daily value percentage per s... 3992104 0.00
# nutrient.number nutrient.rank
#0 303 5400
#1 606 9700
#2 601 15700
#3 401 6300

```

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

**Version 0.2.2**

* Added nutrition information after https://github.com/Nelson-Gon/rfdc/issues/2

* Fixed a bug that caused wrong output in `get_food_info`.

* Now supporting a script mode.
Expand Down
26 changes: 19 additions & 7 deletions docs/source/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,25 @@ To get nutrient details:
my_search.get_food_details(fdc_id= 496446,target_field="nutrients")
# id number name rank unitName
#0 2045 951 Proximates 50 g
#1 1051 255 Water 100 g
#2 1008 208 Energy 300 kcal
#3 1062 268 Energy 400 kJ
#4 1003 203 Protein 600 g
#5 1004 204 Total lipid (fat) 800 g
# nutrient.id nutrient.unitName nutrient.name \
#0 1089 mg Iron, Fe
#1 1258 g Fatty acids, total saturated
#2 1253 mg Cholesterol
#3 1162 mg Vitamin C, total ascorbic acid
#4 1087 mg Calcium, Ca
#5 1104 IU Vitamin A, IU
# foodNutrientDerivation.description id amount \
#0 Calculated from a daily value percentage per s... 3992103 3.39
#1 Calculated from a daily value percentage per s... 3992107 0.47
#2 Calculated from a daily value percentage per s... 3992106 0.00
#3 Calculated from a daily value percentage per s... 3992105 0.00
#4 Calculated from a daily value percentage per s... 3992102 118.00
#5 Calculated from a daily value percentage per s... 3992104 0.00
# nutrient.number nutrient.rank
#0 303 5400
#1 606 9700
#2 601 15700
#3 401 6300
**Credit**

Expand Down
3 changes: 3 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Welcome to pyfdc's changelog
**Version 0.2.2**


*
Added nutrition information after https://github.com/Nelson-Gon/rfdc/issues/2

*
Fixed a bug that caused wrong output in ``get_food_info``.

Expand Down
11 changes: 8 additions & 3 deletions pyfdc/pyfdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def get_food_details(self, fdc_id=None, target_field=None):
# Replace in base url so we have only for a specific FDC ID.
assert fdc_id is not None, "fdc_id should not be None"
assert isinstance(fdc_id, int), f"fdc_id should be an int not {type(fdc_id).__name__}"
base_url = self.base_url.replace("foods/search", f"{fdc_id}")
base_url = self.base_url.replace("foods/search", f"food/{fdc_id}")
url_response = requests.get(base_url, headers={"User-Agent": "Mozilla-5.0"})
url_response.raise_for_status()
result = url_response.json()
Expand All @@ -162,7 +162,12 @@ def get_food_details(self, fdc_id=None, target_field=None):

if target_field == "nutrients":
result = result["foodNutrients"]
return json_normalize(DataFrame(result)["nutrient"])
return json_normalize(result)[{'id', 'amount', 'nutrient.id', 'nutrient.number',
'nutrient.name', 'nutrient.rank', 'nutrient.unitName',
'foodNutrientDerivation.description'}]

else:
return result[target_field]
return result[target_field]

my_search = FoodDataCentral(api_key="vuF8C2aKQOq2K5ZUyq175VPh3YbKgnMX0kTIvY9z")
my_search.get_food_details(fdc_id=496446, target_field="nutrients").head(6)

0 comments on commit 9bcbcf4

Please sign in to comment.