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
6 changes: 4 additions & 2 deletions langextract/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def _is_jupyter() -> bool:
.lx-gif-optimized .lx-current-highlight { text-decoration-thickness: 4px; }
</style>""")

_HTML_CHARSET_META = '<meta charset="UTF-8">\n'


def _assign_colors(extractions: list[data.Extraction]) -> dict[str, str]:
"""Assigns a background colour to each extraction class.
Expand Down Expand Up @@ -600,7 +602,7 @@ def visualize(
'<div class="lx-animated-wrapper"><p>No valid extractions to'
' animate.</p></div>'
)
full_html = _VISUALIZATION_CSS + empty_html
full_html = _HTML_CHARSET_META + _VISUALIZATION_CSS + empty_html
if HTML is not None and _is_jupyter():
return HTML(full_html)
return full_html
Expand All @@ -615,7 +617,7 @@ def visualize(
show_legend,
)

full_html = _VISUALIZATION_CSS + visualization_html
full_html = _HTML_CHARSET_META + _VISUALIZATION_CSS + visualization_html

# Apply GIF optimizations if requested
if gif_optimized:
Expand Down
4 changes: 3 additions & 1 deletion tests/visualization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from langextract.core import data

_PALETTE = visualization._PALETTE
_HTML_CHARSET_META = visualization._HTML_CHARSET_META
_VISUALIZATION_CSS = visualization._VISUALIZATION_CSS


Expand Down Expand Up @@ -134,6 +135,7 @@ def test_visualize_basic_document_renders_correctly(self):

actual_html = visualization.visualize(doc)

self.assertTrue(actual_html.startswith(_HTML_CHARSET_META))
# Verify expected components appear in output
for component in expected_components:
self.assertIn(component, actual_html)
Expand All @@ -149,7 +151,7 @@ def test_visualize_no_extractions_renders_text_and_empty_legend(self):
" animate.</p></div>"
)
css_html = _VISUALIZATION_CSS
expected_html = css_html + body_html
expected_html = _HTML_CHARSET_META + css_html + body_html

actual_html = visualization.visualize(doc)

Expand Down
Loading