Nanny7 commited on
Commit
71f8c18
·
1 Parent(s): 6394787

Embed interactive 3D orbital viewer directly in app using iframe

Browse files
Files changed (2) hide show
  1. app.py +25 -15
  2. force_rebuild.txt +1 -1
app.py CHANGED
@@ -366,23 +366,33 @@ def smiles_to_molecular_orbitals(smiles_input: str, name_input: str) -> str:
366
  </html>
367
  """
368
 
369
- # Save HTML file
370
- html_filename = f"{label.lower()}_orbital.html"
371
- html_path = Path(tmpdir) / html_filename
 
 
 
 
 
372
  html_path.write_text(html_content)
 
373
 
374
- # Create download link
375
- html_sections.append(
376
- f"<div style='margin: 20px 0; padding: 15px; background: #e8f4f8; border: 2px solid #2196F3; border-radius: 8px;'>"
377
- f"<h3 style='color: #1976D2; margin-top: 0;'>{label} Orbital - Interactive 3D Model</h3>"
378
- f"<p style='margin: 10px 0;'><strong>Download the interactive 3D visualization:</strong></p>"
379
- f"<a href='data:text/html;base64,{base64.b64encode(html_content.encode()).decode()}' "
380
- f"download='{html_filename}' "
381
- f"style='display: inline-block; padding: 12px 24px; background: #2196F3; color: white; "
382
- f"text-decoration: none; border-radius: 4px; font-weight: bold;'>📥 Download {label} 3D Model (HTML)</a>"
383
- f"<p style='margin: 10px 0; color: #666;'><small>Open the downloaded HTML file in any web browser for full interactive 3D rotation</small></p>"
384
- f"</div>"
385
- )
 
 
 
 
386
 
387
  if not html_sections:
388
  return "<p>Could not prepare HOMO/LUMO visualizations.</p>"
 
366
  </html>
367
  """
368
 
369
+ # Save HTML file to a location accessible by Gradio
370
+ html_filename = f"{label.lower()}_orbital_{hash(canonical_smiles)}.html"
371
+
372
+ # Move back to original directory to save in a persistent location
373
+ os.chdir(original_cwd)
374
+ output_dir = Path("orbital_outputs")
375
+ output_dir.mkdir(exist_ok=True)
376
+ html_path = output_dir / html_filename
377
  html_path.write_text(html_content)
378
+ os.chdir(tmpdir)
379
 
380
+ # Create iframe to embed the HTML directly
381
+ iframe_html = f"""
382
+ <div style='margin: 20px 0; padding: 15px; background: white; border: 2px solid #2196F3; border-radius: 8px;'>
383
+ <h3 style='color: #1976D2; margin-top: 0;'>{label} Orbital - Interactive 3D</h3>
384
+ <p style='margin: 10px 0; color: #666;'><small>
385
+ <strong>Blue:</strong> Positive orbital lobe | <strong>Red:</strong> Negative orbital lobe<br>
386
+ <strong>Controls:</strong> Left-click drag to rotate, Scroll to zoom, Right-click drag to pan
387
+ </small></p>
388
+ <iframe srcdoc="{html_content.replace('"', '&quot;')}"
389
+ style="width: 100%; height: 650px; border: 1px solid #ddd; border-radius: 4px;"
390
+ sandbox="allow-scripts allow-same-origin">
391
+ </iframe>
392
+ </div>
393
+ """
394
+
395
+ html_sections.append(iframe_html)
396
 
397
  if not html_sections:
398
  return "<p>Could not prepare HOMO/LUMO visualizations.</p>"
force_rebuild.txt CHANGED
@@ -1,2 +1,2 @@
1
  # Force rebuild
2
- 2025-11-08 v14
 
1
  # Force rebuild
2
+ 2025-11-08 v15