Customization

Open In Colab

The grid can be customized quite extensively, from the content that is displayed to the look of the grid and images.

[1]:
# uncomment and run if you're on Google Colab
# !pip install rdkit mols2grid
# !wget https://raw.githubusercontent.com/rdkit/rdkit/master/Docs/Book/data/solubility.test.sdf
[2]:
from pathlib import Path

from rdkit import RDConfig

import mols2grid


SDF_FILE = (
    f"{RDConfig.RDDocsDir}/Book/data/solubility.test.sdf"
    if Path(RDConfig.RDDocsDir).is_dir()
    else "solubility.test.sdf"
)

To display all the arguments available, type help(mols2grid.display)

[3]:
mols2grid.display(
    SDF_FILE,
    # rename fields for the output document
    rename={"SOL": "Solubility", "SOL_classification": "Class", "NAME": "Name"},
    # set what's displayed on the grid
    subset=["ID", "img", "Solubility"],
    # set what's displayed on the hover tooltip
    tooltip=["Name", "SMILES", "Class", "Solubility"],
    # style for the grid labels and tooltips
    style={
        "Solubility": lambda x: "color: red; font-weight: bold;" if x < -3 else "",
        "__all__": lambda x: "background-color: azure;" if x["Solubility"] > -1 else "",
    },
    # change the precision and format (or other transformations)
    transform={"Solubility": lambda x: round(x, 2)},
    # sort the grid in a different order by default
    sort_by="Name",
    # molecule drawing parameters
    fixedBondLength=25,
    clearBackground=False,
)
[3]:

See rdkit.Chem.Draw.rdMolDraw2D.MolDrawOptions for the molecule drawing options available.

The grid’s look can also be customized to an even greater extent:

[4]:
# some unnecessarily complicated CSS stylesheet 🌈
# use .m2g-cell to select each grid's cell
# or .data for every data field
# or .data-<field> for a specific field
css_style = """
/* rainbow background */
.m2g-cell {
    background: linear-gradient(124deg, #ff2400, #e81d1d, #e8b71d, #e3e81d, #1de840, #1ddde8, #2b1de8, #dd00f3, #dd00f3);
    background-size: 500% 500%;
    -webkit-animation: rainbow 10s ease infinite;
    animation: rainbow 10s ease infinite;
}
.m2g-cell:hover {
    border-color: red !important;
}
/* rainbow font color */
.data {
    font-weight: bold;
    -webkit-text-stroke: 1px black;
    background: linear-gradient(to right, #ef5350, #f48fb1, #7e57c2, #2196f3, #26c6da, #43a047, #eeff41, #f9a825, #ff5722);
    -webkit-background-clip: text;
    color: transparent;
}
/* background animation */
@-webkit-keyframes rainbow {
    0% {background-position: 0% 50%}
    50% {background-position: 100% 50%}
    100% {background-position: 0% 50%}
}
@keyframes rainbow {
    0% {background-position: 0% 50%}
    50% {background-position: 100% 50%}
    100% {background-position: 0% 50%}
}
"""

mols2grid.display(
    SDF_FILE,
    # RDKit drawing options
    comicMode=True,
    fixedBondLength=20,
    bondLineWidth=1,
    # custom atom colour palette (all white)
    atomColourPalette={z: (1, 1, 1) for z in range(1, 295)},
    # mols2grid options
    subset=["NAME", "img"],
    custom_css=css_style,
    fontfamily='"Comic Sans MS", "Comic Sans", cursive;',
    # image size
    size=(130, 80),
    # number of results per page
    n_items_per_page=20,
    # border around each cell
    border="5px ridge cyan",
    # gap between cells
    gap=3,
    # disable selection
    selection=False,
)
[4]: