File size: 746 Bytes
9fc3c5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import py3Dmol
import streamlit.components.v1 as components

def mol_to_xyz(mol):
    """

    Converts RDKit molecule to XYZ format string.

    """
    conf = mol.GetConformer()
    atoms = mol.GetAtoms()
    xyz = f"{len(atoms)}\n\n"
    for atom in atoms:
        pos = conf.GetAtomPosition(atom.GetIdx())
        xyz += f"{atom.GetSymbol()} {pos.x:.4f} {pos.y:.4f} {pos.z:.4f}\n"
    return xyz

def show_molecule_3d(mol):
    """

    Returns a Py3Dmol viewer for the molecule.

    """
    xyz = mol_to_xyz(mol)
    viewer = py3Dmol.view(width=500, height=400)
    viewer.addModel(xyz, "xyz")
    viewer.setStyle({"stick": {}})
    viewer.zoomTo()
    html = viewer._make_html()
    components.html(html, height=400)