Skip to content

Commit f8b08e0

Browse files
committed
feat(append_node): append node to existing graph
1 parent 376f758 commit f8b08e0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

scrapegraphai/graphs/abstract_graph.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ def get_state(self, key=None) -> dict:
378378
return self.final_state[key]
379379
return self.final_state
380380

381+
def append_node(self, node):
382+
"""
383+
Add a node to the graph.
384+
385+
Args:
386+
node (BaseNode): The node to add to the graph.
387+
"""
388+
389+
self.graph.append_node(node)
390+
381391
def get_execution_info(self):
382392
"""
383393
Returns the execution information of the graph.

scrapegraphai/graphs/base_graph.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class BaseGraph:
4949
def __init__(self, nodes: list, edges: list, entry_point: str, use_burr: bool = False, burr_config: dict = None):
5050

5151
self.nodes = nodes
52+
self.raw_edges = edges
5253
self.edges = self._create_edges({e for e in edges})
5354
self.entry_point = entry_point.node_name
5455
self.initial_state = {}
@@ -168,4 +169,25 @@ def execute(self, initial_state: dict) -> Tuple[dict, list]:
168169
result = bridge.execute(initial_state)
169170
return (result["_state"], [])
170171
else:
171-
return self._execute_standard(initial_state)
172+
return self._execute_standard(initial_state)
173+
174+
def append_node(self, node):
175+
"""
176+
Adds a node to the graph.
177+
178+
Args:
179+
node (BaseNode): The node instance to add to the graph.
180+
"""
181+
182+
# if node name already exists in the graph, raise an exception
183+
if node.node_name in {n.node_name for n in self.nodes}:
184+
raise ValueError(f"Node with name '{node.node_name}' already exists in the graph. You can change it by setting the 'node_name' attribute.")
185+
186+
# get the last node in the list
187+
last_node = self.nodes[-1]
188+
# add the edge connecting the last node to the new node
189+
self.raw_edges.append((last_node, node))
190+
# add the node to the list of nodes
191+
self.nodes.append(node)
192+
# update the edges connecting the last node to the new node
193+
self.edges = self._create_edges({e for e in self.raw_edges})

0 commit comments

Comments
 (0)