Py.Cafe

jackparmer/

plotly-3d-hamburger-builder

3D Hamburger Builder with Plotly

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
import streamlit as st
import plotly.graph_objs as go
import numpy as np

# Function to generate circle coordinates
def generate_circle(radius, z, num_points=100):
    theta = np.linspace(0, 2 * np.pi, num_points)
    x = radius * np.cos(theta)
    y = radius * np.sin(theta)
    z = np.full_like(theta, z)
    return x, y, z

# Function to generate lettuce coordinates (irregular shape)
def generate_lettuce(radius, z, num_points=100):
    theta = np.linspace(0, 2 * np.pi, num_points)
    r = radius + 0.2 * np.sin(6 * theta)  # Adding some wave-like irregularities
    x = r * np.cos(theta)
    y = r * np.sin(theta)
    z = np.full_like(theta, z)
    return x, y, z

# Function to generate data for a realistic 3D hamburger with multiple layers
def generate_hamburger(num_patties):
    layers = []
    
    # Bottom bun
    x, y, z = generate_circle(5, 0)
    layers.append((x, y, z, 'goldenrod', 'Bottom Bun'))

    current_height = 0.3

    for _ in range(num_patties):
        # Patty
        x, y, z = generate_circle(5, current_height)
        layers.append((x, y, z, 'saddlebrown', 'Patty'))
        current_height += 0.2

        # Lettuce
        x, y, z = generate_lettuce(5, current_height)
        layers.append((x, y, z, 'green', 'Lettuce'))
        current_height += 0.1

        # Tomato
        x, y, z = generate_circle(5, current_height)
        layers.append((x, y, z, 'tomato', 'Tomato'))
        current_height += 0.1

    # Top bun
    x, y, z = generate_circle(5, current_height)
    layers.append((x, y, z, 'goldenrod', 'Top Bun'))

    return layers

# Generate the plotly figure
def create_figure(num_patties):
    fig = go.Figure()

    layers = generate_hamburger(num_patties)
    for x, y, z, color, name in layers:
        fig.add_trace(go.Scatter3d(x=x, y=y, z=z, mode='lines', line=dict(color=color, width=10), name=name))

    fig.update_layout(
        title='Realistic 3D Hamburger',
        scene=dict(
            xaxis=dict(title='X-axis'),
            yaxis=dict(title='Y-axis'),
            zaxis=dict(title='Z-axis')
        ),
        scene_aspectmode='data'
    )
    return fig

# Streamlit app layout
st.title("3D Hamburger Builder")

num_patties = st.slider("Select number of patties:", min_value=1, max_value=5, value=2)

st.plotly_chart(create_figure(num_patties))