Guiyom commited on
Commit
e21a0a3
·
verified ·
1 Parent(s): f491046

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -713,21 +713,28 @@ Now provide the result directly.
713
  # When creating the iframe, properly escape single quotes in the srcdoc attribute
714
  result = result.strip().strip("```").strip()
715
 
716
- # Replace single quotes with their HTML entity to avoid breaking the srcdoc attribute
717
- escaped_result = result.replace("'", "'")
 
718
 
719
- # Create the iframe with proper styling and dimensions
720
- htmloutput = f"""<div class="graph-container" style="width:100%; overflow:auto;">
721
- <iframe class="visual-frame" width="1400" height="800"
722
- style="width:100%; min-height:600px; max-height:800px; border:none;"
723
- srcdoc="{escaped_result}"></iframe>
724
- </div>"""
 
 
 
 
 
 
725
 
726
- logging.info(f"The code produced for this graph placeholder:\n{placeholder_text}\n\n")
727
  return htmloutput
728
 
729
  def replace_graph_placeholders(report_html: str, context: str, initial_query: str, crumbs: str) -> str:
730
- pattern = r"\[\[Graph Placeholder (\d+):(.*?)\]\]" # Capture placeholder number
731
 
732
  def placeholder_replacer(match):
733
  placeholder_num = match.group(1)
@@ -736,11 +743,11 @@ def replace_graph_placeholders(report_html: str, context: str, initial_query: st
736
 
737
  try:
738
  visual_html = generate_graph_snippet(instructions, context, initial_query, crumbs)
739
- # Add a container div for better styling control
740
- return f'<div class="graph-wrapper">\n<!-- Graph {placeholder_num} Start -->\n{visual_html}\n<!-- Graph {placeholder_num} End -->\n</div>'
741
  except Exception as e:
742
  logging.error(f"Graph {placeholder_num} failed: {str(e)}")
743
- return f'<div class="error-message"><!-- ERROR GENERATING GRAPH {placeholder_num} --></div>'
 
744
 
745
  return re.sub(pattern, placeholder_replacer, report_html, flags=re.DOTALL)
746
 
 
713
  # When creating the iframe, properly escape single quotes in the srcdoc attribute
714
  result = result.strip().strip("```").strip()
715
 
716
+ # Properly escape the HTML content for srcdoc attribute
717
+ # Replace single quotes with HTML entities to avoid breaking the srcdoc attribute
718
+ escaped_content = result.replace("'", "&apos;").replace('"', "&quot;")
719
 
720
+ # Create a responsive container with proper width constraints
721
+ htmloutput = f"""
722
+ <div class="d3-visualization" style="width:100%; max-width:1400px; margin:0 auto; overflow:hidden;">
723
+ <iframe
724
+ class="visual-frame"
725
+ width="100%"
726
+ height="600"
727
+ style="border:none; width:100%; min-height:600px;"
728
+ srcdoc="{escaped_content}">
729
+ </iframe>
730
+ </div>
731
+ """
732
 
733
+ logging.info(f"The code produced for this graph placeholder:\n{placeholder_text}")
734
  return htmloutput
735
 
736
  def replace_graph_placeholders(report_html: str, context: str, initial_query: str, crumbs: str) -> str:
737
+ pattern = r"\[\[Graph Placeholder (\d+):(.*?)\]\]"
738
 
739
  def placeholder_replacer(match):
740
  placeholder_num = match.group(1)
 
743
 
744
  try:
745
  visual_html = generate_graph_snippet(instructions, context, initial_query, crumbs)
746
+ return f'<!-- Graph {placeholder_num} Start -->\n<div class="graph-container" style="margin:20px 0;">\n{visual_html}\n</div>\n<!-- Graph {placeholder_num} End -->'
 
747
  except Exception as e:
748
  logging.error(f"Graph {placeholder_num} failed: {str(e)}")
749
+ error_msg = f'<div class="error-message" style="color:red; border:1px solid red; padding:10px; margin:10px 0;">Error generating Graph {placeholder_num}: {str(e)}</div>'
750
+ return error_msg
751
 
752
  return re.sub(pattern, placeholder_replacer, report_html, flags=re.DOTALL)
753