Spaces:
Running
Running
Update graph_generator_utils.py
Browse files- graph_generator_utils.py +94 -94
graph_generator_utils.py
CHANGED
|
@@ -1,92 +1,10 @@
|
|
| 1 |
-
import graphviz
|
| 2 |
-
|
| 3 |
-
def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list, current_depth: int, base_color: str):
|
| 4 |
-
"""
|
| 5 |
-
Recursively adds nodes and edges to a Graphviz Digraph object,
|
| 6 |
-
applying a color gradient and consistent styling.
|
| 7 |
-
|
| 8 |
-
Args:
|
| 9 |
-
dot (graphviz.Digraph): The Graphviz Digraph object to modify.
|
| 10 |
-
parent_id (str): The ID of the parent node for the current set of nodes.
|
| 11 |
-
nodes_list (list): A list of dictionaries, each representing a node
|
| 12 |
-
with 'id', 'label', 'relationship', and optional 'subnodes'.
|
| 13 |
-
current_depth (int): The current depth in the graph hierarchy (0 for central node).
|
| 14 |
-
base_color (str): The hexadecimal base color for the deepest nodes.
|
| 15 |
-
"""
|
| 16 |
-
# Calculate color for current depth, making it lighter
|
| 17 |
-
# This factor determines how quickly the color lightens per level.
|
| 18 |
-
lightening_factor = 0.12
|
| 19 |
-
|
| 20 |
-
# Convert base_color hex to RGB for interpolation
|
| 21 |
-
# Ensure base_color is a valid hex string before converting
|
| 22 |
-
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 23 |
-
base_color = '#19191a' # Fallback to default dark if invalid
|
| 24 |
-
|
| 25 |
-
base_r = int(base_color[1:3], 16)
|
| 26 |
-
base_g = int(base_color[3:5], 16)
|
| 27 |
-
base_b = int(base_color[5:7], 16)
|
| 28 |
-
|
| 29 |
-
# Calculate current node color by blending towards white
|
| 30 |
-
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 31 |
-
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 32 |
-
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 33 |
-
|
| 34 |
-
# Clamp values to 255 to stay within valid RGB range
|
| 35 |
-
current_r = min(255, current_r)
|
| 36 |
-
current_g = min(255, current_g)
|
| 37 |
-
current_b = min(255, current_b)
|
| 38 |
-
|
| 39 |
-
node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 40 |
-
|
| 41 |
-
# Font color: white for dark nodes, black for very light nodes for readability
|
| 42 |
-
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
| 43 |
-
|
| 44 |
-
# Edge colors and font sizes
|
| 45 |
-
edge_color = '#4a4a4a' # Dark gray for lines
|
| 46 |
-
# Font size adjusts based on depth, ensuring a minimum size
|
| 47 |
-
font_size = max(9, 14 - (current_depth * 2))
|
| 48 |
-
edge_font_size = max(7, 10 - (current_depth * 1))
|
| 49 |
-
|
| 50 |
-
for node in nodes_list:
|
| 51 |
-
node_id = node.get('id')
|
| 52 |
-
label = node.get('label')
|
| 53 |
-
relationship = node.get('relationship')
|
| 54 |
-
|
| 55 |
-
# Basic validation for node data
|
| 56 |
-
if not all([node_id, label, relationship]):
|
| 57 |
-
raise ValueError(f"Invalid node: {node}")
|
| 58 |
-
|
| 59 |
-
# Add node with specified style
|
| 60 |
-
dot.node(
|
| 61 |
-
node_id,
|
| 62 |
-
label,
|
| 63 |
-
shape='box', # All nodes are rectangular
|
| 64 |
-
style='filled,rounded', # Filled and rounded corners
|
| 65 |
-
fillcolor=node_fill_color,
|
| 66 |
-
fontcolor=font_color,
|
| 67 |
-
fontsize=str(font_size)
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
# Add edge from parent to current node
|
| 71 |
-
dot.edge(
|
| 72 |
-
parent_id,
|
| 73 |
-
node_id,
|
| 74 |
-
label=relationship,
|
| 75 |
-
color=edge_color,
|
| 76 |
-
fontcolor=edge_color, # Edge label color also dark gray
|
| 77 |
-
fontsize=str(edge_font_size)
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# Recursively call for subnodes
|
| 81 |
-
if 'subnodes' in node:
|
| 82 |
-
add_nodes_and_edges(dot, node_id, node['subnodes'], current_depth + 1, base_color)
|
| 83 |
-
|
| 84 |
# import graphviz
|
| 85 |
|
| 86 |
# def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list, current_depth: int, base_color: str):
|
| 87 |
# """
|
| 88 |
# Recursively adds nodes and edges to a Graphviz Digraph object,
|
| 89 |
# applying a color gradient and consistent styling.
|
|
|
|
| 90 |
# Args:
|
| 91 |
# dot (graphviz.Digraph): The Graphviz Digraph object to modify.
|
| 92 |
# parent_id (str): The ID of the parent node for the current set of nodes.
|
|
@@ -95,55 +13,137 @@ def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list,
|
|
| 95 |
# current_depth (int): The current depth in the graph hierarchy (0 for central node).
|
| 96 |
# base_color (str): The hexadecimal base color for the deepest nodes.
|
| 97 |
# """
|
| 98 |
-
#
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
| 100 |
# if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 101 |
-
# base_color = '#
|
|
|
|
| 102 |
# base_r = int(base_color[1:3], 16)
|
| 103 |
# base_g = int(base_color[3:5], 16)
|
| 104 |
# base_b = int(base_color[5:7], 16)
|
| 105 |
-
|
|
|
|
| 106 |
# current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 107 |
# current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 108 |
# current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 109 |
-
|
|
|
|
| 110 |
# current_r = min(255, current_r)
|
| 111 |
# current_g = min(255, current_g)
|
| 112 |
# current_b = min(255, current_b)
|
| 113 |
|
| 114 |
# node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
#
|
|
|
|
|
|
|
| 118 |
# font_size = max(9, 14 - (current_depth * 2))
|
| 119 |
# edge_font_size = max(7, 10 - (current_depth * 1))
|
| 120 |
-
|
| 121 |
# for node in nodes_list:
|
| 122 |
# node_id = node.get('id')
|
| 123 |
# label = node.get('label')
|
| 124 |
# relationship = node.get('relationship')
|
| 125 |
|
|
|
|
| 126 |
# if not all([node_id, label, relationship]):
|
| 127 |
# raise ValueError(f"Invalid node: {node}")
|
| 128 |
|
|
|
|
| 129 |
# dot.node(
|
| 130 |
# node_id,
|
| 131 |
# label,
|
| 132 |
-
# shape='box',
|
| 133 |
-
# style='filled,rounded',
|
| 134 |
# fillcolor=node_fill_color,
|
| 135 |
# fontcolor=font_color,
|
| 136 |
# fontsize=str(font_size)
|
| 137 |
# )
|
| 138 |
|
|
|
|
| 139 |
# dot.edge(
|
| 140 |
# parent_id,
|
| 141 |
# node_id,
|
| 142 |
# label=relationship,
|
| 143 |
# color=edge_color,
|
| 144 |
-
# fontcolor=edge_color,
|
| 145 |
# fontsize=str(edge_font_size)
|
| 146 |
# )
|
| 147 |
|
|
|
|
| 148 |
# if 'subnodes' in node:
|
| 149 |
-
# add_nodes_and_edges(dot, node_id, node['subnodes'], current_depth + 1, base_color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# import graphviz
|
| 2 |
|
| 3 |
# def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list, current_depth: int, base_color: str):
|
| 4 |
# """
|
| 5 |
# Recursively adds nodes and edges to a Graphviz Digraph object,
|
| 6 |
# applying a color gradient and consistent styling.
|
| 7 |
+
|
| 8 |
# Args:
|
| 9 |
# dot (graphviz.Digraph): The Graphviz Digraph object to modify.
|
| 10 |
# parent_id (str): The ID of the parent node for the current set of nodes.
|
|
|
|
| 13 |
# current_depth (int): The current depth in the graph hierarchy (0 for central node).
|
| 14 |
# base_color (str): The hexadecimal base color for the deepest nodes.
|
| 15 |
# """
|
| 16 |
+
# # Calculate color for current depth, making it lighter
|
| 17 |
+
# # This factor determines how quickly the color lightens per level.
|
| 18 |
+
# lightening_factor = 0.12
|
| 19 |
|
| 20 |
+
# # Convert base_color hex to RGB for interpolation
|
| 21 |
+
# # Ensure base_color is a valid hex string before converting
|
| 22 |
# if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 23 |
+
# base_color = '#19191a' # Fallback to default dark if invalid
|
| 24 |
+
|
| 25 |
# base_r = int(base_color[1:3], 16)
|
| 26 |
# base_g = int(base_color[3:5], 16)
|
| 27 |
# base_b = int(base_color[5:7], 16)
|
| 28 |
+
|
| 29 |
+
# # Calculate current node color by blending towards white
|
| 30 |
# current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 31 |
# current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 32 |
# current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 33 |
+
|
| 34 |
+
# # Clamp values to 255 to stay within valid RGB range
|
| 35 |
# current_r = min(255, current_r)
|
| 36 |
# current_g = min(255, current_g)
|
| 37 |
# current_b = min(255, current_b)
|
| 38 |
|
| 39 |
# node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 40 |
+
|
| 41 |
+
# # Font color: white for dark nodes, black for very light nodes for readability
|
| 42 |
+
# font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
| 43 |
|
| 44 |
+
# # Edge colors and font sizes
|
| 45 |
+
# edge_color = '#4a4a4a' # Dark gray for lines
|
| 46 |
+
# # Font size adjusts based on depth, ensuring a minimum size
|
| 47 |
# font_size = max(9, 14 - (current_depth * 2))
|
| 48 |
# edge_font_size = max(7, 10 - (current_depth * 1))
|
| 49 |
+
|
| 50 |
# for node in nodes_list:
|
| 51 |
# node_id = node.get('id')
|
| 52 |
# label = node.get('label')
|
| 53 |
# relationship = node.get('relationship')
|
| 54 |
|
| 55 |
+
# # Basic validation for node data
|
| 56 |
# if not all([node_id, label, relationship]):
|
| 57 |
# raise ValueError(f"Invalid node: {node}")
|
| 58 |
|
| 59 |
+
# # Add node with specified style
|
| 60 |
# dot.node(
|
| 61 |
# node_id,
|
| 62 |
# label,
|
| 63 |
+
# shape='box', # All nodes are rectangular
|
| 64 |
+
# style='filled,rounded', # Filled and rounded corners
|
| 65 |
# fillcolor=node_fill_color,
|
| 66 |
# fontcolor=font_color,
|
| 67 |
# fontsize=str(font_size)
|
| 68 |
# )
|
| 69 |
|
| 70 |
+
# # Add edge from parent to current node
|
| 71 |
# dot.edge(
|
| 72 |
# parent_id,
|
| 73 |
# node_id,
|
| 74 |
# label=relationship,
|
| 75 |
# color=edge_color,
|
| 76 |
+
# fontcolor=edge_color, # Edge label color also dark gray
|
| 77 |
# fontsize=str(edge_font_size)
|
| 78 |
# )
|
| 79 |
|
| 80 |
+
# # Recursively call for subnodes
|
| 81 |
# if 'subnodes' in node:
|
| 82 |
+
# add_nodes_and_edges(dot, node_id, node['subnodes'], current_depth + 1, base_color)
|
| 83 |
+
|
| 84 |
+
import graphviz
|
| 85 |
+
|
| 86 |
+
def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list, current_depth: int, base_color: str):
|
| 87 |
+
"""
|
| 88 |
+
Recursively adds nodes and edges to a Graphviz Digraph object,
|
| 89 |
+
applying a color gradient and consistent styling.
|
| 90 |
+
Args:
|
| 91 |
+
dot (graphviz.Digraph): The Graphviz Digraph object to modify.
|
| 92 |
+
parent_id (str): The ID of the parent node for the current set of nodes.
|
| 93 |
+
nodes_list (list): A list of dictionaries, each representing a node
|
| 94 |
+
with 'id', 'label', 'relationship', and optional 'subnodes'.
|
| 95 |
+
current_depth (int): The current depth in the graph hierarchy (0 for central node).
|
| 96 |
+
base_color (str): The hexadecimal base color for the deepest nodes.
|
| 97 |
+
"""
|
| 98 |
+
lightening_factor = 0.06
|
| 99 |
+
|
| 100 |
+
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 101 |
+
base_color = '#BEBEBE'
|
| 102 |
+
base_r = int(base_color[1:3], 16)
|
| 103 |
+
base_g = int(base_color[3:5], 16)
|
| 104 |
+
base_b = int(base_color[5:7], 16)
|
| 105 |
+
|
| 106 |
+
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 107 |
+
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 108 |
+
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 109 |
+
|
| 110 |
+
current_r = min(255, current_r)
|
| 111 |
+
current_g = min(255, current_g)
|
| 112 |
+
current_b = min(255, current_b)
|
| 113 |
+
|
| 114 |
+
node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 115 |
+
font_color = 'black'
|
| 116 |
+
|
| 117 |
+
edge_color = '#4a4a4a'
|
| 118 |
+
font_size = max(9, 14 - (current_depth * 2))
|
| 119 |
+
edge_font_size = max(7, 10 - (current_depth * 1))
|
| 120 |
+
|
| 121 |
+
for node in nodes_list:
|
| 122 |
+
node_id = node.get('id')
|
| 123 |
+
label = node.get('label')
|
| 124 |
+
relationship = node.get('relationship')
|
| 125 |
+
|
| 126 |
+
if not all([node_id, label, relationship]):
|
| 127 |
+
raise ValueError(f"Invalid node: {node}")
|
| 128 |
+
|
| 129 |
+
dot.node(
|
| 130 |
+
node_id,
|
| 131 |
+
label,
|
| 132 |
+
shape='box',
|
| 133 |
+
style='filled,rounded',
|
| 134 |
+
fillcolor=node_fill_color,
|
| 135 |
+
fontcolor=font_color,
|
| 136 |
+
fontsize=str(font_size)
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
dot.edge(
|
| 140 |
+
parent_id,
|
| 141 |
+
node_id,
|
| 142 |
+
label=relationship,
|
| 143 |
+
color=edge_color,
|
| 144 |
+
fontcolor=edge_color,
|
| 145 |
+
fontsize=str(edge_font_size)
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if 'subnodes' in node:
|
| 149 |
+
add_nodes_and_edges(dot, node_id, node['subnodes'], current_depth + 1, base_color)
|