import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import numpy as np
# Initialize the Dash app
app = dash.Dash(__name__)
# Function to create a cylinder mesh
def create_cylinder_mesh(radius, height, z_offset, color):
theta = np.linspace(0, 2 * np.pi, 100)
x = radius * np.cos(theta)
y = radius * np.sin(theta)
z_top = np.full_like(theta, height / 2) + z_offset
z_bottom = np.full_like(theta, -height / 2) + z_offset
vertices_x = np.concatenate([x, x])
vertices_y = np.concatenate([y, y])
vertices_z = np.concatenate([z_top, z_bottom])
indices = []
for i in range(len(theta) - 1):
indices.extend([i, i + 1, i + len(theta)])
indices.extend([i + 1, i + len(theta) + 1, i + len(theta)])
indices.extend([len(theta) - 1, 0, len(theta) * 2 - 1])
indices.extend([0, len(theta), len(theta) * 2 - 1])
mesh = go.Mesh3d(
x=vertices_x,
y=vertices_y,
z=vertices_z,
i=indices[::3],
j=indices[1::3],
k=indices[2::3],
color=color,
opacity=0.9
)
top_surface = go.Surface(
x=[x], y=[y], z=[z_top],
colorscale=[[0, color], [1, color]], showscale=False, opacity=0.9
)
bottom_surface = go.Surface(
x=[x], y=[y], z=[z_bottom],
colorscale=[[0, color], [1, color]], showscale=False, opacity=0.9
)
return mesh, top_surface, bottom_surface
# Function to create a hamburger model
def create_hamburger(num_patties):
components = []
# Bun top
mesh, top_surface, bottom_surface = create_cylinder_mesh(0.5, 0.2, 0.35, 'sandybrown')
components.extend([mesh, top_surface, bottom_surface])
# Patties
for i in range(num_patties):
mesh, top_surface, bottom_surface = create_cylinder_mesh(0.4, 0.1, 0.15 - 0.15 * i, 'brown')
components.extend([mesh, top_surface, bottom_surface])
# Lettuce
mesh, top_surface, bottom_surface = create_cylinder_mesh(0.45, 0.05, -0.05, 'green')
components.extend([mesh, top_surface, bottom_surface])
# Tomato
mesh, top_surface, bottom_surface = create_cylinder_mesh(0.45, 0.05, -0.15, 'red')
components.extend([mesh, top_surface, bottom_surface])
# Bun bottom
mesh, top_surface, bottom_surface = create_cylinder_mesh(0.5, 0.2, -0.35, 'sandybrown')
components.extend([mesh, top_surface, bottom_surface])
fig = go.Figure(data=components)
# Update layout for better viewing angle
fig.update_layout(scene=dict(
xaxis=dict(visible=False),
yaxis=dict(visible=False),
zaxis=dict(visible=False),
camera=dict(eye=dict(x=1.5, y=1.5, z=1))
))
return fig
# Define the layout of the Dash app
app.layout = html.Div([
html.H1("Realistic 3D Hamburger"),
dcc.Slider(
id='patty-slider',
min=1,
max=5,
step=1,
value=1,
marks={i: str(i) for i in range(1, 6)},
tooltip={"placement": "bottom", "always_visible": True}
),
html.Label("Number of Patties"),
dcc.Graph(id='hamburger-3d')
])
# Define the callback to update the hamburger model
@app.callback(
Output('hamburger-3d', 'figure'),
[Input('patty-slider', 'value')]
)
def update_hamburger(num_patties):
return create_hamburger(num_patties)
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)