From 08085e94992dafb497f1fe4b076af495a0e2407e Mon Sep 17 00:00:00 2001 From: Leland McInnes Date: Fri, 16 Feb 2024 18:30:23 +0000 Subject: [PATCH] Better handling of extra_point_data --- datamapplot/interactive_rendering.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/datamapplot/interactive_rendering.py b/datamapplot/interactive_rendering.py index 148ca01..fa94605 100644 --- a/datamapplot/interactive_rendering.py +++ b/datamapplot/interactive_rendering.py @@ -761,9 +761,7 @@ def render_html( point_data = point_dataframe[["x", "y", "r", "g", "b", "a"]] if "hover_text" in point_dataframe.columns: - if extra_point_data is not None and ( - hover_text_html_template is not None or on_click is not None - ): + if extra_point_data is not None: hover_data = pd.concat( [point_dataframe[["hover_text"]], extra_point_data], axis=1, @@ -798,6 +796,22 @@ def render_html( if on_click is not None: on_click = '({index}, event) => ' + on_click.format_map(replacements) + elif extra_point_data is not None: + hover_data = extra_point_data + replacements = FormattingDict( + **{ + str(name): f"${{hoverData.data.{name}[index]}}" + for name in hover_data.columns + } + ) + if hover_text_html_template is not None: + get_tooltip = ( + '({index, picked}) => picked ? {"html": `' + + hover_text_html_template.format_map(replacements) + + "`} : null" + ) + else: + get_tooltip = "null" else: hover_data = pd.DataFrame(columns=("hover_text",)) get_tooltip = "null"