Wanswan commited on
Commit
ea998af
·
verified ·
1 Parent(s): 8a7aeb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -41
app.py CHANGED
@@ -133,60 +133,93 @@ menu = {
133
  ],
134
  }
135
 
136
-
137
- css_fix_tabs = """
138
  <style>
139
  footer, .svelte-1ipelgc { display: none !important; }
140
- .gr-tab-label {
141
- font-size: 13px !important;
142
- padding: 6px 10px !important;
 
143
  white-space: nowrap;
 
 
 
 
144
  }
145
- .gr-tabs > div:first-child {
146
- flex-wrap: nowrap !important;
147
- overflow-x: auto;
148
- scrollbar-width: none;
149
- -ms-overflow-style: none;
 
 
 
 
 
150
  }
151
- .gr-tabs > div:first-child::-webkit-scrollbar {
 
152
  display: none;
153
  }
154
  </style>
155
  """
156
 
157
- def render_item(item):
158
- return gr.Row(
159
- [
160
- gr.Column(
161
- [gr.HTML(
162
- f"<img src='{item['image']}' alt='{item['name']}' style='height:90px;width:120px;border-radius:8px;' />"
163
- )],
164
- scale=1,
165
- ),
166
- gr.Column(
167
- [
168
- gr.Markdown(
169
- f"**{item['name']}**\n\n{item['description']}\n\n"
170
- f"<span style='font-size:1.1em;font-weight:bold;color:green;'>{item['price']}</span>",
171
- show_label=False,
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(css_fix_tabs)
182
- gr.Markdown(
183
- "Your task: One starter containing mushroom,One main course containing beef;One drink that is the cheapest on the menu."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  )
185
 
186
- with gr.Tabs():
187
- for section, items in menu.items():
188
- with gr.TabItem(section):
189
- for item in items:
190
- render_item(item)
191
 
192
- demo.launch()
 
 
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()