Blog Pages

Chemistry Molecule Visualizer

 import streamlit as st

from rdkit import Chem

from rdkit.Chem import Draw, Descriptors

import py3Dmol


def mol_to_3d_view(smiles):

    mol = Chem.MolFromSmiles(smiles)

    mb = Chem.AddHs(mol)

    Chem.EmbedMolecule(mb)

    mol_block = Chem.MolToMolBlock(mb)


    viewer = py3Dmol.view(width=400, height=400)

    viewer.addModel(mol_block, 'mol')

    viewer.setStyle({'stick': {}})

    viewer.zoomTo()

    return viewer


st.title("🧪 Chemistry Molecule Visualizer")


smiles = st.text_input("Enter SMILES string", "CC(=O)O")  # Acetic Acid


if smiles:

    mol = Chem.MolFromSmiles(smiles)

    

    if mol:

        st.subheader("📌 Molecular Structure (2D)")

        st.image(Draw.MolToImage(mol, size=(300, 300)))


        st.subheader("🔬 Properties")

        st.markdown(f"**Formula**: {Chem.rdMolDescriptors.CalcMolFormula(mol)}")

        st.markdown(f"**Molecular Weight**: {Descriptors.MolWt(mol):.2f} g/mol")


        st.subheader("🧬 3D Structure")

        viewer = mol_to_3d_view(smiles)

        viewer_html = viewer._make_html()

        st.components.v1.html(viewer_html, height=450)

    else:

        st.error("Invalid SMILES string. Try again.")


No comments:

Post a Comment