Py.Cafe

jackparmer/

hamburger-helper

DocsPricing
  • app.py
  • requirements.txt
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)