-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_graph_examples.py
More file actions
120 lines (99 loc) · 4.12 KB
/
generate_graph_examples.py
File metadata and controls
120 lines (99 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# generate_graph_examples.py
import importlib
import os
import sys
import networkx as nx
# --- Configuration ---
DOCS_DIR = "docs"
GENERATED_GRAPHS_ASSET_SUBDIR = os.path.join("assets", "generated_graphs")
OUTPUT_DIR = os.path.join(DOCS_DIR, GENERATED_GRAPHS_ASSET_SUBDIR)
EXAMPLES_DIR = "examples"
project_root = os.path.dirname(os.path.abspath(__file__))
examples_abs_path = os.path.join(project_root, EXAMPLES_DIR)
if examples_abs_path not in sys.path:
sys.path.insert(0, examples_abs_path)
os.makedirs(OUTPUT_DIR, exist_ok=True)
print(f"Ensured output directory exists: {os.path.abspath(OUTPUT_DIR)}")
# --- Helper Function ---
def generate_graph_from_example(
example_module_name: str,
graph_creation_func_name: str,
vis_options_var_name: str,
output_graph_filename: str,
html_page_title: str,
):
print(f"\n--- Generating: {html_page_title} ({output_graph_filename}) ---")
try:
from nx_vis_visualizer import nx_to_vis # Import here
example_module = importlib.import_module(example_module_name)
graph_creation_func = getattr(example_module, graph_creation_func_name)
vis_options_dict = getattr(example_module, vis_options_var_name)
G = graph_creation_func()
if not isinstance(G, nx.Graph): # nx.DiGraph is a subclass of nx.Graph
print(
f"Error: {graph_creation_func_name} from {example_module_name} did not produce a valid NetworkX graph."
)
return
output_filepath = os.path.join(OUTPUT_DIR, output_graph_filename)
nx_to_vis(
G,
output_filename=output_filepath,
html_title=html_page_title,
vis_options=vis_options_dict,
show_browser=False,
graph_width="100%",
graph_height="100%",
)
print(f"Successfully saved: {output_filepath}")
except ImportError as e:
print(
f"ImportError for module {example_module_name} or nx_vis_visualizer: {e}"
)
except AttributeError as e:
print(
f"AttributeError accessing function/variable in {example_module_name}: {e}"
)
except Exception as e:
print(f"An unexpected error occurred for {html_page_title}: {e}")
import traceback
traceback.print_exc()
# --- Main execution ---
if __name__ == "__main__":
print("Starting generation of graph examples from 'examples' directory...")
generate_graph_from_example(
example_module_name="cycle_example",
graph_creation_func_name="create_cycle_graph_data",
vis_options_var_name="cycle_graph_vis_options",
output_graph_filename="cycle_graph_example.html",
html_page_title="Interactive 5-Cycle Graph",
)
generate_graph_from_example(
example_module_name="karate_club_example",
graph_creation_func_name="create_karate_club_graph_data",
vis_options_var_name="karate_club_vis_options",
output_graph_filename="karate_club_example.html",
html_page_title="Zachary's Karate Club",
)
generate_graph_from_example(
example_module_name="simple_directed_example",
graph_creation_func_name="create_simple_directed_graph_data",
vis_options_var_name="simple_directed_vis_options",
output_graph_filename="simple_directed_example.html",
html_page_title="Simple Directed Graph",
)
generate_graph_from_example(
example_module_name="course_prerequisites_example",
graph_creation_func_name="create_course_prerequisites_graph_data",
vis_options_var_name="course_prereq_vis_options",
output_graph_filename="course_prerequisites_example.html",
html_page_title="Course Prerequisites",
)
generate_graph_from_example(
example_module_name="customization_example",
graph_creation_func_name="create_showcase_graph",
vis_options_var_name="custom_vis_options",
output_graph_filename="advanced_showcase_example.html",
html_page_title="Advanced Graph Showcase",
)
print("\nFinished generating graph examples.")
print(f"Check the output in: {os.path.abspath(OUTPUT_DIR)}")