88import re
99
1010import pandas as pd
11+ import numpy as np
1112from dash import Input , Output , dcc , html , no_update
1213from jupyter_dash import JupyterDash
1314from pandas .core .groupby import DataFrameGroupBy
@@ -54,14 +55,14 @@ def find_grouping(
5455) -> tuple [DataFrameGroupBy , dict ]:
5556
5657 if fig .data [0 ].hovertemplate is not None :
57- col_names = re .findall (r"(.*?)=(?!%) .*?<.*?>" , fig .data [0 ].hovertemplate )
58+ col_names = re .findall (r"(.*?)=.*?<.*?>" , fig .data [0 ].hovertemplate )
5859 col_names = [re .sub (r"(.*)>" , "" , col_name ) for col_name in col_names ]
59- if set (col_names ) != set (cols ) :
60+ if set (cols ). issubset ( set (col_names )) is False :
6061 raise ValueError (
6162 f"marker_col/color_col/facet_col is misspecified because the specified dataframe grouping names { cols } don't match the names in the plotly figure { col_names } ." ,
6263 )
6364
64- df_grouped = df_data .groupby (col_names )
65+ df_grouped = df_data .groupby (cols )
6566
6667 str_groups = {}
6768 for name , group in df_grouped :
@@ -72,8 +73,20 @@ def find_grouping(
7273
7374 curve_dict = {}
7475 for index , data in enumerate (fig .data ):
75- curve_name = re .findall (r".*?=(?!%)( .*?)<.*?>" , data .hovertemplate )
76+ curve_name = re .findall (r".*?=(.*?)<.*?>" , data .hovertemplate )
7677 curve_name = ", " .join (str (x ) for x in curve_name )
78+ if "%{x}" in curve_name :
79+ unique_x_values = np .unique (data .x )
80+ if len (unique_x_values ) == 1 :
81+ curve_name = curve_name .replace ("%{x}" , str (unique_x_values [0 ]))
82+ else :
83+ curve_name = curve_name .replace (", %{x}" , "" )
84+ if "%{y}" in curve_name :
85+ unique_y_values = np .unique (data .y )
86+ if len (unique_y_values ) == 1 :
87+ curve_name = curve_name .replace ("%{y}" , str (unique_y_values [0 ]))
88+ else :
89+ curve_name = curve_name .replace (", %{y}" , "" )
7790 curve_dict [index ] = str_groups [curve_name ]
7891
7992 return df_grouped , curve_dict
@@ -215,15 +228,15 @@ def add_molecules(
215228
216229 if mol_col is not None and len (mol_col ) > 1 :
217230 menu = dcc .Dropdown (
218- options = [{"label" : x , "value " : x } for x in mol_col ],
231+ options = [{"label" : x , "smiles_value " : x } for x in mol_col ],
219232 value = mol_col [0 ],
220233 multi = True ,
221234 id = "smiles-menu" ,
222235 placeholder = "Select a mol column to display" ,
223236 )
224237 elif smiles_col is not None and len (smiles_col ) > 1 :
225238 menu = dcc .Dropdown (
226- options = [{"label" : x , "value " : x } for x in smiles_col ],
239+ options = [{"label" : x , "smiles_value " : x } for x in smiles_col ],
227240 value = smiles_col [0 ],
228241 multi = True ,
229242 id = "smiles-menu" ,
@@ -251,21 +264,24 @@ def add_molecules(
251264 Output ("graph-tooltip" , "bbox" ),
252265 Output ("graph-tooltip" , "children" ),
253266 ],
254- inputs = [Input ("graph-basic-2" , "hoverData" ), Input ("smiles-menu" , "value" )],
267+ inputs = [
268+ Input ("graph-basic-2" , "hoverData" ),
269+ Input ("smiles-menu" , "smiles_value" ),
270+ ],
255271 )
256- def display_hover (hoverData , value ):
272+ def display_hover (hoverData , smiles_value ):
257273 if hoverData is None :
258274 return False , no_update , no_update
259275
260- if value is None :
276+ if smiles_value is None :
261277 if mol_col is not None :
262- value = mol_col
278+ smiles_value = mol_col
263279 elif smiles_col is not None :
264- value = smiles_col
265- if isinstance (value , str ):
266- chosen_smiles = [value ]
280+ smiles_value = smiles_col
281+ if isinstance (smiles_value , str ):
282+ chosen_smiles = [smiles_value ]
267283 else :
268- chosen_smiles = value
284+ chosen_smiles = smiles_value
269285
270286 pt = hoverData ["points" ][0 ]
271287 bbox = pt ["bbox" ]
@@ -330,7 +346,7 @@ def display_hover(hoverData, value):
330346 )
331347
332348 if title_col is not None :
333- title = df_row [title_col ]. astype ( str )
349+ title = str ( df_row [title_col ])
334350 if title_col in caption_transform :
335351 title = caption_transform [title_col ](title )
336352
0 commit comments