|
4 | 4 | import re
|
5 | 5 | import urllib.parse
|
6 | 6 | from fractions import Fraction
|
7 |
| -from typing import TYPE_CHECKING, Any, Callable |
| 7 | +from typing import TYPE_CHECKING, Any, Callable, Literal |
8 | 8 | from uuid import uuid4
|
9 | 9 |
|
10 | 10 | import dash
|
@@ -519,7 +519,7 @@ def hook_up_fig_with_struct_viewer(
|
519 | 519 | fig: go.Figure,
|
520 | 520 | df: pd.DataFrame,
|
521 | 521 | struct_col: str = "structure",
|
522 |
| - validate_id: Callable[[str], bool] = lambda id: True, |
| 522 | + transform_id: Callable[[str], str | Literal[False]] = lambda mat_id: mat_id, |
523 | 523 | highlight_selected: Callable[[dict[str, Any]], dict[str, Any]] | None = None,
|
524 | 524 | ) -> Dash:
|
525 | 525 | """Create a Dash app that hooks up a Plotly figure with a Crystal Toolkit structure
|
@@ -555,11 +555,10 @@ def hook_up_fig_with_struct_viewer(
|
555 | 555 | struct_col (str, optional): Name of the column in the data frame that contains
|
556 | 556 | the structures. Defaults to 'structure'. Can be instances of
|
557 | 557 | pymatgen.core.Structure or dicts created with Structure.as_dict().
|
558 |
| - validate_id (Callable[[str], bool], optional): Function that takes a string |
| 558 | + transform_id (Callable[[str], str | False], optional): Function that takes a string |
559 | 559 | extracted from the hovertext key of a hoverData event payload and returns
|
560 |
| - True if the string is a valid df row index. Defaults to lambda |
561 |
| - id: True. Useful for not running the update-structure |
562 |
| - callback on unexpected data. |
| 560 | + a string that can be used to index the dataframe. Return False to prevent |
| 561 | + the update-structure callback from being called. |
563 | 562 | highlight_selected (Callable[[dict[str, Any]], dict[str, Any]], optional):
|
564 | 563 | Function that takes the clicked or last-hovered point and returns a dict of
|
565 | 564 | kwargs to be passed to go.Figure.add_annotation() to highlight said point.
|
@@ -642,9 +641,8 @@ def update_structure(
|
642 | 641 |
|
643 | 642 | # hover_data and click_data are identical since a hover event always precedes a
|
644 | 643 | # click so we always use hover_data
|
645 |
| - material_id = hover_data["points"][0]["hovertext"] |
646 |
| - if not validate_id(material_id): |
647 |
| - print(f"bad {material_id=}") |
| 644 | + material_id = transform_id(hover_data["points"][0]["hovertext"]) |
| 645 | + if material_id is False: |
648 | 646 | raise dash.exceptions.PreventUpdate
|
649 | 647 |
|
650 | 648 | struct = df[struct_col][material_id]
|
|
0 commit comments