Jimmyzheng-10 commited on
Commit
95ce3be
·
1 Parent(s): 80ce879

Fix HTML rendering issues by cleaning up problematic script and CSS references

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -67,7 +67,25 @@ def render_preview(code: str, width: int, height: int, scale: float) -> str:
67
  """
68
  Preview renderer with both width and height control for the inner canvas.
69
  """
70
- safe_code = html.escape(code).replace("'", "'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  iframe_html = f"""
72
  <div style="width: 100%; max-width: 1920px; margin: 0 auto; overflow-x: auto; overflow-y: hidden;">
73
  <div style="
@@ -132,6 +150,19 @@ def process_and_generate(image_np, image_path_from_state, sidebar_prompt, header
132
 
133
  print(f"Processing HTML for run_id: {run_id}")
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  for img in soup.find_all('img'):
136
  if img.get('src') and not img['src'].startswith(('http', 'data:')):
137
  original_src = img['src']
 
67
  """
68
  Preview renderer with both width and height control for the inner canvas.
69
  """
70
+ # Clean up the HTML code to remove problematic script and CSS references
71
+ soup = BeautifulSoup(code, 'html.parser')
72
+
73
+ # Remove any script tags that reference local assets
74
+ for script in soup.find_all('script'):
75
+ src = script.get('src', '')
76
+ if src and ('assets/' in src or 'index-' in src):
77
+ script.decompose()
78
+
79
+ # Remove any link tags that reference local CSS assets
80
+ for link in soup.find_all('link'):
81
+ href = link.get('href', '')
82
+ if href and ('assets/' in href or 'index-' in href):
83
+ link.decompose()
84
+
85
+ # Get the cleaned HTML
86
+ cleaned_code = str(soup)
87
+ safe_code = html.escape(cleaned_code).replace("'", "&apos;")
88
+
89
  iframe_html = f"""
90
  <div style="width: 100%; max-width: 1920px; margin: 0 auto; overflow-x: auto; overflow-y: hidden;">
91
  <div style="
 
150
 
151
  print(f"Processing HTML for run_id: {run_id}")
152
 
153
+ # Clean up problematic script and CSS references
154
+ for script in soup.find_all('script'):
155
+ src = script.get('src', '')
156
+ if src and ('assets/' in src or 'index-' in src):
157
+ print(f"Removing problematic script: {src}")
158
+ script.decompose()
159
+
160
+ for link in soup.find_all('link'):
161
+ href = link.get('href', '')
162
+ if href and ('assets/' in href or 'index-' in href):
163
+ print(f"Removing problematic CSS link: {href}")
164
+ link.decompose()
165
+
166
  for img in soup.find_all('img'):
167
  if img.get('src') and not img['src'].startswith(('http', 'data:')):
168
  original_src = img['src']