Rdkit run with streamlit leads to argument error

The following example works fine if it is run directly from the python file.

import streamlit as st

from rdkit import Chem
from rdkit.Chem import Draw

compound_smiles = 'C'
list_compounds = [Chem.MolFromSmiles(compound_smiles)]
fig = Draw.MolsToGridImage(list_compounds, legends = ['methane'],
                               molsPerRow = 1, maxMols = 1)

If it is run with streamlit, it produces the following error:

ArgumentError: Python argument types in MolDraw2D.DrawMolecules(MolDraw2DCairo, list) did not match C++ signature: DrawMolecules(RDKit::MolDraw2D {lvalue} self, boost::python::api::object mols, boost::python::api::object highlightAtoms=None, boost::python::api::object highlightBonds=None, boost::python::api::object highlightAtomColors=None, boost::python::api::object highlightBondColors=None, boost::python::api::object highlightAtomRadii=None, boost::python::api::object confIds=None, boost::python::api::object legends=None)

Traceback:

File "/home/lili/SDSC/Projects/MSEI/raw-data-preprocessing/conda-env-full/lib/python3.7/site-packages/streamlit/ScriptRunner.py", line 322, in _run_script
    exec(code, module.__dict__)
File "/home/lili/SDSC/Projects/MSEI/raw-data-preprocessing/trystuff/rdkit-streamlit-minimalexample.py", line 9, in <module>
    molsPerRow = 1, maxMols = 1)
File "/home/lili/SDSC/Projects/MSEI/raw-data-preprocessing/conda-env-full/lib/python3.7/site-packages/rdkit/Chem/Draw/__init__.py", line 497, in MolsToGridImage
    highlightBondLists=highlightBondLists, **kwargs)
File "/home/lili/SDSC/Projects/MSEI/raw-data-preprocessing/conda-env-full/lib/python3.7/site-packages/rdkit/Chem/Draw/__init__.py", line 443, in _MolsToGridImage
    highlightBonds=highlightBondLists, **kwargs)

Any idea how this could be solved?

Thanks in advance!

Hi @Lili, welcome to the Streamlit community!

Where does this work when you are running it, and what is it supposed to do? On my Ubuntu 18.04 machine, it doesn’t work either:

Python 3.8.3 (default, May 19 2020, 18:47:26) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from rdkit import Chem                                                                           

In [2]: from rdkit.Chem import Draw                                                                      

In [3]: compound_smiles = 'C'                                                                            

In [4]: list_compounds = [Chem.MolFromSmiles(compound_smiles)]                                           

In [5]: fig = Draw.MolsToGridImage(list_compounds, legends = ['methane'], 
   ...:                                molsPerRow = 1, maxMols = 1)                                      
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-5-d6f40020f9da> in <module>
----> 1 fig = Draw.MolsToGridImage(list_compounds, legends = ['methane'],
      2                                molsPerRow = 1, maxMols = 1)

~/miniconda3/envs/maddy/lib/python3.8/site-packages/rdkit/Chem/Draw/__init__.py in MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, useSVG, **kwargs)
    575                           highlightBondLists=highlightBondLists, **kwargs)
    576   else:
--> 577     return _MolsToGridImage(mols, molsPerRow=molsPerRow, subImgSize=subImgSize, legends=legends,
    578                             highlightAtomLists=highlightAtomLists,
    579                             highlightBondLists=highlightBondLists, **kwargs)

~/miniconda3/envs/maddy/lib/python3.8/site-packages/rdkit/Chem/Draw/__init__.py in _MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, drawOptions, **kwargs)
    522           setattr(dops, k, v)
    523           del kwargs[k]
--> 524     d2d.DrawMolecules(list(mols), legends=legends, highlightAtoms=highlightAtomLists,
    525                       highlightBonds=highlightBondLists, **kwargs)
    526     d2d.FinishDrawing()

ArgumentError: Python argument types in
    MolDraw2D.DrawMolecules(MolDraw2DCairo, list)
did not match C++ signature:
    DrawMolecules(RDKit::MolDraw2D {lvalue} self, boost::python::api::object mols, boost::python::api::object highlightAtoms=None, boost::python::api::object highlightBonds=None, boost::python::api::object highlightAtomColors=None, boost::python::api::object highlightBondColors=None, boost::python::api::object highlightAtomRadii=None, boost::python::api::object confIds=None, boost::python::api::object legends=None)

So I’m not sure this is a Streamlit issue, unless there is an interaction somewhere on install.

Hi @randyzwitch, thanks for your answer!

It works on my Ubuntu 18.04 with Python 3.7.7. The code should draw an image with the molecular structure of methane based on its so-called smiles (‘C’).

Any idea where I can start looking to solve this?

Not sure where to get started, but what I do know is that Cairo is one of those fiddly C libraries that needs just the right setup, and it sounds like this one of those problems that conda was created to fix.

Given that it complains about the function signature, I suspect that the issue is that the Python library requires a specific version of Cairo, and if there is a different version installed, then Python binds to that wrong version and everything blows up. :frowning:

Simply adding this line

from rdkit.Chem.Draw import IPythonConsole

to the imports solved it. Glad it was so easy!

Thanks a lot for your time!

1 Like

Awesome, glad you figured it out! Does this now allow for use within Streamlit?

Yes, now it runs smoothly within streamlit.

1 Like