Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in IPython display after setting a molecule property #5192

Closed
dwhswenson opened this issue Apr 12, 2022 · 2 comments
Closed

Bug in IPython display after setting a molecule property #5192

dwhswenson opened this issue Apr 12, 2022 · 2 comments
Labels
Milestone

Comments

@dwhswenson
Copy link

Describe the bug

After setting a property on a molecule, the IPython display code takes a code path that leads to an unbound variable.

To Reproduce

In Jupyter notebook:

import rdkit

mol = rdkit.Chem.MolFromSmiles("COC")
mol.SetProp('foo', 'bar')
mol

Expected behavior

No error.

Screenshots (well, traceback, since that's searchable)

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
File ~/mambaforge/envs/openfe-notebooks/lib/python3.9/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/mambaforge/envs/openfe-notebooks/lib/python3.9/site-packages/rdkit/Chem/Draw/IPythonConsole.py:130, in _toHTML(mol)
    127   else:
    128     content = Draw._moltoSVG(mol, molSize, [], nm, kekulize=kekulizeStructures,
    129                              drawOptions=drawOptions)
--> 130 res.append(f'<tr><td colspan=2 style="text-align:center">{content}</td></tr>')
    132 for i, (pn, pv) in enumerate(props.items()):
    133   if ipython_maxProperties >= 0 and i >= ipython_maxProperties:

UnboundLocalError: local variable 'content' referenced before assignment

Note: The image does show in the output as well, but obviously the error isn't desirable.

Configuration (please complete the following information):

conda list
# packages in environment at /Users/dwhs/mambaforge/envs/openfe-notebooks:
#
# Name                    Version                   Build  Channel
ambertools                21.12            py39hf80593e_0    conda-forge
amberutils                21.0                     pypi_0    pypi
appnope                   0.1.3              pyhd8ed1ab_0    conda-forge
argon2-cffi               21.3.0             pyhd8ed1ab_0    conda-forge
argon2-cffi-bindings      21.2.0           py39h89e85a6_1    conda-forge
arpack                    3.7.0                hefb7bc6_2    conda-forge
asttokens                 2.0.5              pyhd8ed1ab_0    conda-forge
astunparse                1.6.3              pyhd8ed1ab_0    conda-forge
attrs                     21.4.0             pyhd8ed1ab_0    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                        py_2    conda-forge
backports.functools_lru_cache 1.6.4              pyhd8ed1ab_0    conda-forge
beautifulsoup4            4.10.0             pyha770c72_0    conda-forge
biopython                 1.79             py39h89e85a6_1    conda-forge
bleach                    4.1.0              pyhd8ed1ab_0    conda-forge
blosc                     1.21.0               he49afe7_0    conda-forge
boost                     1.74.0           py39ha1f3e3e_5    conda-forge
boost-cpp                 1.74.0               h8b082ac_8    conda-forge
brotli                    1.0.9                h5eb16cf_7    conda-forge
brotli-bin                1.0.9                h5eb16cf_7    conda-forge
bzip2                     1.0.8                h0d85af4_4    conda-forge
c-ares                    1.18.1               h0d85af4_0    conda-forge
ca-certificates           2021.10.8            h033912b_0    conda-forge
cairo                     1.16.0               h8023c5d_1  
certifi                   2021.10.8        py39h6e9494a_2    conda-forge
cffi                      1.15.0           py39hc55c11b_1  
cftime                    1.6.0            py39h86b5767_0    conda-forge
click                     8.1.2            py39h6e9494a_0    conda-forge
colorama                  0.4.4              pyh9f0ad1d_0    conda-forge
coverage                  6.3.2            py39h63b48b0_2    conda-forge
curl                      7.82.0               h9f20792_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
cyjupyter                 0.2.0                    pypi_0    pypi
cython                    0.29.28          py39hfd1d529_2    conda-forge
debugpy                   1.5.1            py39h9fcab8e_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
defusedxml                0.7.1              pyhd8ed1ab_0    conda-forge
entrypoints               0.4                pyhd8ed1ab_0    conda-forge
execnet                   1.9.0              pyhd8ed1ab_0    conda-forge
executing                 0.8.3              pyhd8ed1ab_0    conda-forge
expat                     2.4.8                h96cf925_0    conda-forge
fftw                      3.3.10          nompi_hf082fe4_102    conda-forge
flit-core                 3.7.1              pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.0               h676cef8_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.31.2           py39h63b48b0_0    conda-forge
freetype                  2.10.4               h4cff582_1    conda-forge
gettext                   0.21.0               h7535e17_0  
giflib                    5.2.1                hbcb3906_2    conda-forge
glib                      2.68.4               he49afe7_0    conda-forge
glib-tools                2.68.4               he49afe7_0    conda-forge
greenlet                  1.1.2            py39hfd1d529_2    conda-forge
griddataformats           0.7.0              pyhd8ed1ab_0    conda-forge
gsd                       2.5.1            py39hc89836e_0    conda-forge
gufe                      0.2                pyhd8ed1ab_0    conda-forge
hdf4                      4.2.15               hefd3b78_3    conda-forge
hdf5                      1.12.1          nompi_ha60fbc9_104    conda-forge
icu                       70.1                 h96cf925_0    conda-forge
importlib-metadata        4.11.3           py39h6e9494a_1    conda-forge
importlib_resources       5.6.0              pyhd8ed1ab_0    conda-forge
iniconfig                 1.1.1              pyh9f0ad1d_0    conda-forge
ipykernel                 6.12.1           py39h71a6800_0    conda-forge
ipython                   8.2.0            py39h6e9494a_0    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
ipywidgets                7.7.0              pyhd8ed1ab_0    conda-forge
jbig                      2.1               h0d85af4_2003    conda-forge
jedi                      0.18.1           py39h6e9494a_1    conda-forge
jinja2                    3.1.1              pyhd8ed1ab_0    conda-forge
joblib                    1.1.0              pyhd8ed1ab_0    conda-forge
jpeg                      9e                   h0d85af4_0    conda-forge
jsonschema                4.4.0              pyhd8ed1ab_0    conda-forge
jupyter_client            7.2.1              pyhd8ed1ab_0    conda-forge
jupyter_core              4.9.2            py39h6e9494a_0    conda-forge
jupyterlab_pygments       0.1.2              pyh9f0ad1d_0    conda-forge
jupyterlab_widgets        1.1.0              pyhd8ed1ab_0    conda-forge
khronos-opencl-icd-loader 2022.01.04           h0d85af4_0    conda-forge
kiwisolver                1.4.2            py39h7248d28_1    conda-forge
krb5                      1.19.3               hb49756b_0    conda-forge
lcms2                     2.12                 h577c468_0    conda-forge
lerc                      3.0                  he49afe7_0    conda-forge
libblas                   3.9.0           13_osx64_openblas    conda-forge
libbrotlicommon           1.0.9                h5eb16cf_7    conda-forge
libbrotlidec              1.0.9                h5eb16cf_7    conda-forge
libbrotlienc              1.0.9                h5eb16cf_7    conda-forge
libcblas                  3.9.0           13_osx64_openblas    conda-forge
libcurl                   7.82.0               h9f20792_0    conda-forge
libcxx                    13.0.1               hc203e6f_0    conda-forge
libdeflate                1.10                 h0d85af4_0    conda-forge
libedit                   3.1.20191231         h0678c8f_2    conda-forge
libev                     4.33                 haf1e3a3_1    conda-forge
libffi                    3.3                  h046ec9c_2    conda-forge
libgfortran               5.0.0           9_3_0_h6c81a4c_23    conda-forge
libgfortran5              9.3.0               h6c81a4c_23    conda-forge
libglib                   2.68.4               hd556434_0    conda-forge
libiconv                  1.16                 haf1e3a3_0    conda-forge
liblapack                 3.9.0           13_osx64_openblas    conda-forge
libllvm10                 10.0.1               h009f743_3    conda-forge
libnetcdf                 4.8.1           nompi_h6609ca0_101    conda-forge
libnghttp2                1.47.0               h942079c_0    conda-forge
libopenblas               0.3.18          openmp_h3351f45_0    conda-forge
libpng                    1.6.37               h7cec526_2    conda-forge
libsodium                 1.0.18               hbcb3906_1    conda-forge
libssh2                   1.10.0               h52ee1ee_2    conda-forge
libtiff                   4.3.0                h17f2ce3_3    conda-forge
libwebp                   1.2.2                h28dabe5_0    conda-forge
libwebp-base              1.2.2                h0d85af4_1    conda-forge
libxcb                    1.13              h0d85af4_1004    conda-forge
libxml2                   2.9.12               he03b247_2    conda-forge
libxslt                   1.1.33               h5bff336_4    conda-forge
libzip                    1.8.0                h8b0c345_1    conda-forge
libzlib                   1.2.11            h6c3fc93_1014    conda-forge
llvm-openmp               13.0.1               hcb1a161_1    conda-forge
llvmlite                  0.36.0           py39h798a4f4_0    conda-forge
lomap                     2.0a0                    pypi_0    pypi
lomap2                    2.0.0              pyhd8ed1ab_0    conda-forge
lxml                      4.8.0            py39h63b48b0_1    conda-forge
lz4-c                     1.9.3                he49afe7_1    conda-forge
markupsafe                2.1.1            py39h63b48b0_1    conda-forge
matplotlib-base           3.5.1            py39hb07454d_0    conda-forge
matplotlib-inline         0.1.3              pyhd8ed1ab_0    conda-forge
mdanalysis                2.1.0            py39hfd1d529_1    conda-forge
mdtraj                    1.9.7            py39h996af62_1    conda-forge
mistune                   0.8.4           py39h89e85a6_1005    conda-forge
mmtf-python               1.1.2                      py_0    conda-forge
mpiplus                   v0.0.1          pyhd8ed1ab_1003    conda-forge
mrcfile                   1.3.0              pyh44b312d_0    conda-forge
msgpack-python            1.0.3            py39h7248d28_1    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
nbclient                  0.5.13             pyhd8ed1ab_0    conda-forge
nbconvert                 6.4.5              pyhd8ed1ab_2    conda-forge
nbconvert-core            6.4.5              pyhd8ed1ab_2    conda-forge
nbconvert-pandoc          6.4.5              pyhd8ed1ab_2    conda-forge
nbformat                  5.3.0              pyhd8ed1ab_0    conda-forge
nbval                     0.9.6              pyh9f0ad1d_0    conda-forge
ncurses                   6.3                  he49afe7_0    conda-forge
nest-asyncio              1.5.5              pyhd8ed1ab_0    conda-forge
netcdf-fortran            4.5.4           nompi_h9ed14b0_100    conda-forge
netcdf4                   1.5.8           nompi_py39he7d1c46_101    conda-forge
networkx                  2.8                pyhd8ed1ab_0    conda-forge
nglview                   3.0.3              pyh8a188c0_0    conda-forge
nose                      1.3.7                   py_1006    conda-forge
notebook                  6.4.10             pyha770c72_0    conda-forge
numba                     0.53.1           py39h32e38f5_1    conda-forge
numexpr                   2.8.0            py39h4d6be9b_1    conda-forge
numpy                     1.22.3           py39hf56e92f_2    conda-forge
ocl_icd_wrapper_apple     1.0.0                hbcb3906_0    conda-forge
openfe                    0.1                      pypi_0    pypi
openff-forcefields        2.0.0              pyh6c4a22f_0    conda-forge
openff-toolkit            0.10.4             pyhd8ed1ab_0    conda-forge
openff-toolkit-base       0.10.4             pyhd8ed1ab_0    conda-forge
openff-units              0.1.5              pyh6c4a22f_0    conda-forge
openff-utilities          0.1.3              pyh6c4a22f_0    conda-forge
openjpeg                  2.4.0                h6e7aa92_1    conda-forge
openmm                    7.7.0           py39h8d72adf_0_khronos    conda-forge
openmmforcefields         0.11.0             pyhd8ed1ab_0    conda-forge
openmmtools               0.21.2             pyhd8ed1ab_0    conda-forge
openssl                   1.1.1n               h6c3fc93_0    conda-forge
packaging                 21.3               pyhd8ed1ab_0    conda-forge
packmol                   20.010               h508aa58_0    conda-forge
packmol-memgen            1.2.1rc0                 pypi_0    pypi
pandas                    1.4.2            py39hbd61c47_0    conda-forge
pandoc                    2.17.1.1             h694c41f_0    conda-forge
pandocfilters             1.5.0              pyhd8ed1ab_0    conda-forge
parmed                    3.4.3            py39h9fcab8e_1    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
patsy                     0.5.2              pyhd8ed1ab_0    conda-forge
pcre                      8.45                 he49afe7_0    conda-forge
pdbfixer                  1.8.1              pyh6c4a22f_0    conda-forge
perl                      5.32.1          2_h0d85af4_perl5    conda-forge
pexpect                   4.8.0              pyh9f0ad1d_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    9.1.0            py39hd2c7aa1_0    conda-forge
pint                      0.19.1             pyhd8ed1ab_0    conda-forge
pip                       22.0.4             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               hbcb3906_0    conda-forge
plugcli                   0.0.1              pyhd8ed1ab_0    conda-forge
pluggy                    1.0.0            py39h6e9494a_3    conda-forge
prometheus_client         0.13.1             pyhd8ed1ab_0    conda-forge
prompt-toolkit            3.0.29             pyha770c72_0    conda-forge
psutil                    5.9.0            py39h63b48b0_1    conda-forge
pthread-stubs             0.4               hc929b4f_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
py                        1.11.0             pyh6c4a22f_0    conda-forge
pycairo                   1.21.0           py39ha25c624_1    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pydantic                  1.9.0            py39h63b48b0_1    conda-forge
pygments                  2.11.2             pyhd8ed1ab_0    conda-forge
pymbar                    3.0.6            py39hc89836e_0    conda-forge
pyparsing                 3.0.7              pyhd8ed1ab_0    conda-forge
pyrsistent                0.18.1           py39h63b48b0_1    conda-forge
pytables                  3.7.0            py39hfd850c7_0    conda-forge
pytest                    7.1.1            py39h6e9494a_1    conda-forge
pytest-cov                3.0.0              pyhd8ed1ab_0    conda-forge
pytest-forked             1.4.0              pyhd8ed1ab_0    conda-forge
pytest-xdist              2.5.0              pyhd8ed1ab_0    conda-forge
python                    3.9.0           h4f09611_5_cpython    conda-forge
python-constraint         1.4.0                      py_0    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-fastjsonschema     2.15.3             pyhd8ed1ab_0    conda-forge
python_abi                3.9                      2_cp39    conda-forge
pytraj                    2.0.6                    pypi_0    pypi
pytz                      2022.1             pyhd8ed1ab_0    conda-forge
pyyaml                    6.0              py39h63b48b0_4    conda-forge
pyzmq                     22.3.0           py39hc2dc7ec_2    conda-forge
rdkit                     2022.03.1        py39h1ae426c_0    conda-forge
readline                  8.1                  h05e3726_0    conda-forge
reportlab                 3.5.68           py39hf37cc50_1    conda-forge
sander                    16.0                     pypi_0    pypi
scikit-learn              1.0.2            py39hd4eea88_0    conda-forge
scipy                     1.8.0            py39h056f1c0_1    conda-forge
seaborn                   0.11.2               hd8ed1ab_0    conda-forge
seaborn-base              0.11.2             pyhd8ed1ab_0    conda-forge
send2trash                1.8.0              pyhd8ed1ab_0    conda-forge
setuptools                62.0.0           py39h6e9494a_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
smirnoff99frosst          1.1.0              pyh44b312d_0    conda-forge
snappy                    1.1.8                hb1e8313_3    conda-forge
soupsieve                 2.3.1              pyhd8ed1ab_0    conda-forge
sqlalchemy                1.4.34           py39h63b48b0_1    conda-forge
sqlite                    3.37.1               hb516253_0    conda-forge
stack_data                0.2.0              pyhd8ed1ab_0    conda-forge
statsmodels               0.13.2           py39hc89836e_0    conda-forge
terminado                 0.13.3           py39h6e9494a_1    conda-forge
testpath                  0.6.0              pyhd8ed1ab_0    conda-forge
threadpoolctl             3.1.0              pyh8a188c0_0    conda-forge
tinydb                    4.7.0              pyhd8ed1ab_0    conda-forge
tk                        8.6.12               h5dbffcc_0    conda-forge
toml                      0.10.2             pyhd8ed1ab_0    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
tornado                   6.1              py39h63b48b0_3    conda-forge
tqdm                      4.64.0             pyhd8ed1ab_0    conda-forge
traitlets                 5.1.1              pyhd8ed1ab_0    conda-forge
typing-extensions         4.1.1                hd8ed1ab_0    conda-forge
typing_extensions         4.1.1              pyha770c72_0    conda-forge
tzdata                    2022a                h191b570_0    conda-forge
unicodedata2              14.0.0           py39h63b48b0_1    conda-forge
validators                0.18.2             pyhd3deb0d_0    conda-forge
wcwidth                   0.2.5              pyh9f0ad1d_2    conda-forge
webencodings              0.5.1                      py_1    conda-forge
wheel                     0.37.1             pyhd8ed1ab_0    conda-forge
widgetsnbextension        3.6.0            py39h6e9494a_0    conda-forge
xmltodict                 0.12.0                     py_0    conda-forge
xorg-kbproto              1.0.7             h35c211d_1002    conda-forge
xorg-libice               1.0.10               h0d85af4_0    conda-forge
xorg-libsm                1.2.3             h0d85af4_1000    conda-forge
xorg-libx11               1.7.2                h0d85af4_0    conda-forge
xorg-libxau               1.0.9                h35c211d_0    conda-forge
xorg-libxdmcp             1.1.3                h35c211d_0    conda-forge
xorg-libxext              1.3.4                h0d85af4_1    conda-forge
xorg-libxt                1.2.1                h0d85af4_2    conda-forge
xorg-xextproto            7.3.0             h35c211d_1002    conda-forge
xorg-xproto               7.0.31            h35c211d_1007    conda-forge
xz                        5.2.5                haf1e3a3_1    conda-forge
yaml                      0.2.5                h0d85af4_2    conda-forge
zeromq                    4.3.4                he49afe7_1    conda-forge
zipp                      3.8.0              pyhd8ed1ab_0    conda-forge
zlib                      1.2.11            h6c3fc93_1014    conda-forge
zstd                      1.5.2                h582d3a0_0    conda-forge

Additional context

I think the bug is in lines 124-126 here, where res is appended to instead of setting the value of content. I'm not sure why this process triggers that code path, but it sure looks like that's the issue.

res = []
if useInteractiveRenderer:
content = InteractiveRenderer.generateHTMLBody(ipython_useSVG, mol, molSize)
else:
if not ipython_useSVG:
png = Draw._moltoimg(mol, molSize, [], nm, returnPNG=True, drawOptions=drawOptions)
png = base64.b64encode(png)
res.append(
f'<tr><td colspan=2 style="text-align:center"><image src="data:image/png;base64,{png.decode()}"></td></tr>'
)
else:
content = Draw._moltoSVG(mol, molSize, [], nm, kekulize=kekulizeStructures,
drawOptions=drawOptions)
res.append(f'<tr><td colspan=2 style="text-align:center">{content}</td></tr>')

See also OpenFreeEnergy/gufe#20.

@greglandrum
Copy link
Member

confirmed. thanks for the detailed report @dwhswenson

@greglandrum
Copy link
Member

FWIW: a workaround until the next patch release is to set IPythonConsole.ipython_useSVG = True

greglandrum added a commit to greglandrum/rdkit that referenced this issue Apr 13, 2022
greglandrum added a commit that referenced this issue Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants