-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
90 lines (71 loc) · 2.87 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import streamlit as st
import pandas
import requests
# import snowflake.connector
# bring in the data - alt source
# my_fruit_list = pandas.read_csv("https://uni-lab-files.s3.us-west-2.amazonaws.com/dabw/fruit_macros.txt")
# my_fruit_list = my_fruit_list.set_index('Fruit')
def get_all_fruit():
fruit_table = pandas.read_csv("./data/fruitweights.csv")
return fruit_table
def smoothie_nutri_total(df, col_name, multiples):
total = 0
for fruit in multiples:
stats = df.loc[[fruit[0]]]
weight_multiple = float(stats['Weight']) / 100
total += (float(stats[col_name]) * weight_multiple) * fruit[1]
total = round(total, 2)
return str(total)
def selected_fruit_slider(fruit_name):
prompt = "Number of " + fruit_name + " in the smoothie: "
num = st.slider(prompt, 0, 30, 2)
return [fruit_name, num]
def App():
st.header('Build your own smoothie below!')
st.text('Figure out how healthy your smoothies with this easy tool.')
# API request for fruits
fruits_df = get_all_fruit()
fruits_df = fruits_df[~fruits_df['Calories'].isnull()]
# clean the table
all_fruits = fruits_df.set_index('Fruit')
# list the selected fruits
fruits_selected = st.multiselect("Pick Fruits:", list(all_fruits.index))
# get fruit multiples
fruit_counts = []
for fruit in fruits_selected:
fruit_counts.append(selected_fruit_slider(str(fruit)))
# get the smoothie stats
st.header("Your Smoothie's Stats")
emojis = ""
# display the icons
for fruit in fruit_counts:
icon = (all_fruits.loc[fruit[0]]).Icon
if (icon != "🍴"):
emojis += (icon + " ")
st.header(emojis)
total_cals = smoothie_nutri_total(all_fruits, 'Calories', fruit_counts)
total_sugar = smoothie_nutri_total(all_fruits, 'Sugar', fruit_counts)
total_carbs = smoothie_nutri_total(all_fruits, 'Carbohydrates', fruit_counts)
total_protine = smoothie_nutri_total(all_fruits, 'Protein', fruit_counts)
total_fat = smoothie_nutri_total(all_fruits, 'Fat', fruit_counts)
# display the stats
cals, sugar = st.columns(2)
carbs, protine = st.columns(2)
fat, other = st.columns(2)
# row 1
cals.metric(label="Smoothie Calories", value=total_cals, delta="1.2 °F")
sugar.metric(label="Smoothie Sugar", value=total_sugar, delta="1.2 °F")
# row 2
carbs.metric(label="Smoothie Carbohydrates", value=total_carbs, delta="1.2 °F")
protine.metric(label="Smoothie Protein", value=total_protine, delta="1.2 °F")
# row 3
fat.metric(label="Smoothie Fat", value=total_fat, delta="1.2 °F")
# the all fruits reference table
st.header('All Fruits Reference')
st.text('All nutrient values are per 100g of the fruit.')
st.dataframe(all_fruits)
st.text("Built by Thomas Lillo")
st.text("Source Code:")
st.text("https://github.com/thomaslillo/SmoothieNutritionCalculator")
# run the app
App()