Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -133,60 +133,93 @@ menu = {
|
|
| 133 |
],
|
| 134 |
}
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
<style>
|
| 139 |
footer, .svelte-1ipelgc { display: none !important; }
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
white-space: nowrap;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
-
|
|
|
|
| 152 |
display: none;
|
| 153 |
}
|
| 154 |
</style>
|
| 155 |
"""
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
scale=4,
|
| 175 |
-
),
|
| 176 |
-
]
|
| 177 |
-
)
|
| 178 |
|
|
|
|
| 179 |
with gr.Blocks() as demo:
|
| 180 |
gr.Markdown("# 🍜🥢 EAT EAT Cafe & Bistro")
|
| 181 |
-
gr.HTML(
|
| 182 |
-
gr.Markdown(
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
for section, items in menu.items():
|
| 188 |
-
with gr.TabItem(section):
|
| 189 |
-
for item in items:
|
| 190 |
-
render_item(item)
|
| 191 |
|
| 192 |
-
|
|
|
|
|
|
| 133 |
],
|
| 134 |
}
|
| 135 |
|
| 136 |
+
# 样式:隐藏footer + 优化导航栏
|
| 137 |
+
css = """
|
| 138 |
<style>
|
| 139 |
footer, .svelte-1ipelgc { display: none !important; }
|
| 140 |
+
|
| 141 |
+
.custom-nav {
|
| 142 |
+
display: flex;
|
| 143 |
+
overflow-x: auto;
|
| 144 |
white-space: nowrap;
|
| 145 |
+
-webkit-overflow-scrolling: touch;
|
| 146 |
+
margin-bottom: 10px;
|
| 147 |
+
padding: 5px 0;
|
| 148 |
+
gap: 6px;
|
| 149 |
}
|
| 150 |
+
|
| 151 |
+
.custom-nav button {
|
| 152 |
+
flex: 1 0 auto;
|
| 153 |
+
font-size: 14px;
|
| 154 |
+
padding: 6px 12px;
|
| 155 |
+
border-radius: 12px;
|
| 156 |
+
border: 1px solid #ccc;
|
| 157 |
+
background: #f5f5f5;
|
| 158 |
+
cursor: pointer;
|
| 159 |
+
white-space: nowrap;
|
| 160 |
}
|
| 161 |
+
|
| 162 |
+
.custom-nav::-webkit-scrollbar {
|
| 163 |
display: none;
|
| 164 |
}
|
| 165 |
</style>
|
| 166 |
"""
|
| 167 |
|
| 168 |
+
# 渲染菜品
|
| 169 |
+
def render_section(category):
|
| 170 |
+
html = ""
|
| 171 |
+
for item in menu.get(category, []):
|
| 172 |
+
html += f"""
|
| 173 |
+
<div style='margin-bottom: 20px;'>
|
| 174 |
+
<div style='display: flex; gap: 12px;'>
|
| 175 |
+
<div><img src="{item['image']}" style="width:100px;height:80px;border-radius:8px;"></div>
|
| 176 |
+
<div>
|
| 177 |
+
<strong>{item['name']}</strong><br>
|
| 178 |
+
{item['description']}<br>
|
| 179 |
+
<span style="color:green;font-weight:bold;">{item['price']}</span>
|
| 180 |
+
</div>
|
| 181 |
+
</div>
|
| 182 |
+
</div>
|
| 183 |
+
"""
|
| 184 |
+
return html
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
# 主界面
|
| 187 |
with gr.Blocks() as demo:
|
| 188 |
gr.Markdown("# 🍜🥢 EAT EAT Cafe & Bistro")
|
| 189 |
+
gr.HTML(css)
|
| 190 |
+
gr.Markdown("Your task: One starter with mushroom, one beef main course, one cheapest drink.")
|
| 191 |
+
|
| 192 |
+
current = gr.State("Starters")
|
| 193 |
+
output = gr.HTML(render_section("Starters"))
|
| 194 |
+
|
| 195 |
+
with gr.Row(elem_id="nav-bar"):
|
| 196 |
+
gr.HTML('<div class="custom-nav">' +
|
| 197 |
+
''.join([
|
| 198 |
+
f'<button onclick="selectCategory(\'{cat}\')">{cat}</button>'
|
| 199 |
+
for cat in menu.keys()
|
| 200 |
+
]) +
|
| 201 |
+
'</div>')
|
| 202 |
+
|
| 203 |
+
# 用 JS 与 Python 通信
|
| 204 |
+
gr.HTML("""
|
| 205 |
+
<script>
|
| 206 |
+
function selectCategory(cat) {
|
| 207 |
+
document.querySelector('textarea[data-testid="component-markdown"]').value = cat;
|
| 208 |
+
document.querySelector('button[data-testid="component-button"]').click();
|
| 209 |
+
}
|
| 210 |
+
</script>
|
| 211 |
+
""")
|
| 212 |
+
|
| 213 |
+
hidden_input = gr.Textbox(visible=False)
|
| 214 |
+
trigger = gr.Button("Load Section", visible=False)
|
| 215 |
+
|
| 216 |
+
trigger.click(
|
| 217 |
+
lambda c: (c, render_section(c)),
|
| 218 |
+
inputs=[hidden_input],
|
| 219 |
+
outputs=[current, output]
|
| 220 |
)
|
| 221 |
|
| 222 |
+
output.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
if __name__ == "__main__":
|
| 225 |
+
demo.launch()
|