ouhenio commited on
Commit
a2875f8
·
verified ·
1 Parent(s): 766b697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -6
app.py CHANGED
@@ -432,11 +432,28 @@ class ApplicationFactory:
432
  """Create the Argilla service."""
433
  return ArgillaService()
434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  @classmethod
436
  def create_webhook_handler(cls, app_state: ApplicationState) -> Callable:
437
  """Create the webhook handler function."""
438
  country_service = CountryMappingService()
439
 
 
440
  @webhook_listener(events=["response.created"])
441
  async def handle_response_created(response, type, timestamp):
442
  try:
@@ -561,12 +578,29 @@ class ApplicationFactory:
561
  outputs=[country_progress]
562
  )
563
 
564
- # When map_data is updated, reload the D3 script
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
  map_data.change(
566
- fn=lambda data: (None, MapVisualization.create_d3_script(data)),
567
  inputs=map_data,
568
- outputs=[None, None],
569
- _js=MapVisualization.create_d3_script(app_state.to_json())
570
  )
571
 
572
  # Use timer to check for new events and update stats
@@ -575,8 +609,20 @@ class ApplicationFactory:
575
  outputs=[events_json, map_data, total_docs_ui, avg_completion_ui, countries_over_50_ui]
576
  )
577
 
578
- # Initialize D3 on page load
579
- demo.load(None, None, None, _js=MapVisualization.create_d3_script(app_state.to_json()))
 
 
 
 
 
 
 
 
 
 
 
 
580
 
581
  return demo
582
 
@@ -590,6 +636,9 @@ def create_application():
590
  app_state = ApplicationFactory.create_app_state()
591
  argilla_service = ApplicationFactory.create_argilla_service()
592
 
 
 
 
593
  # Create and register webhook handler
594
  webhook_handler = ApplicationFactory.create_webhook_handler(app_state)
595
 
 
432
  """Create the Argilla service."""
433
  return ArgillaService()
434
 
435
+ @staticmethod
436
+ def cleanup_existing_webhooks(argilla_client):
437
+ """Clean up existing webhooks to avoid warnings."""
438
+ try:
439
+ # Get existing webhooks
440
+ existing_webhooks = argilla_client.webhooks.list()
441
+
442
+ # Look for our webhook
443
+ for webhook in existing_webhooks:
444
+ if "handle_response_created" in getattr(webhook, 'url', ''):
445
+ logger.info(f"Removing existing webhook: {webhook.id}")
446
+ argilla_client.webhooks.delete(webhook.id)
447
+ break
448
+ except Exception as e:
449
+ logger.warning(f"Could not clean up webhooks: {e}")
450
+
451
  @classmethod
452
  def create_webhook_handler(cls, app_state: ApplicationState) -> Callable:
453
  """Create the webhook handler function."""
454
  country_service = CountryMappingService()
455
 
456
+ # Define the webhook handler
457
  @webhook_listener(events=["response.created"])
458
  async def handle_response_created(response, type, timestamp):
459
  try:
 
578
  outputs=[country_progress]
579
  )
580
 
581
+ # Alternative approach to load JavaScript without using _js parameter
582
+ # Create a hidden HTML component to hold our script
583
+ js_holder = gr.HTML("", visible=False)
584
+
585
+ # When map_data is updated, create a script tag with our D3 code
586
+ def create_script_tag(data):
587
+ script_content = MapVisualization.create_d3_script(data)
588
+ html = f"""
589
+ <div id="js-executor">
590
+ <script>
591
+ (async () => {{
592
+ const scriptFn = {script_content};
593
+ await scriptFn();
594
+ }})();
595
+ </script>
596
+ </div>
597
+ """
598
+ return html
599
+
600
  map_data.change(
601
+ fn=create_script_tag,
602
  inputs=map_data,
603
+ outputs=js_holder
 
604
  )
605
 
606
  # Use timer to check for new events and update stats
 
609
  outputs=[events_json, map_data, total_docs_ui, avg_completion_ui, countries_over_50_ui]
610
  )
611
 
612
+ # Initialize D3 on page load using an initial script tag
613
+ initial_map_script = gr.HTML(
614
+ f"""
615
+ <div id="initial-js-executor">
616
+ <script>
617
+ document.addEventListener('DOMContentLoaded', async () => {{
618
+ const scriptFn = {MapVisualization.create_d3_script(app_state.to_json())};
619
+ await scriptFn();
620
+ }});
621
+ </script>
622
+ </div>
623
+ """,
624
+ visible=False
625
+ )
626
 
627
  return demo
628
 
 
636
  app_state = ApplicationFactory.create_app_state()
637
  argilla_service = ApplicationFactory.create_argilla_service()
638
 
639
+ # Clean up existing webhooks
640
+ ApplicationFactory.cleanup_existing_webhooks(argilla_service.client)
641
+
642
  # Create and register webhook handler
643
  webhook_handler = ApplicationFactory.create_webhook_handler(app_state)
644