Skip to content

Commit f31be21

Browse files
Merge pull request #5 from robert-mcdermott/win-encoding-bug
Fixed Issue #4 (charmap) issue on Windows systems Fix Windows encoding issues in knowledge graph visualization ## Issue Windows users were experiencing UnicodeEncodeError when generating knowledge graphs with certain input files. The error occurred during HTML generation with the message: 'charmap' codec can't encode characters in position 263607-263621: character maps to <undefined> ## Root Cause The PyVis library was using the system's default encoding when writing HTML files. On Windows, this is typically 'cp1252' (Windows-1252), which has limited Unicode support compared to UTF-8 that's used on Mac/Linux systems. ## Fix Modified the [_save_and_modify_html](cci:1://file:///Users/rmcdermo/mycode/ai-knowledge-graph/src/knowledge_graph/visualization.py:324:0-362:44) function to: 1. Use PyVis's internal HTML generation capabilities instead of letting it write to a file 2. Directly access the generated HTML content in memory 3. Write the file ourselves with explicit UTF-8 encoding This bypasses the encoding issue completely by taking control of the file writing process with proper UTF-8 handling, ensuring consistent behavior across all operating systems. ## Testing Confirmed working on both Mac OS and Windows environments with the same input data.
2 parents 17fc7d7 + dd77eb8 commit f31be21

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "ai-knowledge-graph"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
description = "Takes a text document and generates an interactive knowledge graph"
55
authors = [{ name = "Robert McDermott", email = "[email protected]" }, ]
66
readme = "README.md"

src/knowledge_graph/visualization.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,15 @@ def _get_visualization_options(edge_smooth=False):
324324

325325
def _save_and_modify_html(net, output_file, community_count, all_nodes, triples):
326326
"""Save the network as HTML and modify with custom template."""
327-
# Save the network as HTML
328-
net.save_graph(output_file)
327+
# Instead of letting PyVis write to a file, we'll access its HTML directly
328+
# and write it ourselves with explicit UTF-8 encoding
329329

330-
# Read the generated HTML
331-
with open(output_file, 'r', encoding='utf-8') as f:
332-
html = f.read()
330+
# Generate the HTML content
331+
# This happens internally in PyVis without writing to a file
332+
net.generate_html()
333+
334+
# Get the HTML from PyVis's internal html attribute
335+
html = net.html
333336

334337
# Add our custom controls by replacing the div with our template
335338
html = html.replace('<div id="mynetwork" class="card-body"></div>', _load_html_template())
@@ -341,7 +344,7 @@ def _save_and_modify_html(net, output_file, community_count, all_nodes, triples)
341344
# Replace the other h1 with our enhanced title
342345
html = html.replace('<h1></h1>', f'<h1>Knowledge Graph - {len(all_nodes)} Nodes, {len(triples)} Relationships, {community_count} Communities</h1>')
343346

344-
# Write back the modified HTML
347+
# Write the HTML directly to the output file with explicit UTF-8 encoding
345348
with open(output_file, 'w', encoding='utf-8') as f:
346349
f.write(html)
347350

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)