Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -133,93 +133,49 @@ menu = {
|
|
| 133 |
],
|
| 134 |
}
|
| 135 |
|
| 136 |
-
# 样式:隐藏footer + 优化导航栏
|
| 137 |
css = """
|
| 138 |
<style>
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 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 |
-
|
| 217 |
-
|
| 218 |
-
inputs=[hidden_input],
|
| 219 |
-
outputs=[current, output]
|
| 220 |
)
|
| 221 |
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
if __name__ == "__main__":
|
| 225 |
demo.launch()
|
|
|
|
| 133 |
],
|
| 134 |
}
|
| 135 |
|
|
|
|
| 136 |
css = """
|
| 137 |
<style>
|
| 138 |
+
footer, .svelte-1ipelgc { display: none !important; }
|
| 139 |
|
| 140 |
+
/* 调整tab样式防止折叠 */
|
| 141 |
+
.tabs.svelte-1ipelgc {
|
| 142 |
+
flex-wrap: nowrap !important;
|
| 143 |
+
overflow-x: auto !important;
|
| 144 |
+
gap: 4px !important;
|
| 145 |
+
}
|
| 146 |
+
.tabitem.svelte-1ipelgc {
|
| 147 |
+
font-size: 14px !important;
|
| 148 |
+
padding: 6px 10px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
white-space: nowrap;
|
| 150 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
</style>
|
| 152 |
"""
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
with gr.Blocks() as demo:
|
| 155 |
gr.Markdown("# 🍜🥢 EAT EAT Cafe & Bistro")
|
| 156 |
gr.HTML(css)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
gr.Markdown(
|
| 159 |
+
"Your task: One starter containing mushroom, One main course containing beef; One drink that is the cheapest on the menu."
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
+
with gr.Tabs():
|
| 163 |
+
for category, items in menu.items():
|
| 164 |
+
with gr.TabItem(category):
|
| 165 |
+
for item in items:
|
| 166 |
+
with gr.Row():
|
| 167 |
+
with gr.Column(scale=1):
|
| 168 |
+
gr.HTML(
|
| 169 |
+
f"<img src='{item.get('image', '')}' style='height:90px;width:120px;border-radius:8px;'/>"
|
| 170 |
+
)
|
| 171 |
+
with gr.Column(scale=4):
|
| 172 |
+
name = item.get("name", "")
|
| 173 |
+
desc = item.get("description", "")
|
| 174 |
+
price = item.get("price", "")
|
| 175 |
+
gr.Markdown(
|
| 176 |
+
f"**{name}**\n\n{desc}\n\n<span style='color:green;font-weight:bold'>{price}</span>",
|
| 177 |
+
show_label=False,
|
| 178 |
+
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
demo.launch()
|