Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import streamlit as st
- import requests
- import matplotlib.pyplot as plt
- import numpy as np
- st.set_option('deprecation.showPyplotGlobalUse', False)
- EDAMAM_APP_ID = 'b43bfd1e'
- EDAMAM_APP_KEY = '52c88b2f791a5ddecbecf76a970d8a13'
- def get_recipe(food, calories):
- url = f"https://api.edamam.com/search?q={food}&app_id={EDAMAM_APP_ID}&app_key={EDAMAM_APP_KEY}&calories=0-{calories}"
- response = requests.get(url)
- data = response.json()
- return data['hits'][0]['recipe'] if data['hits'] else None
- def plot_nutrition(recipe):
- nutrients = recipe['totalNutrients']
- nutrients_names = [nutrient['label'] for nutrient in nutrients.values()]
- nutrients_values = [nutrient['quantity'] for nutrient in nutrients.values()]
- colors = ['#FFA07A', '#20B2AA']
- fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
- plt.style.use('ggplot')
- y_pos = np.arange(len(nutrients_names))
- bars = ax1.barh(y_pos, nutrients_values, align='center', color=colors[0])
- ax1.set_yticks(y_pos)
- ax1.set_yticklabels(nutrients_names, fontweight='bold', fontsize=12, family='Arial', color='white')
- ax1.set_xlabel('Quantity')
- ax1.set_title('Nutrition Values', fontweight='bold', fontsize=16, color='white')
- ax1.invert_yaxis()
- ax1.spines['top'].set_visible(False)
- ax1.spines['right'].set_visible(False)
- ax1.spines['bottom'].set_visible(False)
- ax1.xaxis.set_tick_params(pad=20)
- for bar in bars:
- width = bar.get_width()
- ax1.text(width, bar.get_y() + bar.get_height() / 2, f'{width:.1f}', va='center', color='white',
- fontweight='bold', fontsize=12, family='Arial')
- ax1.tick_params(axis='y', which='major', pad=10)
- balance_score = np.mean(nutrients_values) / np.sum(nutrients_values)
- labels = ['Balance', 'Total']
- sizes = [balance_score, 1 - balance_score]
- explode = (0.1, 0)
- ax2.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
- shadow=True, startangle=90)
- ax2.axis('equal')
- ax2.set_title('Meal Balance', fontweight='bold', fontsize=16, color='white')
- plt.tight_layout()
- st.pyplot(fig)
- def main():
- st.title("Welcome to DailyCalories!")
- st.markdown(
- """
- <style>
- body {
- background-color: #f0f0f0;
- color: #333333;
- font-family: Arial, sans-serif;
- line-height: 1.6;
- }
- .container {
- max-width: 800px;
- margin: 0 auto;
- padding: 20px;
- }
- .header {
- background-color: #007bff;
- color: white;
- padding: 20px;
- border-radius: 5px;
- }
- .subheader {
- font-size: 20px;
- font-weight: bold;
- margin-bottom: 20px;
- }
- .input {
- margin-bottom: 20px;
- }
- .button {
- background-color: #007bff;
- color: white;
- border: none;
- padding: 10px 20px;
- border-radius: 5px;
- cursor: pointer;
- font-size: 16px;
- }
- .button:hover {
- background-color: #0056b3;
- }
- .image {
- max-width: 100%;
- margin-bottom: 20px;
- border-radius: 5px;
- }
- </style>
- """,
- unsafe_allow_html=True
- )
- st.write("""Irgend en wilkommens text hald""")
- food_type = st.text_input("Enter a Food Type:", "Chicken")
- max_calories = st.number_input("Enter Maximum Calories:", min_value=0, step=100, value=500)
- if st.button("Find Recipe", key='find_recipe_button'):
- recipe = get_recipe(food_type, max_calories)
- if recipe:
- st.subheader(recipe['label'])
- st.markdown(f'<img src="{recipe["image"]}" class="image">', unsafe_allow_html=True)
- st.subheader("Ingredients:")
- for ingredient in recipe['ingredientLines']:
- st.write(ingredient)
- plot_nutrition(recipe)
- else:
- st.error("No recipe found. Please try different criteria.")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement