MolGrid object#
The class that creates and controls the grid of molecules. Allows for a more advanced usage, like filtering the grid using external controls such as slider widgets. See the notebooks for examples.
- class mols2grid.MolGrid(df, smiles_col='SMILES', mol_col=None, size=(130, 90), useSVG=True, prerender=False, cache_selection=False, removeHs=False, use_coords=False, coordGen=True, MolDrawOptions=None, name='default', rename=None, **kwargs)[source]#
Class that handles drawing molecules, rendering the HTML document and saving or displaying it in a Jupyter Notebook.
Parameters: Data#
- dfpandas.DataFrame, dict or list, required
Dataframe containing a SMILES or mol column, or dictionary containing a list of SMILES, or list of dictionnaries containing a SMILES field.
- smiles_colstr or None, default=âSMILESâ
Name of the SMILES column in the dataframe, if available.
- mol_colstr or None, default=None
Name of an RDKit molecule column. If available, coordinates and atom/bonds annotations from this will be used for depiction.
Parameters: Display#
- sizetuple, default=(130, 90)
The size of the drawing canvas. The cell minimum width is set to the width of the image, so if the cell padding is increased, the image will be displayed smaller.
- useSVGbool, default=True
Use SVG images instead of PNG.
- prerenderbool, default=False
Prerender images for the entire dataset, or generate them on-the-fly. Prerendering is slow and memory-hungry, but required when
template="static"oruseSVG=False.- cache_selectionbool, default=False
Restores the selection from a previous grid with the same name.
Parameters: Mols#
- removeHsbool, default=False
Remove hydrogen atoms from the drawings.
- use_coordsbool, default=False
Use the coordinates of the molecules (only relevant when an SDF file, a list of molecules or a DataFrame of RDKit molecules were used as input.)
- coordGenbool, default=True
Use the CoordGen library instead of the RDKit one to depict the molecules in 2D.
- MolDrawOptionsrdkit.Chem.Draw.rdMolDraw2D.MolDrawOptions or None, default=None
Drawing options. Useful for making highly customized drawings.
Parameters: Customization#
- namestr, default=âdefaultâ
Name of the grid. Used when retrieving selections from multiple grids at the same time.
- renamedict or None, default=None
Rename the properties in the final document.
- kwargsobject
MolDrawOptionsattributes, and the additionalatomColourPalette.
Notes
On-the-fly rendering of images does not read the atom colour palette from the
MolDrawOptionsparameter. If this is needed, use the following:MolGrid(df, atomColourPalette={1: (.8, 0, 1)})
Changed in version 0.1.0: Added
renameparameter to replacemapping.New in version 0.2.0: Added
prerenderandcache_selectionparameters.Changed in version 0.2.0: Images are now generated on-the-fly.
use_coordsis nowFalseby default to avoid a systematic error when usingMolGrid.from_sdf.- display(use_iframe=False, iframe_width='100%', iframe_height=None, iframe_allow='clipboard-write', iframe_sandbox='allow-scripts allow-same-origin allow-downloads allow-popups allow-modals', **kwargs)[source]#
Render and display the grid in a Jupyter notebook.
- Returns:
view
- Return type:
- filter(mask)[source]#
Filters the grid using a mask (boolean array).
- Parameters:
mask (list, pandas.Series or numpy.ndarray) â Boolean array:
Truewhen the item should be displayed,Falseif it should be filtered out.
- classmethod from_mols(mols, **kwargs)[source]#
Set up the dataframe used by mols2grid directly from a list of RDKit molecules.
- classmethod from_sdf(sdf_file, **kwargs)[source]#
Set up the dataframe used by mols2grid directly from an SDFile.
- Parameters:
sdf_file (str, pathlib.Path) â Path to the SDF file (.sdf or .sdf.gz)
kwargs (object) â Other arguments passed on initialization
Changed in version 0.2.0: Added support for .sdf.gz files
- get_marimo_selection()[source]#
Returns a marimo state object containing the list of selected indices. Only available when running in marimo.
- Returns:
A getter function for the selection state. Calling it with no arguments returns the current list of selected IDs.
- Return type:
getter
- get_selection()[source]#
Retrieve the dataframe subset corresponding to your selection.
- Return type:
- mol_to_img(mol)[source]#
Convert an RDKit mol to an inline PNG image containing a drawing of the molecule.
- render(template='interactive', **kwargs)[source]#
Returns the HTML document corresponding to the âinteractiveâ or âstaticâ template. See
to_interactive()andto_static()for the full list of arguments.- Parameters:
template (str) â
What kind of grid to draw:
- interactive
An interactive grid that layouts the original set of molecules on several pages, allowing for selecting molecules and filtering them using text or substructure queries.
- static
A simple table with all molecules displayed at once, similarly to RDKitâs
MolsToGridImage(). This template is mainly used for printing on paper or in a PDF file. Most of the interactive actions arenât available.
- property template#
Kind of grid displayed, one of:
interactive
static
- to_interactive(subset=None, tooltip=None, tooltip_fmt='<strong>{key}</strong>: {value}', tooltip_placement='auto', transform=None, sort_by=None, use_iframe=False, truncate=True, n_items_per_page=24, selection=True, substruct_highlight=None, single_highlight=False, border='1px solid #cccccc', gap=0, pad=10, fontsize='12px', fontfamily="'DejaVu', sans-serif", textalign='center', background_color='white', hover_color='rgba(0,0,0,0.05)', custom_css=None, style=None, custom_header=None, callback=None)[source]#
Returns the HTML document for the âinteractiveâ template.
Parameters: Display#
- subset: list or None, default=None
Columns to be displayed in each cell of the grid. Each columnâs value will be displayed from top to bottom in the order provided. The
"img"and"mols2grid-id"columns are displayed by default, however you can still add the"img"column if you wish to change the display order.- tooltiplist, None or False, default=None
Columns to be displayed inside the tooltip. When no subset is set, all columns will be listed in the tooltip by default. Use
Falseto hide the tooltip.- tooltip_fmtstr, default=â<strong>{key}</strong>: {value}â
Format string of each key/value pair in the tooltip.
- tooltip_placementstr, default=âautoâ
Position of the tooltip:
auto,top,bottom,leftorright.- transformdict or None, default=None
Functions applied to specific items in all cells. The dict must follow a
key: functionstructure where the key must correspond to one of the columns insubsetortooltip. The function takes the itemâs value as input and transforms it, for example:transform={ "Solubility": lambda x: f"{x:.2f}", "Melting point": lambda x: f"MP: {5/9*(x-32):.1f}°C" }
These transformations only affect columns in
subsetandtooltip, and do not interfere withstyle.- sort_bystr or None, default=None
Sort the grid according to the following field (which must be present in
subsetortooltip).- use_iframebool, default=False
Whether to use an iframe to display the grid. When the grid is displayed inside a Jupyter Notebook or JupyterLab, this will default to
True.- truncate: bool, default=True/False
Whether to truncate the text in each cell if itâs too long. Defaults to
Truefor interactive grids,Falsefor static grid.- n_items_per_page, default=24
Number of items to display per page. A multiple of 12 is recommended for optimal display.
- selectionbool, default=True
Enables the selection of molecules and displays a checkbox at the top of each cell. In the context of a Jupyter Notebook, this gives you access to your selection (index and SMILES) through
mols2grid.get_selection()orMolGrid.get_selection(). In all cases, you can export your selection by clicking on the triple-dot menu.
Parameters: Mols#
- substruct_highlightbool or None, default=None
Highlight substructure when using the SMARTS search. Active by default when
prerender=False.- single_highlightbool, default=False
Highlight only the first match of the substructure query.
Parameters: CSS#
- borderstr, default=â1px solid #ccccccâ
Styling of the border around each cell.
- gapint, default=0
Size in pixels of the gap between cells.
- padint, default=10
Size in pixels of the cell padding.
- fontsizestr, default=â12pxâ
Font size of the text displayed in each cell.
- fontfamilystr, default=ââDejaVuâ, sans-serifâ
Font used for the text in each cell.
- textalignstr, default=âcenterâ
Alignment of the text in each cell.
- background_colorstr, default=âwhiteâ
Background color of a cell.
- hover_colorstr, default=ârgba(0,0,0,0.05)â
Background color when hovering a cell.
- custom_cssstr or None, default=None
Custom CSS properties applied to the generated HTML. Please note that the CSS will apply to the entire page if no iframe is used (see
use_iframefor more details).- styledict or None, default=None
CSS styling applied to each item in a cell. The dict must follow a
key: functionstructure where the key must correspond to one of the columns insubsetortooltip. The function takes the itemâs value as input, and outputs a valid CSS styling. For example, if you want to color the text corresponding to the âSolubilityâ column in your dataframe:style={"Solubility": lambda x: "color: red" if x < -5 else ""}
You can also style a whole cell using the
__all__key, the corresponding function then has access to all values for each cell:style={ "__all__": lambda x: "color: red" if x["Solubility"] < -5 else "" }
Parameters: Customization#
- custom_headerstr or None, default=None
Custom libraries to be loaded in the header of the document.
- callbackstr, callable or None, default=None
JavaScript or Python callback to be executed when clicking on an image. A dictionnary containing the data for the full cell is directly available as
datain JS. For Python, the callback function must havedataas the first argument to the function. All the values in thedatadict are parsed as strings, except âmols2grid-idâ which is always an integer. Note that fields containing spaces in their name will be replaced by hyphens, i.e. âmol weightâ becomes available asdata["mol-weight"].
- returns:
html_document
- rtype:
str
Notes
If
subset=None, tooltip=None, the index and image will be directly displayed on the grid while the remaining fields will be in the tooltip.The cell width is defined by the size[0] parameter.
New in version 0.1.0: Added
sort_by,custom_css,custom_headerandcallbackarguments. Added the ability to style an entire cell withstyle={"__all__": <function>}.New in version 0.2.0: Added
substruct_highlightargument.Changed in version 0.2.2: If both
subsetandtooltipareNone, the index and image will be directly displayed on the grid while the remaining fields will be in the tooltip.Changed in version 1.0.0:
callbackcan now be a lambda function. Ifprerender=True, substructure highlighting will be automatically disabled if it wasnât explicitely set toTrueinstead of raising an error.
- to_static(subset=None, tooltip=None, tooltip_fmt='<strong>{key}</strong>: {value}', tooltip_trigger='focus', tooltip_placement='auto', transform=None, sort_by=None, use_iframe=False, truncate=False, n_cols=5, border='1px solid #cccccc', gap=0, pad=10, fontsize='12px', fontfamily="'DejaVu', sans-serif", textalign='center', custom_css=None, style=None, custom_header=None)[source]#
Returns the HTML document for the âstaticâ template
Parameters: Display#
- subset: list or None, default=None
Columns to be displayed in each cell of the grid. Each columnâs value will be displayed from top to bottom in the order provided. The
"img"and"mols2grid-id"columns are displayed by default, however you can still add the"img"column if you wish to change the display order.- tooltiplist, None or False, default=None
Columns to be displayed inside the tooltip. When no subset is set, all columns will be listed in the tooltip by default. Use
Falseto hide the tooltip.- tooltip_fmtstr, default=â<strong>{key}</strong>: {value}â
Format string of each key/value pair in the tooltip.
- tooltip_triggerstr, default=âfocusâ
Sequence of triggers for the tooltip:
click,hoverorfocus- tooltip_placementstr, default=âautoâ
Position of the tooltip:
auto,top,bottom,leftorright.- transformdict or None, default=None
Functions applied to specific items in all cells. The dict must follow a
key: functionstructure where the key must correspond to one of the columns insubsetortooltip. The function takes the itemâs value as input and transforms it, for example:transform={ "Solubility": lambda x: f"{x:.2f}", "Melting point": lambda x: f"MP: {5/9*(x-32):.1f}°C" }
These transformations only affect columns in
subsetandtooltip, and do not interfere withstyle.- sort_bystr or None, default=None
Sort the grid according to the following field (which must be present in
subsetortooltip).- use_iframebool, default=False
Whether to use an iframe to display the grid. When the grid is displayed inside a Jupyter Notebook or JupyterLab, this will default to
True.- truncate: bool, default=False
Whether to truncate the text in each cell if itâs too long.
- n_colsint, default=5
Number of columns in the table.
Parameters: CSS#
- borderstr, default=â1px solid #ccccccâ
Styling of the border around each cell.
- gapint, default=0
Size in pixels of the gap between cells.
- pad: int, default=10
Size in pixels of the cell padding.
- fontsizestr, default=â12ptâ
Font size of the text displayed in each cell.
- fontfamilystr, default=ââDejaVuâ, sans-serifâ
Font used for the text in each cell.
- textalignstr, default=âcenterâ
Alignment of the text in each cell.
- custom_cssstr or None, default=None
Custom CSS properties applied to the generated HTML. Please note that the CSS will apply to the entire page if no iframe is used (see
use_iframefor more details).- styledict or None, default=None
CSS styling applied to each item in a cell. The dict must follow a
key: functionstructure where the key must correspond to one of the columns insubsetortooltip. The function takes the itemâs value as input, and outputs a valid CSS styling. For example, if you want to color the text corresponding to the âSolubilityâ column in your dataframe:style={"Solubility": lambda x: "color: red" if x < -5 else ""}
You can also style a whole cell using the
__all__key, the corresponding function then has access to all values for each cell:style={ "__all__": lambda x: "color: red" if x["Solubility"] < -5 else "" }
Parameters: Customization#
- custom_headerstr or None
Custom libraries to be loaded in the header of the document
- returns:
html_document
- rtype:
str
Notes
If
subset=None, tooltip=None, the index and image will be directly displayed on the grid while the remaining fields will be in the tooltip.New in version 0.1.0: Added the ability to style an entire cell with
style={"__all__": <function>}New in version 0.2.2: Added
sort_by,custom_css,custom_headerarguments.Changed in version 0.2.2: If both
subsetandtooltipareNone, the index and image will be directly displayed on the grid while the remaining fields will be in the tooltip.