ydshieh HF Staff commited on
Commit
571875d
·
verified ·
1 Parent(s): dd14891

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -35
app.py CHANGED
@@ -208,9 +208,6 @@ with gr.Blocks() as demo:
208
  with gr.Row():
209
  search_btn = gr.Button("Search")
210
  refresh_btn = gr.Button("Clear cache")
211
-
212
- # Hidden textbox to receive URL params from JavaScript
213
- url_params_hidden = gr.Textbox(visible=False, elem_id="url_params_hidden")
214
 
215
  table = gr.Dataframe(
216
  headers=[
@@ -227,46 +224,72 @@ with gr.Blocks() as demo:
227
  json_view = gr.Code(label="Latest entry details", language="json")
228
  status = gr.Markdown("")
229
 
230
- def parse_url_params_from_js(params_json):
231
- """Parse URL parameters passed from JavaScript"""
232
- import json
233
- try:
234
- if params_json:
235
- params = json.loads(params_json)
236
- print(f"DEBUG: Parsed URL params from JS: {params}")
237
- return params.get('repo', ''), params.get('pr', ''), params.get('sha', '')
238
- except Exception as e:
239
- print(f"DEBUG: Error parsing URL params: {e}")
240
- return '', '', ''
241
-
242
  search_btn.click(query, inputs=[repo_box, pr_box, sha_box], outputs=[table, json_view, status])
243
  refresh_btn.click(refresh_dataset, outputs=status)
244
 
245
- # When page loads, populate fields from URL parameters
246
- demo.load(parse_url_params_from_js, inputs=[url_params_hidden], outputs=[repo_box, pr_box, sha_box])
247
-
248
- # JavaScript to extract URL params and put them in the hidden field
249
  gr.HTML("""
250
  <script>
251
- setTimeout(function() {
 
252
  const params = new URLSearchParams(window.location.search);
253
- const paramsObj = {
254
- repo: params.get('repo') || '',
255
- pr: params.get('pr') || '',
256
- sha: params.get('sha') || ''
257
- };
258
- console.log('URL params detected:', paramsObj);
 
 
 
 
 
 
 
 
259
 
260
- // Find the hidden textbox and set its value
261
- const hiddenInput = document.querySelector('#url_params_hidden textarea');
262
- if (hiddenInput) {
263
- hiddenInput.value = JSON.stringify(paramsObj);
264
- hiddenInput.dispatchEvent(new Event('input', { bubbles: true }));
265
- console.log('Set hidden input value');
266
- } else {
267
- console.log('Hidden input not found');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  }
269
- }, 100);
 
 
 
 
 
270
  </script>
271
  """)
272
 
 
208
  with gr.Row():
209
  search_btn = gr.Button("Search")
210
  refresh_btn = gr.Button("Clear cache")
 
 
 
211
 
212
  table = gr.Dataframe(
213
  headers=[
 
224
  json_view = gr.Code(label="Latest entry details", language="json")
225
  status = gr.Markdown("")
226
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  search_btn.click(query, inputs=[repo_box, pr_box, sha_box], outputs=[table, json_view, status])
228
  refresh_btn.click(refresh_dataset, outputs=status)
229
 
230
+ # Add JavaScript that runs after Gradio initializes and directly manipulates the components
 
 
 
231
  gr.HTML("""
232
  <script>
233
+ // Wait for Gradio to fully load
234
+ function populateFromURL() {
235
  const params = new URLSearchParams(window.location.search);
236
+ const repo = params.get('repo') || '';
237
+ const pr = params.get('pr') || '';
238
+ const sha = params.get('sha') || '';
239
+
240
+ console.log('Attempting to populate with:', {repo, pr, sha});
241
+
242
+ if (!repo && !pr && !sha) {
243
+ console.log('No URL parameters found');
244
+ return;
245
+ }
246
+
247
+ // Try multiple selectors to find the inputs
248
+ const allInputs = document.querySelectorAll('input[type="text"], textarea');
249
+ console.log('Found ' + allInputs.length + ' input elements');
250
 
251
+ // Look for inputs by their labels
252
+ const labels = document.querySelectorAll('label');
253
+ labels.forEach(label => {
254
+ const text = label.textContent;
255
+ const input = label.parentElement?.querySelector('input, textarea');
256
+
257
+ if (text.includes('Repository') && repo && input) {
258
+ console.log('Setting repository to:', repo);
259
+ input.value = repo;
260
+ input.dispatchEvent(new Event('input', {bubbles: true}));
261
+ input.dispatchEvent(new Event('change', {bubbles: true}));
262
+ } else if (text.includes('PR number') && pr && input) {
263
+ console.log('Setting PR to:', pr);
264
+ input.value = pr;
265
+ input.dispatchEvent(new Event('input', {bubbles: true}));
266
+ input.dispatchEvent(new Event('change', {bubbles: true}));
267
+ } else if (text.includes('Commit SHA') && sha && input) {
268
+ console.log('Setting SHA to:', sha);
269
+ input.value = sha;
270
+ input.dispatchEvent(new Event('input', {bubbles: true}));
271
+ input.dispatchEvent(new Event('change', {bubbles: true}));
272
+ }
273
+ });
274
+
275
+ // Auto-trigger search if we have a PR
276
+ if (pr) {
277
+ setTimeout(() => {
278
+ const buttons = document.querySelectorAll('button');
279
+ buttons.forEach(btn => {
280
+ if (btn.textContent.includes('Search')) {
281
+ console.log('Auto-clicking Search button');
282
+ btn.click();
283
+ }
284
+ });
285
+ }, 1000);
286
  }
287
+ }
288
+
289
+ // Try multiple times as Gradio might take time to initialize
290
+ setTimeout(populateFromURL, 500);
291
+ setTimeout(populateFromURL, 1500);
292
+ setTimeout(populateFromURL, 3000);
293
  </script>
294
  """)
295