Spaces:
Running
Running
Commit
·
c6373cc
1
Parent(s):
c897576
Restore BeautifulSoup HTML cleaning with error handling to fix CSS loading issues
Browse files
app.py
CHANGED
|
@@ -67,7 +67,30 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 +155,22 @@ 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 |
+
try:
|
| 71 |
+
# Clean up the HTML code to remove problematic script and CSS references
|
| 72 |
+
soup = BeautifulSoup(code, 'html.parser')
|
| 73 |
+
|
| 74 |
+
# Remove any script tags that reference problematic files
|
| 75 |
+
for script in soup.find_all('script'):
|
| 76 |
+
src = script.get('src', '')
|
| 77 |
+
if src and any(pattern in src for pattern in ['assets/', 'index-', 'iframeResizer']):
|
| 78 |
+
script.decompose()
|
| 79 |
+
|
| 80 |
+
# Remove any link tags that reference problematic CSS files
|
| 81 |
+
for link in soup.find_all('link'):
|
| 82 |
+
href = link.get('href', '')
|
| 83 |
+
if href and any(pattern in href for pattern in ['assets/', 'index-']):
|
| 84 |
+
link.decompose()
|
| 85 |
+
|
| 86 |
+
# Get the cleaned HTML
|
| 87 |
+
cleaned_code = str(soup)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"Error cleaning HTML in render_preview: {e}")
|
| 90 |
+
# Fallback to original code if cleaning fails
|
| 91 |
+
cleaned_code = code
|
| 92 |
+
|
| 93 |
+
safe_code = html.escape(cleaned_code).replace("'", "'")
|
| 94 |
iframe_html = f"""
|
| 95 |
<div style="width: 100%; max-width: 1920px; margin: 0 auto; overflow-x: auto; overflow-y: hidden;">
|
| 96 |
<div style="
|
|
|
|
| 155 |
|
| 156 |
print(f"Processing HTML for run_id: {run_id}")
|
| 157 |
|
| 158 |
+
# Clean up problematic script and CSS references with error handling
|
| 159 |
+
try:
|
| 160 |
+
for script in soup.find_all('script'):
|
| 161 |
+
src = script.get('src', '')
|
| 162 |
+
if src and any(pattern in src for pattern in ['assets/', 'index-', 'iframeResizer']):
|
| 163 |
+
print(f"Removing problematic script: {src}")
|
| 164 |
+
script.decompose()
|
| 165 |
+
|
| 166 |
+
for link in soup.find_all('link'):
|
| 167 |
+
href = link.get('href', '')
|
| 168 |
+
if href and any(pattern in href for pattern in ['assets/', 'index-']):
|
| 169 |
+
print(f"Removing problematic CSS link: {href}")
|
| 170 |
+
link.decompose()
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"Error cleaning HTML in process_and_generate: {e}")
|
| 173 |
+
|
| 174 |
for img in soup.find_all('img'):
|
| 175 |
if img.get('src') and not img['src'].startswith(('http', 'data:')):
|
| 176 |
original_src = img['src']
|