Make interactive web app with streamlit and RDKit #RDKit #streamlit

Recently @napoles3D shared very useful code which shows integrate rdkit and stremlit. Here is the code.

So I have interest about integrate rdkit and streamlit because streamlit can make web app easily without considering and making UI like jupyter notebook.

So today, I would like to share an example to integrate rdkit and streamlit.

At fist, I made predictive model with solubility dataset. The code is shown below. I used randomforest classifier with default settings.

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import DataStructs
import numpy as np
classes = {'(A) low':0, '(B) medium':1, '(C) high':2 }
rclasses = {0:'(A) low', 1:'(B) medium', 2: '(C) high'}
def mol2fp(mol):
    fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2)
    arr = np.zeros((0,))
    DataStructs.ConvertToNumpyArray(fp, arr)
    return arr

def make_data(sdf):
    mols = [m for m in Chem.SDMolSupplier(sdf) if m !=None]
    X = np.array([mol2fp(m) for m in mols])
    Y = [classes[m.GetProp('SOL_classification')] for m in mols]
    Y = np.array(Y)
    return (X, Y)
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import datamaker
import pickle

sdf = './solubility.train.sdf'
rfc = RandomForestClassifier()
x, y = datamaker.make_data(sdf), y)
pickle.dump(rfc, open('rf.pkl', 'wb'))

To build model, I run the from shell. ‘$python’

After that, I could get a predictive model for solubility, so next I wrote code for web app.
import pickle
import datamaker
import streamlit as st
st.title('Streamlit + RDKit :rocket:')
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import SimilarityMaps
from io import BytesIO
from functools import partial
from PIL import Image
from rdkit.Chem.Draw import rdDepictor

rfc = pickle.load(open('rf.pkl', 'rb'))
fpfunc = partial(SimilarityMaps.GetMorganFingerprint, radius=2)

mols = [m for m in Chem.SDMolSupplier('./solubility.test.sdf')]

option = st.selectbox(
    'Please select index of test molecules',
    [i for i in range(len(mols))]

st.write('you selected:', option, Chem.MolToSmiles(mols[option]))
fp = [datamaker.mol2fp(mols[option])]
kls = rfc.predict(fp)

img = Draw.MolToImage(mols[option])
bio = BytesIO(), format='png')

st.write('predicted class:', datamaker.rclasses[kls[0]])

The code is very simple… Difference between @napoles3D‘s code and mine is that I used BytesIO to prevent making temporary files.

OK, now all task was done. Let’s run the app. It’s easy just type ‘$streamlit run’

After run the command, access http://localhost:8501/, interactive predictive app is launching.


Streamlit has lots of functions so there are lots of space of improvement I think. In summary, streamlit is very interesting app for chemoinformatics.

I uploaded my code on github. Readers who has interest the task, please check it ;)


Published by iwatobipen

I'm medicinal chemist in mid size of pharmaceutical company. I love chemoinfo, cording, organic synthesis, my family.

One thought on “Make interactive web app with streamlit and RDKit #RDKit #streamlit

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: