Note
Go to the end to download the full example code.
Graph
The node_graph.Graph object is a collection of tasks and links.
This example shows how to create a graph, add tasks and links,
and export it to a dictionary.
Create a graph
Create an empty graph.
from node_graph import Graph
g = Graph(name="my_first_nodegraph")
Add nodes
Add a float task and an add task.
float1 = g.add_task("node_graph.test_float", name="float1")
add1 = g.add_task("node_graph.test_add", name="add1")
Add link between nodes
Connect the output of float1 to the input of add1.
g.add_link(float1.outputs.result, add1.inputs.x)
g
Save to dict
Convert the graph to a Python dictionary representation.
d = g.to_dict()
print("Graph as dict:")
print(d)
Graph as dict:
{'platform_version': 'node_graph@0.6.5', 'uuid': '2ec544be-10e7-11f1-a699-e6e7c7bea291', 'name': 'my_first_nodegraph', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {'graph_type': 'NORMAL', 'graph_class': {'callable_name': 'Graph', 'module_path': 'node_graph.graph'}}, 'spec': {'schema_source': 'EMBEDDED', 'inputs': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1000000}}, 'outputs': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1}}, 'ctx': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1000000}}}, 'tasks': {'graph_inputs': {'identifier': 'graph_inputs', 'uuid': '2ec54aea-10e7-11f1-a699-e6e7c7bea291', 'name': 'graph_inputs', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {}, 'spec': {'schema_source': 'embedded', 'identifier': 'graph_inputs', 'task_type': 'Normal', 'catalog': 'Others', 'metadata': {}, 'base_class_path': 'node_graph.tasks.builtins.GraphLevelTask', 'inputs': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1000000}}}, 'properties': {}, 'inputs': {}, 'position': [30, 30], 'description': '', 'log': '', 'hash': '', 'input_socket_meta': {}, 'parent_task': [None]}, 'graph_outputs': {'identifier': 'graph_outputs', 'uuid': '2ec550d0-10e7-11f1-a699-e6e7c7bea291', 'name': 'graph_outputs', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {}, 'spec': {'schema_source': 'embedded', 'identifier': 'graph_outputs', 'task_type': 'Normal', 'catalog': 'Others', 'metadata': {}, 'base_class_path': 'node_graph.tasks.builtins.GraphLevelTask', 'inputs': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1}}}, 'properties': {}, 'inputs': {}, 'position': [30, 30], 'description': '', 'log': '', 'hash': '', 'input_socket_meta': {}, 'parent_task': [None]}, 'graph_ctx': {'identifier': 'graph_ctx', 'uuid': '2ec554e0-10e7-11f1-a699-e6e7c7bea291', 'name': 'graph_ctx', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {}, 'spec': {'schema_source': 'embedded', 'identifier': 'graph_ctx', 'task_type': 'Normal', 'catalog': 'Others', 'metadata': {}, 'base_class_path': 'node_graph.tasks.builtins.GraphLevelTask', 'inputs': {'identifier': 'node_graph.namespace', 'dynamic': True, 'meta': {'required': True, 'child_default_link_limit': 1000000}}}, 'properties': {}, 'inputs': {}, 'position': [30, 30], 'description': '', 'log': '', 'hash': '', 'input_socket_meta': {}, 'parent_task': [None]}, 'float1': {'identifier': 'node_graph.test_float', 'uuid': '2ec5de92-10e7-11f1-a699-e6e7c7bea291', 'name': 'float1', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {}, 'spec': {'schema_source': 'handle', 'identifier': 'node_graph.test_float', 'task_type': 'Function', 'catalog': 'Others', 'metadata': {'callable': {'module': 'node_graph.tasks.tests', 'qualname': 'test_float', 'callable_path': 'node_graph.tasks.tests.test_float', 'file_path': '/home/docs/checkouts/readthedocs.org/user_builds/node-graph/envs/latest/lib/python3.11/site-packages/node_graph/tasks/tests.py', 'package': 'node_graph', 'package_version': '0.6.5'}}, 'base_class_path': 'node_graph.tasks.function_task.FunctionTask', 'executor': {'mode': 'module', 'module_path': 'node_graph.tasks.tests', 'callable_name': 'test_float', 'callable_kind': None, 'graph_data': None, 'pickled_callable': None, 'source_code': '', 'metadata': None}, 'version': '0.6.5'}, 'properties': {}, 'inputs': {'t': 1}, 'position': [30, 30], 'description': '', 'log': '', 'hash': '', 'input_socket_meta': {}, 'parent_task': [None]}, 'add1': {'identifier': 'node_graph.test_add', 'uuid': '2ec5e9e6-10e7-11f1-a699-e6e7c7bea291', 'name': 'add1', 'state': 'CREATED', 'action': 'NONE', 'error': '', 'metadata': {}, 'spec': {'schema_source': 'handle', 'identifier': 'node_graph.test_add', 'task_type': 'Function', 'catalog': 'Others', 'metadata': {'callable': {'module': 'node_graph.tasks.tests', 'qualname': 'test_add', 'callable_path': 'node_graph.tasks.tests.test_add', 'file_path': '/home/docs/checkouts/readthedocs.org/user_builds/node-graph/envs/latest/lib/python3.11/site-packages/node_graph/tasks/tests.py', 'package': 'node_graph', 'package_version': '0.6.5'}}, 'base_class_path': 'node_graph.tasks.function_task.FunctionTask', 'executor': {'mode': 'module', 'module_path': 'node_graph.tasks.tests', 'callable_name': 'test_add', 'callable_kind': None, 'graph_data': None, 'pickled_callable': None, 'source_code': '', 'metadata': None}, 'version': '0.6.5'}, 'properties': {}, 'inputs': {'t': 1}, 'position': [30, 30], 'description': '', 'log': '', 'hash': '', 'input_socket_meta': {}, 'parent_task': [None]}}, 'links': [{'from_socket': 'result', 'from_task': 'float1', 'to_socket': 'x', 'to_task': 'add1'}], 'description': '', 'knowledge_graph': {'namespaces': {'qudt': 'http://qudt.org/schema/qudt/', 'qudt-unit': 'http://qudt.org/vocab/unit/', 'prov': 'http://www.w3.org/ns/prov#', 'schema': 'https://schema.org/'}, 'sockets': {}, 'triples': []}}
Total running time of the script: (0 minutes 0.007 seconds)