Kaludi commited on
Commit
2ddade0
·
1 Parent(s): 55c07ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -72
app.py CHANGED
@@ -157,7 +157,7 @@ def main():
157
  response_json = json.loads(response.content)
158
  # Convert response_json to a list
159
  response_json = list(response_json)
160
-
161
  if len(response_json) == 0:
162
  st.markdown("### No Recipe Found:")
163
  st.write("**No recipes found. Please adjust your search criteria.**")
@@ -181,67 +181,100 @@ def main():
181
  st.write("**Description:** ", random_recipe['Description'])
182
  st.write("**Ingredients:** ", random_recipe['Ingredients'])
183
  st.write("**Recipe Facts:** ", random_recipe['Recipe Facts'])
184
- st.write("**Directions:** ", random_recipe['Directions'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  nutrition_html = """
186
  <div id="nutrition-info_6-0" class="comp nutrition-info">
187
  <table class="nutrition-info__table">
188
  <thead>
189
  <tr>
190
- <th class="nutrition-info__heading" colspan="2">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
191
  </tr>
192
  </thead>
193
  <tbody class="nutrition-info__table--body">
194
  <tr class="nutrition-info__table--row">
195
  <td class="nutrition-info__table--cell">Calories</td>
196
  <td class="nutrition-info__table--cell">{calories}</td>
 
197
  </tr>
198
  <tr class="nutrition-info__table--row">
199
  <td class="nutrition-info__table--cell">Total Fat</td>
200
  <td class="nutrition-info__table--cell">{total_fat}</td>
 
201
  </tr>
202
  <tr class="nutrition-info__table--row">
203
  <td class="nutrition-info__table--cell">Saturated Fat</td>
204
  <td class="nutrition-info__table--cell">{saturated_fat}</td>
 
205
  </tr>
206
  <tr class="nutrition-info__table--row">
207
  <td class="nutrition-info__table--cell">Cholesterol</td>
208
  <td class="nutrition-info__table--cell">{cholesterol}</td>
 
209
  </tr>
210
  <tr class="nutrition-info__table--row">
211
  <td class="nutrition-info__table--cell">Sodium</td>
212
  <td class="nutrition-info__table--cell">{sodium}</td>
 
213
  </tr>
214
  <tr class="nutrition-info__table--row">
215
  <td class="nutrition-info__table--cell">Total Carbohydrate</td>
216
  <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
 
217
  </tr>
218
  <tr class="nutrition-info__table--row">
219
  <td class="nutrition-info__table--cell">Dietary Fiber</td>
220
  <td class="nutrition-info__table--cell">{dietary_fiber}</td>
 
221
  </tr>
222
  <tr class="nutrition-info__table--row">
223
  <td class="nutrition-info__table--cell">Total Sugars</td>
224
  <td class="nutrition-info__table--cell">{total_sugars}</td>
 
225
  </tr>
226
  <tr class="nutrition-info__table--row">
227
  <td class="nutrition-info__table--cell">Protein</td>
228
  <td class="nutrition-info__table--cell">{protein}</td>
 
229
  </tr>
230
  <tr class="nutrition-info__table--row">
231
  <td class="nutrition-info__table--cell">Vitamin C</td>
232
  <td class="nutrition-info__table--cell">{vitc}</td>
 
233
  </tr>
234
  <tr class="nutrition-info__table--row">
235
  <td class="nutrition-info__table--cell">Calcium</td>
236
  <td class="nutrition-info__table--cell">{calc}</td>
 
237
  </tr>
238
  <tr class="nutrition-info__table--row">
239
  <td class="nutrition-info__table--cell">Iron</td>
240
  <td class="nutrition-info__table--cell">{iron}</td>
 
241
  </tr>
242
  <tr class="nutrition-info__table--row">
243
  <td class="nutrition-info__table--cell">Potassium</td>
244
  <td class="nutrition-info__table--cell">{pota}</td>
 
245
  </tr>
246
  </tbody>
247
  </table>
@@ -262,7 +295,20 @@ def main():
262
  calc=random_recipe['Calcium'],
263
  iron=random_recipe['Iron'],
264
  pota=random_recipe['Potassium'],
265
- protein=random_recipe['Protein']
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  )
267
 
268
  # Define a function to apply the CSS styles to the table cells
@@ -273,22 +319,7 @@ def main():
273
  # Add the nutrition table to the Streamlit app
274
  st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
275
  st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
276
- st.write("<p style='text-align:left;'>*Nutrition information is calculated using an ingredient database and should be considered an estimate.</p>", unsafe_allow_html=True)
277
- # extract only numeric values and convert mg to g
278
- values = [
279
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Fat'])),
280
- float(re.sub(r'[^\d.]+', '', random_recipe['Saturated Fat'])),
281
- float(re.sub(r'[^\d.]+', '', random_recipe['Cholesterol'])) / 1000,
282
- float(re.sub(r'[^\d.]+', '', random_recipe['Sodium'])) / 1000,
283
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Carbohydrate'])),
284
- float(re.sub(r'[^\d.]+', '', random_recipe['Dietary Fiber'])),
285
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Sugars'])),
286
- float(re.sub(r'[^\d.]+', '', random_recipe['Protein'])),
287
- float(re.sub(r'[^\d.]+', '', random_recipe['Vitamin C'])) / 1000,
288
- float(re.sub(r'[^\d.]+', '', random_recipe['Calcium'])) / 1000,
289
- float(re.sub(r'[^\d.]+', '', random_recipe['Iron'])) / 1000,
290
- float(re.sub(r'[^\d.]+', '', random_recipe['Potassium'])) / 1000
291
- ]
292
  # create pie chart
293
  labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
294
  fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
@@ -310,67 +341,100 @@ def main():
310
  st.write("**Description:** ", response_json[0]['Description'])
311
  st.write("**Ingredients:** ", response_json[0]['Ingredients'])
312
  st.write("**Recipe Facts:** ", response_json[0]['Recipe Facts'])
313
- st.write("**Directions:** ", random_recipe['Directions'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  nutrition_html = """
315
  <div id="nutrition-info_6-0" class="comp nutrition-info">
316
  <table class="nutrition-info__table">
317
  <thead>
318
  <tr>
319
- <th class="nutrition-info__heading" colspan="2">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
320
  </tr>
321
  </thead>
322
  <tbody class="nutrition-info__table--body">
323
  <tr class="nutrition-info__table--row">
324
  <td class="nutrition-info__table--cell">Calories</td>
325
  <td class="nutrition-info__table--cell">{calories}</td>
 
326
  </tr>
327
  <tr class="nutrition-info__table--row">
328
  <td class="nutrition-info__table--cell">Total Fat</td>
329
  <td class="nutrition-info__table--cell">{total_fat}</td>
 
330
  </tr>
331
  <tr class="nutrition-info__table--row">
332
  <td class="nutrition-info__table--cell">Saturated Fat</td>
333
  <td class="nutrition-info__table--cell">{saturated_fat}</td>
 
334
  </tr>
335
  <tr class="nutrition-info__table--row">
336
  <td class="nutrition-info__table--cell">Cholesterol</td>
337
  <td class="nutrition-info__table--cell">{cholesterol}</td>
 
338
  </tr>
339
  <tr class="nutrition-info__table--row">
340
  <td class="nutrition-info__table--cell">Sodium</td>
341
  <td class="nutrition-info__table--cell">{sodium}</td>
 
342
  </tr>
343
  <tr class="nutrition-info__table--row">
344
  <td class="nutrition-info__table--cell">Total Carbohydrate</td>
345
  <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
 
346
  </tr>
347
  <tr class="nutrition-info__table--row">
348
  <td class="nutrition-info__table--cell">Dietary Fiber</td>
349
  <td class="nutrition-info__table--cell">{dietary_fiber}</td>
 
350
  </tr>
351
  <tr class="nutrition-info__table--row">
352
  <td class="nutrition-info__table--cell">Total Sugars</td>
353
  <td class="nutrition-info__table--cell">{total_sugars}</td>
 
354
  </tr>
355
  <tr class="nutrition-info__table--row">
356
  <td class="nutrition-info__table--cell">Protein</td>
357
  <td class="nutrition-info__table--cell">{protein}</td>
 
358
  </tr>
359
  <tr class="nutrition-info__table--row">
360
  <td class="nutrition-info__table--cell">Vitamin C</td>
361
  <td class="nutrition-info__table--cell">{vitc}</td>
 
362
  </tr>
363
  <tr class="nutrition-info__table--row">
364
  <td class="nutrition-info__table--cell">Calcium</td>
365
  <td class="nutrition-info__table--cell">{calc}</td>
 
366
  </tr>
367
  <tr class="nutrition-info__table--row">
368
  <td class="nutrition-info__table--cell">Iron</td>
369
  <td class="nutrition-info__table--cell">{iron}</td>
 
370
  </tr>
371
  <tr class="nutrition-info__table--row">
372
  <td class="nutrition-info__table--cell">Potassium</td>
373
  <td class="nutrition-info__table--cell">{pota}</td>
 
374
  </tr>
375
  </tbody>
376
  </table>
@@ -378,20 +442,33 @@ def main():
378
  """
379
  # Use the nutrition HTML and format it with the values
380
  formatted_html = nutrition_html.format(
381
- calories=random_recipe['Calories'],
382
- total_fat=random_recipe['Total Fat'],
383
- saturated_fat=random_recipe['Saturated Fat'],
384
- cholesterol=random_recipe['Cholesterol'],
385
- sodium=random_recipe['Sodium'],
386
- total_carbohydrate=random_recipe['Total Carbohydrate'],
387
- dietary_fiber=random_recipe['Dietary Fiber'],
388
- total_sugars=random_recipe['Total Sugars'],
389
- servings=random_recipe['Number of Servings'],
390
- vitc=random_recipe['Vitamin C'],
391
- calc=random_recipe['Calcium'],
392
- iron=random_recipe['Iron'],
393
- pota=random_recipe['Potassium'],
394
- protein=random_recipe['Protein']
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  )
396
 
397
  # Define a function to apply the CSS styles to the table cells
@@ -402,50 +479,19 @@ def main():
402
  # Add the nutrition table to the Streamlit app
403
  st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
404
  st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
405
- st.write("<p style='text-align:left;'>*Nutrition information is calculated using an ingredient database and should be considered an estimate.</p>", unsafe_allow_html=True)
406
- # extract only numeric values and convert mg to g
407
- values = [
408
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Fat'])),
409
- float(re.sub(r'[^\d.]+', '', random_recipe['Saturated Fat'])),
410
- float(re.sub(r'[^\d.]+', '', random_recipe['Cholesterol'])) / 1000,
411
- float(re.sub(r'[^\d.]+', '', random_recipe['Sodium'])) / 1000,
412
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Carbohydrate'])),
413
- float(re.sub(r'[^\d.]+', '', random_recipe['Dietary Fiber'])),
414
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Sugars'])),
415
- float(re.sub(r'[^\d.]+', '', random_recipe['Protein'])),
416
- float(re.sub(r'[^\d.]+', '', random_recipe['Vitamin C'])) / 1000,
417
- float(re.sub(r'[^\d.]+', '', random_recipe['Calcium'])) / 1000,
418
- float(re.sub(r'[^\d.]+', '', random_recipe['Iron'])) / 1000,
419
- float(re.sub(r'[^\d.]+', '', random_recipe['Potassium'])) / 1000
420
- ]
421
  # create pie chart
422
  labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
423
  fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
424
  st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
425
- st.plotly_chart(fig)
426
- st.write("**Tags:** ", random_recipe['Tags'])
427
- st.write("**Recipe URL:** ", random_recipe['Recipe URLs'])
428
  st.markdown("### JSON Response:")
429
- st.write(response_json)
430
 
431
  st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
432
  st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
433
 
434
  if __name__ == '__main__':
435
  main()
436
-
437
-
438
- ## Fetch random recipes:
439
- ## Fetch the healthy recipe
440
- #response = requests.get("test.org/test.json")
441
- #data = response.json()
442
- #healthy_recipes = [recipe for recipe in data['recipe'] if recipe['calories'] < int(calories)]
443
- #healthy_recipe = random.choice(healthy_recipes)
444
-
445
- ## Fetch the unhealthy recipe
446
- #unhealthy_recipes = [recipe for recipe in data['recipe'] if recipe['calories'] < int(calories)]
447
- #unhealthy_recipe = random.choice(unhealthy_recipes)
448
-
449
- ## Add code to display the healthy and unhealthy recipes
450
- #st.write("Healthy Recipe: ", healthy_recipe['name'], "Calories: ", healthy_recipe['calories'])
451
- #st.write("Unhealthy Recipe: ", unhealthy_recipe['name'], "Calories: ", unhealthy_recipe['calories'])
 
157
  response_json = json.loads(response.content)
158
  # Convert response_json to a list
159
  response_json = list(response_json)
160
+
161
  if len(response_json) == 0:
162
  st.markdown("### No Recipe Found:")
163
  st.write("**No recipes found. Please adjust your search criteria.**")
 
181
  st.write("**Description:** ", random_recipe['Description'])
182
  st.write("**Ingredients:** ", random_recipe['Ingredients'])
183
  st.write("**Recipe Facts:** ", random_recipe['Recipe Facts'])
184
+ st.write("**Directions:** ", random_recipe['Directions'])
185
+ # extract only numeric values and convert mg to g
186
+ values = [
187
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Fat'])),
188
+ float(re.sub(r'[^\d.]+', '', random_recipe['Saturated Fat'])),
189
+ float(re.sub(r'[^\d.]+', '', random_recipe['Cholesterol'])) / 1000,
190
+ float(re.sub(r'[^\d.]+', '', random_recipe['Sodium'])) / 1000,
191
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Carbohydrate'])),
192
+ float(re.sub(r'[^\d.]+', '', random_recipe['Dietary Fiber'])),
193
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Sugars'])),
194
+ float(re.sub(r'[^\d.]+', '', random_recipe['Protein'])),
195
+ float(re.sub(r'[^\d.]+', '', random_recipe['Vitamin C'])) / 1000,
196
+ float(re.sub(r'[^\d.]+', '', random_recipe['Calcium'])) / 1000,
197
+ float(re.sub(r'[^\d.]+', '', random_recipe['Iron'])) / 1000,
198
+ float(re.sub(r'[^\d.]+', '', random_recipe['Potassium'])) / 1000
199
+ ]
200
+ # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
201
+ dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
202
+
203
+ # Calculate the percentage of DV for each nutrient
204
+ dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
205
  nutrition_html = """
206
  <div id="nutrition-info_6-0" class="comp nutrition-info">
207
  <table class="nutrition-info__table">
208
  <thead>
209
  <tr>
210
+ <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
211
  </tr>
212
  </thead>
213
  <tbody class="nutrition-info__table--body">
214
  <tr class="nutrition-info__table--row">
215
  <td class="nutrition-info__table--cell">Calories</td>
216
  <td class="nutrition-info__table--cell">{calories}</td>
217
+ <td class="nutrition-info__table--cell"></td>
218
  </tr>
219
  <tr class="nutrition-info__table--row">
220
  <td class="nutrition-info__table--cell">Total Fat</td>
221
  <td class="nutrition-info__table--cell">{total_fat}</td>
222
+ <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
223
  </tr>
224
  <tr class="nutrition-info__table--row">
225
  <td class="nutrition-info__table--cell">Saturated Fat</td>
226
  <td class="nutrition-info__table--cell">{saturated_fat}</td>
227
+ <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
228
  </tr>
229
  <tr class="nutrition-info__table--row">
230
  <td class="nutrition-info__table--cell">Cholesterol</td>
231
  <td class="nutrition-info__table--cell">{cholesterol}</td>
232
+ <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
233
  </tr>
234
  <tr class="nutrition-info__table--row">
235
  <td class="nutrition-info__table--cell">Sodium</td>
236
  <td class="nutrition-info__table--cell">{sodium}</td>
237
+ <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
238
  </tr>
239
  <tr class="nutrition-info__table--row">
240
  <td class="nutrition-info__table--cell">Total Carbohydrate</td>
241
  <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
242
+ <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
243
  </tr>
244
  <tr class="nutrition-info__table--row">
245
  <td class="nutrition-info__table--cell">Dietary Fiber</td>
246
  <td class="nutrition-info__table--cell">{dietary_fiber}</td>
247
+ <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
248
  </tr>
249
  <tr class="nutrition-info__table--row">
250
  <td class="nutrition-info__table--cell">Total Sugars</td>
251
  <td class="nutrition-info__table--cell">{total_sugars}</td>
252
+ <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
253
  </tr>
254
  <tr class="nutrition-info__table--row">
255
  <td class="nutrition-info__table--cell">Protein</td>
256
  <td class="nutrition-info__table--cell">{protein}</td>
257
+ <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
258
  </tr>
259
  <tr class="nutrition-info__table--row">
260
  <td class="nutrition-info__table--cell">Vitamin C</td>
261
  <td class="nutrition-info__table--cell">{vitc}</td>
262
+ <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
263
  </tr>
264
  <tr class="nutrition-info__table--row">
265
  <td class="nutrition-info__table--cell">Calcium</td>
266
  <td class="nutrition-info__table--cell">{calc}</td>
267
+ <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
268
  </tr>
269
  <tr class="nutrition-info__table--row">
270
  <td class="nutrition-info__table--cell">Iron</td>
271
  <td class="nutrition-info__table--cell">{iron}</td>
272
+ <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
273
  </tr>
274
  <tr class="nutrition-info__table--row">
275
  <td class="nutrition-info__table--cell">Potassium</td>
276
  <td class="nutrition-info__table--cell">{pota}</td>
277
+ <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
278
  </tr>
279
  </tbody>
280
  </table>
 
295
  calc=random_recipe['Calcium'],
296
  iron=random_recipe['Iron'],
297
  pota=random_recipe['Potassium'],
298
+ protein=random_recipe['Protein'],
299
+ fat_percent=dv_percent[0],
300
+ sat_fat_percent=dv_percent[1],
301
+ chol_percent=dv_percent[2],
302
+ sodium_percent=dv_percent[3],
303
+ carb_percent=dv_percent[4],
304
+ diet_fibe_percent=dv_percent[5],
305
+ tot_sugars_percent=dv_percent[6],
306
+ protein_percent=dv_percent[7],
307
+ vitc_percent=dv_percent[8],
308
+ calc_percent=dv_percent[9],
309
+ iron_percent=dv_percent[10],
310
+ pota_percent=dv_percent[11]
311
+
312
  )
313
 
314
  # Define a function to apply the CSS styles to the table cells
 
319
  # Add the nutrition table to the Streamlit app
320
  st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
321
  st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
322
+ st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  # create pie chart
324
  labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
325
  fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
 
341
  st.write("**Description:** ", response_json[0]['Description'])
342
  st.write("**Ingredients:** ", response_json[0]['Ingredients'])
343
  st.write("**Recipe Facts:** ", response_json[0]['Recipe Facts'])
344
+ st.write("**Directions:** ", response_json[0]['Directions'])
345
+ # extract only numeric values and convert mg to g
346
+ values = [
347
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Fat'])),
348
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Saturated Fat'])),
349
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Cholesterol'])) / 1000,
350
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Sodium'])) / 1000,
351
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Carbohydrate'])),
352
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Dietary Fiber'])),
353
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Sugars'])),
354
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Protein'])),
355
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Vitamin C'])) / 1000,
356
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Calcium'])) / 1000,
357
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Iron'])) / 1000,
358
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Potassium'])) / 1000
359
+ ]
360
+ # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
361
+ dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
362
+
363
+ # Calculate the percentage of DV for each nutrient
364
+ dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
365
  nutrition_html = """
366
  <div id="nutrition-info_6-0" class="comp nutrition-info">
367
  <table class="nutrition-info__table">
368
  <thead>
369
  <tr>
370
+ <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
371
  </tr>
372
  </thead>
373
  <tbody class="nutrition-info__table--body">
374
  <tr class="nutrition-info__table--row">
375
  <td class="nutrition-info__table--cell">Calories</td>
376
  <td class="nutrition-info__table--cell">{calories}</td>
377
+ <td class="nutrition-info__table--cell"></td>
378
  </tr>
379
  <tr class="nutrition-info__table--row">
380
  <td class="nutrition-info__table--cell">Total Fat</td>
381
  <td class="nutrition-info__table--cell">{total_fat}</td>
382
+ <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
383
  </tr>
384
  <tr class="nutrition-info__table--row">
385
  <td class="nutrition-info__table--cell">Saturated Fat</td>
386
  <td class="nutrition-info__table--cell">{saturated_fat}</td>
387
+ <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
388
  </tr>
389
  <tr class="nutrition-info__table--row">
390
  <td class="nutrition-info__table--cell">Cholesterol</td>
391
  <td class="nutrition-info__table--cell">{cholesterol}</td>
392
+ <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
393
  </tr>
394
  <tr class="nutrition-info__table--row">
395
  <td class="nutrition-info__table--cell">Sodium</td>
396
  <td class="nutrition-info__table--cell">{sodium}</td>
397
+ <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
398
  </tr>
399
  <tr class="nutrition-info__table--row">
400
  <td class="nutrition-info__table--cell">Total Carbohydrate</td>
401
  <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
402
+ <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
403
  </tr>
404
  <tr class="nutrition-info__table--row">
405
  <td class="nutrition-info__table--cell">Dietary Fiber</td>
406
  <td class="nutrition-info__table--cell">{dietary_fiber}</td>
407
+ <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
408
  </tr>
409
  <tr class="nutrition-info__table--row">
410
  <td class="nutrition-info__table--cell">Total Sugars</td>
411
  <td class="nutrition-info__table--cell">{total_sugars}</td>
412
+ <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
413
  </tr>
414
  <tr class="nutrition-info__table--row">
415
  <td class="nutrition-info__table--cell">Protein</td>
416
  <td class="nutrition-info__table--cell">{protein}</td>
417
+ <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
418
  </tr>
419
  <tr class="nutrition-info__table--row">
420
  <td class="nutrition-info__table--cell">Vitamin C</td>
421
  <td class="nutrition-info__table--cell">{vitc}</td>
422
+ <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
423
  </tr>
424
  <tr class="nutrition-info__table--row">
425
  <td class="nutrition-info__table--cell">Calcium</td>
426
  <td class="nutrition-info__table--cell">{calc}</td>
427
+ <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
428
  </tr>
429
  <tr class="nutrition-info__table--row">
430
  <td class="nutrition-info__table--cell">Iron</td>
431
  <td class="nutrition-info__table--cell">{iron}</td>
432
+ <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
433
  </tr>
434
  <tr class="nutrition-info__table--row">
435
  <td class="nutrition-info__table--cell">Potassium</td>
436
  <td class="nutrition-info__table--cell">{pota}</td>
437
+ <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
438
  </tr>
439
  </tbody>
440
  </table>
 
442
  """
443
  # Use the nutrition HTML and format it with the values
444
  formatted_html = nutrition_html.format(
445
+ calories=response_json[0]['Calories'],
446
+ total_fat=response_json[0]['Total Fat'],
447
+ saturated_fat=response_json[0]['Saturated Fat'],
448
+ cholesterol=response_json[0]['Cholesterol'],
449
+ sodium=response_json[0]['Sodium'],
450
+ total_carbohydrate=response_json[0]['Total Carbohydrate'],
451
+ dietary_fiber=response_json[0]['Dietary Fiber'],
452
+ total_sugars=response_json[0]['Total Sugars'],
453
+ servings=response_json[0]['Number of Servings'],
454
+ vitc=response_json[0]['Vitamin C'],
455
+ calc=response_json[0]['Calcium'],
456
+ iron=response_json[0]['Iron'],
457
+ pota=response_json[0]['Potassium'],
458
+ protein=response_json[0]['Protein'],
459
+ fat_percent=dv_percent[0],
460
+ sat_fat_percent=dv_percent[1],
461
+ chol_percent=dv_percent[2],
462
+ sodium_percent=dv_percent[3],
463
+ carb_percent=dv_percent[4],
464
+ diet_fibe_percent=dv_percent[5],
465
+ tot_sugars_percent=dv_percent[6],
466
+ protein_percent=dv_percent[7],
467
+ vitc_percent=dv_percent[8],
468
+ calc_percent=dv_percent[9],
469
+ iron_percent=dv_percent[10],
470
+ pota_percent=dv_percent[11]
471
+
472
  )
473
 
474
  # Define a function to apply the CSS styles to the table cells
 
479
  # Add the nutrition table to the Streamlit app
480
  st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
481
  st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
482
+ st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  # create pie chart
484
  labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
485
  fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
486
  st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
487
+ st.plotly_chart(fig)
488
+ st.write("**Tags:** ", response_json[0]['Tags'])
489
+ st.write("**Recipe URL:** ", response_json[0]['Recipe URLs'])
490
  st.markdown("### JSON Response:")
491
+ st.write(response_json)
492
 
493
  st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
494
  st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
495
 
496
  if __name__ == '__main__':
497
  main()