Skip to content

Commit

Permalink
fix: Resolve the issue of HTML not opening properly due to excessivel…
Browse files Browse the repository at this point in the history
…y long JSON (#37)

Co-authored-by: gaozixiang <[email protected]>
  • Loading branch information
akai-shuuichi and gaozixiang authored Sep 14, 2024
1 parent 9755b5a commit 165920c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions examples/graphml_visualize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import networkx as nx
import json
import webbrowser
import os
import http.server
import socketserver
import threading
Expand All @@ -12,9 +11,9 @@ def graphml_to_json(graphml_file):
data = nx.node_link_data(G)
return json.dumps(data)


# 创建HTML文件
def create_html(json_data, html_path):
json_data = json_data.replace('\\"', '')
def create_html(html_path):
html_content = '''
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -94,8 +93,9 @@ def create_html(json_data, html_path):
<svg></svg>
<div class="tooltip"></div>
<div class="legend"></div>
<script type="text/javascript" src="./graph_json.js"></script>
<script>
const graphData = JSON.parse('{json_data}');
const graphData = graphJson;
const svg = d3.select("svg"),
width = window.innerWidth,
Expand Down Expand Up @@ -230,11 +230,18 @@ def create_html(json_data, html_path):
</script>
</body>
</html>
'''.replace("{json_data}", json_data.replace("'", "\\'").replace("\n", ""))
'''

with open(html_path, 'w', encoding='utf-8') as f:
f.write(html_content)


def create_json(json_data, json_path):
json_data = "var graphJson = " + json_data.replace('\\"', '').replace("'", "\\'").replace("\n", "")
with open(json_path, 'w', encoding='utf-8') as f:
f.write(json_data)


# 启动简单的HTTP服务器
def start_server():
handler = http.server.SimpleHTTPRequestHandler
Expand All @@ -245,8 +252,8 @@ def start_server():
# 主函数
def visualize_graphml(graphml_file, html_path):
json_data = graphml_to_json(graphml_file)
create_html(json_data, html_path)

create_json(json_data, 'graph_json.js')
create_html(html_path)
# 在后台启动服务器
server_thread = threading.Thread(target=start_server)
server_thread.daemon = True
Expand Down

0 comments on commit 165920c

Please sign in to comment.