Skip to content

Commit

Permalink
Added string formatting. Would solve cbouy#10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Wallner committed Apr 6, 2021
1 parent 849cf49 commit 0083e63
Show file tree
Hide file tree
Showing 2 changed files with 389 additions and 521 deletions.
867 changes: 350 additions & 517 deletions demo.ipynb

Large diffs are not rendered by default.

43 changes: 39 additions & 4 deletions mols2grid/molgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def to_pages(self, subset=None, tooltip=None,
fontsize="12pt", fontfamily="'DejaVu', sans-serif",
textalign="center", tooltip_fmt="<strong>{key}</strong>: {value}",
tooltip_trigger="click hover", tooltip_placement="bottom",
hover_color="#e7e7e7", style=None, selection=True):
hover_color="#e7e7e7", style=None, selection=True, fmt=None):
"""Returns the HTML document for the "pages" template
Parameters
Expand Down Expand Up @@ -251,6 +251,15 @@ def to_pages(self, subset=None, tooltip=None,
Enables the selection of molecules and displays a checkbox at the top of each
cell. This is only usefull in the context of a Jupyter notebook, which gives
you access to your selection (index and SMILES) through `mols2grid.selection`
fmt : dict or None
Format string or callable to apply to each item in a cell. The dict
must follow a `key: formatter` structure where the key must correspond
to one of the columns in `subset`. The formatter is either a format
string that will be applied with `formatter.format(value)` or a callable
that will be called with `formatter(value)`. For example,
`fmt={"Solubility": "{:.2f}", "clogP": lambda x: f"clogP: {x:.2f}"}`
will format the solubility with 2 decimals and add the text "clogP: "
before the clogP value.
"""
df = self.dataframe.drop(columns=self.mol_col).copy()
cell_width = self.img_size[0]
Expand All @@ -266,6 +275,8 @@ def to_pages(self, subset=None, tooltip=None,
sort_cols = ["mols2grid-id"] + sort_cols
if style is None:
style = {}
if fmt is None:
fmt = {}
value_names = list(set(subset + [smiles]))
value_names = [f"data-{col}" for col in value_names]
width = n_cols * (cell_width + 2 * (gap + 2))
Expand Down Expand Up @@ -319,6 +330,12 @@ def to_pages(self, subset=None, tooltip=None,
item = '<div class="cell" data-mols2grid-id="0">{}{}</div>'.format(
checkbox if selection else "",
"".join(content))
for fmtkey, fmtitem in fmt.items():
if fmtkey in final_columns:
try:
df[fmtkey] = df[fmtkey].apply(fmtitem)
except TypeError:
df[fmtkey] = df[fmtkey].apply(lambda x: fmtitem.format(x))
df = df[final_columns].rename(columns=column_map)

template = env.get_template('pages.html')
Expand Down Expand Up @@ -368,7 +385,7 @@ def to_table(self, subset=None, tooltip=None, n_cols=6,
fontsize="12pt", fontfamily="'DejaVu', sans-serif",
textalign="center", tooltip_fmt="<strong>{key}</strong>: {value}",
tooltip_trigger="click hover", tooltip_placement="bottom",
hover_color="#e7e7e7", style=None):
hover_color="#e7e7e7", style=None, fmt=None):
"""Returns the HTML document for the "table" template
Parameters
Expand Down Expand Up @@ -409,6 +426,15 @@ def to_table(self, subset=None, tooltip=None, n_cols=6,
`style={"Solubility": lambda x: "color: red" if x < -5 else "color: black"}`
if you want to color the text corresponding to the "Solubility"
column in your dataframe.
fmt : dict or None
Format string or callable to apply to each item in a cell. The dict
must follow a `key: formatter` structure where the key must correspond
to one of the columns in `subset`. The formatter is either a format
string that will be applied with `formatter.format(value)` or a callable
that will be called with `formatter(value)`. For example,
`fmt={"Solubility": "{:.2f}", "clogP": lambda x: f"clogP: {x:.2f}"}`
will format the solubility with 2 decimals and add the text "clogP: "
before the clogP value.
"""
tr = []
data = []
Expand All @@ -420,6 +446,8 @@ def to_table(self, subset=None, tooltip=None, n_cols=6,
subset = [subset.pop(subset.index("img"))] + subset
if style is None:
style = {}
if fmt is None:
fmt = {}

for i, row in df.iterrows():
ncell = i + 1
Expand All @@ -435,9 +463,16 @@ def to_table(self, subset=None, tooltip=None, n_cols=6,
else:
func = style.get(col)
if func:
item = f'<div class="data data-{col}" style="{func(v)}">{v}</div>'
item = f'<div class="data data-{col}" style="{func(v)}">'
else:
item = f'<div class="data data-{col}">{v}</div>'
item = f'<div class="data data-{col}">'
fmter = fmt.get(col)
if fmter:
try:
v = fmter(v)
except TypeError:
v = fmter.format(v)
item += f'{v}</div>'
div.append(item)
div.append("</div>")
td.append("\n".join(div))
Expand Down

0 comments on commit 0083e63

Please sign in to comment.