SupraDashboard / src /viz /structure.py
Tianyi-Billy-Ma
Deploy: simplify codebase (dead-code removal, behavior-preserving)
1727091
Raw
History Blame Contribute Delete
713 Bytes
"""Render a guest 2D structure from SMILES with RDKit (no external service)."""
from __future__ import annotations
from io import BytesIO
def render_smiles(smiles: str, size: int = 320):
"""Return a PIL.Image of the molecule, or None if SMILES is empty/invalid."""
if not smiles:
return None
try:
from rdkit import Chem
from rdkit.Chem.Draw import rdMolDraw2D
except ImportError:
return None
mol = Chem.MolFromSmiles(smiles)
if mol is None:
return None
drawer = rdMolDraw2D.MolDraw2DCairo(size, size)
drawer.DrawMolecule(mol)
drawer.FinishDrawing()
from PIL import Image
return Image.open(BytesIO(drawer.GetDrawingText()))