-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathCalculation_of_meal.py
88 lines (77 loc) · 3.44 KB
/
Calculation_of_meal.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from IPython.display import display, HTML
from ipywidgets import interact, widgets
from plotly.subplots import make_subplots
# Ingredients for the pizza
sauces = ['Tomato', 'Pesto', 'Alfredo', 'BBQ']
breads = ['Thin Crust', 'Regular', 'Whole Wheat', 'Gluten-Free']
cheeses = ['Mozzarella', 'Cheddar', 'Parmesan']
# Ensure all ingredient lists have the same length
max_len = max(len(sauces), len(breads), len(cheeses))
sauces = sauces + [''] * (max_len - len(sauces))
breads = breads + [''] * (max_len - len(breads))
cheeses = cheeses + [''] * (max_len - len(cheeses))
# Create a DataFrame for ingredient options
ingredient_options = pd.DataFrame({
'Sauce': sauces,
'Bread': breads,
'Cheese': cheeses
})
# Create interactive dropdowns using ipywidgets
sauce_dropdown = widgets.Dropdown(options=ingredient_options['Sauce'])
bread_dropdown = widgets.Dropdown(options=ingredient_options['Bread'])
cheese_dropdown = widgets.Dropdown(options=ingredient_options['Cheese'])
# Function to update the pizza visualization
def update_pizza(sauce, bread, cheese):
global selected_sauce, selected_bread, selected_cheese
selected_sauce = sauce
selected_bread = bread
selected_cheese = cheese
fig = make_subplots(rows=1, cols=3, specs=[[{'type': 'xy'}, {'type': 'xy'}, {'type': 'xy'}]])
# Create the pizza visualization
pizza = go.Scatter(
x=[0, 0.5, 1, 0], y=[0, 1, 0, 0],
mode="lines", fill="toself",
fillcolor="lightgrey"
)
# Highlight selected ingredients
if selected_sauce:
sauce_idx = ingredient_options[ingredient_options['Sauce'] == selected_sauce].index[0]
fig.add_trace(go.Scatter(x=[0, 0.25, 0.25, 0],
y=[0, 0.5, 1, 0], mode="lines", fill="toself",
fillcolor="red", name="Sauce: " + selected_sauce))
if selected_bread:
bread_idx = ingredient_options[ingredient_options['Bread'] == selected_bread].index[0]
fig.add_trace(go.Scatter(x=[0.25, 0.5, 0.75, 0.25],
y=[0.5, 1, 0.5, 0.5], mode="lines", fill="toself",
fillcolor="brown", name="Bread: " + selected_bread))
if selected_cheese:
cheese_idx = ingredient_options[ingredient_options['Cheese'] == selected_cheese].index[0]
fig.add_trace(go.Scatter(x=[0.5, 1, 1, 0.75],
y=[0, 0.5, 1, 0], mode="lines", fill="toself",
fillcolor="yellow", name="Cheese: " + selected_cheese))
fig.add_trace(pizza)
fig.update_layout(
showlegend=False, xaxis=dict(range=[-0.2, 1.2]), yaxis=dict(range=[-0.2, 1.2]),
annotations=[
dict(
x=0.25, y=0.25, xref="x", yref="y",
text="Sauce", showarrow=True, arrowhead=7, ax=0, ay=-40
),
dict(
x=0.625, y=0.75, xref="x", yref="y",
text="Bread", showarrow=True, arrowhead=7, ax=0, ay=-40
),
dict(
x=0.875, y=0.25, xref="x", yref="y",
text="Cheese", showarrow=True, arrowhead=7, ax=0, ay=-40
)
],
width=800, height=300
)
return fig
# Create interactive UI using ipywidgets interact function
interact(update_pizza, sauce=sauce_dropdown, bread=bread_dropdown, cheese=cheese_dropdown)