akhaliq HF Staff commited on
Commit
60cb489
·
1 Parent(s): b33df83

fix examples

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -149,14 +149,28 @@ def send_to_sandbox(code):
149
 
150
  def demo_card_click(e: gr.EventData):
151
  try:
152
- # Try to get the index from the event data
153
- if hasattr(e, '_data') and e._data and 'component' in e._data:
154
- index = e._data['component'].get('index', 0)
 
 
 
 
 
 
 
 
 
155
  else:
156
- # Fallback to first item if we can't get the index
157
  index = 0
 
 
 
 
 
158
  return DEMO_LIST[index]['description']
159
- except (KeyError, IndexError, AttributeError):
 
160
  # Return the first demo description as fallback
161
  return DEMO_LIST[0]['description']
162
 
@@ -185,10 +199,10 @@ with gr.Blocks(css_paths="app.css") as demo:
185
 
186
  antd.Divider("examples")
187
  with antd.Flex(gap="small", wrap=True):
188
- with ms.Each(DEMO_LIST):
189
- with antd.Card(hoverable=True, as_item="card") as demoCard:
190
- antd.CardMeta()
191
- demoCard.click(demo_card_click, outputs=[input])
192
 
193
  antd.Divider("setting")
194
 
 
149
 
150
  def demo_card_click(e: gr.EventData):
151
  try:
152
+ # Get the index from the event data
153
+ if hasattr(e, '_data') and e._data:
154
+ # Try different ways to get the index
155
+ if 'index' in e._data:
156
+ index = e._data['index']
157
+ elif 'component' in e._data and 'index' in e._data['component']:
158
+ index = e._data['component']['index']
159
+ elif 'target' in e._data and 'index' in e._data['target']:
160
+ index = e._data['target']['index']
161
+ else:
162
+ # If we can't get the index, try to extract it from the card data
163
+ index = 0
164
  else:
 
165
  index = 0
166
+
167
+ # Ensure index is within bounds
168
+ if index >= len(DEMO_LIST):
169
+ index = 0
170
+
171
  return DEMO_LIST[index]['description']
172
+ except (KeyError, IndexError, AttributeError) as e:
173
+ print(f"Error in demo_card_click: {e}")
174
  # Return the first demo description as fallback
175
  return DEMO_LIST[0]['description']
176
 
 
199
 
200
  antd.Divider("examples")
201
  with antd.Flex(gap="small", wrap=True):
202
+ for i, demo_item in enumerate(DEMO_LIST):
203
+ with antd.Card(hoverable=True, title=demo_item["title"]) as demoCard:
204
+ antd.CardMeta(description=demo_item["description"])
205
+ demoCard.click(lambda e, idx=i: DEMO_LIST[idx]['description'], outputs=[input])
206
 
207
  antd.Divider("setting")
208