Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion langextract/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _is_jupyter() -> bool:

_VISUALIZATION_CSS = textwrap.dedent("""\
<style>
.lx-highlight { position: relative; border-radius:3px; padding:1px 2px;}
.lx-highlight { position: relative; border-radius:3px; padding:1px 2px; cursor: pointer;}
.lx-highlight .lx-tooltip {
visibility: hidden;
opacity: 0;
Expand Down Expand Up @@ -544,6 +544,13 @@ def _extraction_sort_key(extraction):
window.prevExtraction = prevExtraction;
window.jumpToExtraction = jumpToExtraction;

document.getElementById('textWindow').addEventListener('click', (event) => {{
const highlight = event.target.closest('.lx-highlight');
if (highlight && highlight.dataset.idx !== undefined) {{
jumpToExtraction(highlight.dataset.idx);
}}
}});

updateDisplay();
}})();
</script>""")
Expand Down
22 changes: 22 additions & 0 deletions tests/visualization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ def test_visualize_basic_document_renders_correctly(self):
for component in expected_components:
self.assertIn(component, actual_html)

@mock.patch.object(
visualization, "HTML", new=None
) # Ensures visualize returns str
def test_visualize_highlights_are_clickable(self):

doc = data.AnnotatedDocument(
text="Patient needs Aspirin.",
extractions=[
data.Extraction(
extraction_class="MEDICATION",
extraction_text="Aspirin",
char_interval=data.CharInterval(start_pos=14, end_pos=21),
)
],
)

actual_html = visualization.visualize(doc)

self.assertIn("cursor: pointer", actual_html)
self.assertIn("addEventListener('click'", actual_html)
self.assertIn("closest('.lx-highlight')", actual_html)

@mock.patch.object(
visualization, "HTML", new=None
) # Ensures visualize returns str
Expand Down
Loading