# 🐷 PyG: PyInGraph
```txt
🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷
+---------+ +---------+ +---------+
| 🐍 A | --> | 🐍 B | --> | 🐍 C |
+---------+ +---------+ +---------+
| |
v v
+---------+ +---------+
| 🐍 D | <------------------ | 🐍 E |
+---------+ +---------+
🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷
```
A Python framework for creating computational graphs where each node represents an algorithm block. Perfect for integrating multiple algorithms into a visual, executable workflow.
## GUI support
2025-07-09: A litegraph-based graphical editor, [LiteGraph-PyG](https://github.com/wdsys/litegraph-PyG), is introduced to support the graph editing and running in users' local python environments. See [LiteGraph-PyG on GitHub](https://github.com/wdsys/litegraph-PyG)
## Dependencies
- Python >= 3.7
- numpy, matplotlib, networkx
- httpimport, requests (for remote modules)
## Quick Start
### Installation
From PyPI (recommended):
```bash
pip install pyingraph
```
From source:
```bash
cd PyInGraph
pip install -e .
```
This demo loads a simple graph that adds two numbers using custom algorithm blocks.
### Accessing Examples After Installation
After installing PyInGraph via pip, you can easily access and copy examples to your working directory:
```python
# Copy all examples to current directory
from pyingraph import copy_examples
copy_examples('.') # Creates 'pyingraph_examples' folder
```
Then try the demos:
```bash
cd pyingraph_examples
python demo_simple_add.py
```
Another demo for remote graph and modules:
```bash
python demo_control_system.py
```
Ohter utils in the examples are:
```python
# List all available examples
from pyingraph import list_examples
examples = list_examples()
for name, info in examples.items():
print(f"{name}: {info['description']}")
# Copy all examples to current directory
from pyingraph import copy_examples
copy_examples('.') # Creates 'pyingraph_examples' folder
# Copy a specific example
from pyingraph import copy_example
copy_example('demo_simple_add', '.') # Copies demo and dependencies
# Get examples location in installed package
from pyingraph import get_examples_path
print(f"Examples located at: {get_examples_path()}")
# Show usage help
from pyingraph import show_example_usage
show_example_usage()
```
> [!TIP]
> Use `copy_examples('.')` to get all examples in a `pyingraph_examples` folder, then navigate there to run the demos.
## Key Features
- **Easy Workflow Integration**: Algorithms, with inputs/outputs/internal-states, can be easily integrated via a graph structure. This graph-based approach, besides being code-efficient, enables a broad category of usages, such as system simulation, AI workflows, and network analysis (e.g., connectivity, condensation, etc.).
- **Local & Remote Modules**: Load algorithms from files or HTTP repositories
- **Built-in Visualization**: See your computational graph with NetworkX
- **Parameter Management**: Configure algorithm parameters via JSON
## How It Works
### An Example Project Structure
```
your_project/
├── local_modules/
│ ├── mod_source_constant.py
│ └── mod_sink_print.py
├── graph_example.json
└── run_graph.py
```
### 1. Create Algorithm Blocks
Each algorithm is a Python class that inherits from `BlockBase`:
#### Block 1 of 2: `mod_source_constant.py`
```python
from pyingraph import BlockBase
class ConstantSource(BlockBase):
"""
A constant source block that outputs a user-specified constant value.
"""
def __init__(self):
super().__init__()
self.attrNamesArr = ["value"] # Parameter name for the constant value
self.value = 0.0 # Default value
def read_inputs(self, inputs: list) -> None:
pass
def compute_outputs(self, time: float = None) -> list:
return [self.value]
def reset(self) -> None:
pass
```
#### Block 2 of 2: `mod_sink_print.py`
```python
from pyingraph import BlockBase
class SinkPrint(BlockBase):
"""
A print sink block that prints all inputs it receives.
This is useful for debugging and monitoring data flow in the graph.
"""
def __init__(self):
super().__init__()
self.attrNamesArr = [] # No parameters needed
def read_inputs(self, inputs: list) -> None:
self.inputs_received = inputs
def compute_outputs(self, time: float = None) -> list:
print(f"SinkPrint received: {self.inputs_received}")
return [] # Sink blocks typically don't produce outputs
def reset(self) -> None:
self.inputs_received = None
```
### 2. Define Your Graph
Create a JSON file `graph_example.json` describing your computational graph:
```json
{
"nodes": [
{
"id": "node1",
"name": "Constant Source 1",
"folder_url": "",
"folder_path": "local_modules",
"class_file": "mod_source_constant.py",
"class_name": "ConstantSource",
"parameters": {
"value": 1.0
}
},
{
"id": "node2",
"name": "Sink print",
"folder_url": "",
"folder_path": "local_modules",
"class_file": "mod_sink_print.py",
"class_name": "SinkPrint",
"parameters": {}
}
],
"edges": [
{
"source_node_id": "node1",
"target_node_id": "node2",
"source_port_idx": 0,
"target_port_idx": 0,
"properties": {}
}
]
}
```
### 3. Load and Run
Create a script `run_graph.py` to load and run your graph:
```python
from pyingraph import GraphLoader
loader = GraphLoader("graph_example.json", flag_remote=False)
loader.load()
nx_graph = loader.get_nx_graph()
loader.visualize_graph(block=True) # See your graph
loader.reset_graph_edges() # init the output data or graph edges
loader.simple_traverse_graph() # Execute the graph
```
Make sure the project structure and the graph json follows [the recommended structure](#an-example-project-structure), particularly the `folder_path` field in the graph json.
### JSON Schema Validation
PyInGraph includes a comprehensive JSON schema (`graph_schema.json`) to help you validate and understand the structure of graph configuration files. The schema provides:
- **Validation**: Ensure your JSON files are correctly formatted
- **IDE Support**: Get auto-completion and error checking in modern editors
- **Documentation**: Clear descriptions of all fields and their purposes
- **Examples**: Sample configurations for reference
## Examples Included
The `examples/` folder contains ready-to-run demos:
- **`demo_simple_add.py`**: Simple demo of a basic graph that adds two numbers, using local modules and graph
- **`demo_control_system.py`**: Control system simulation example, using remote modules and graph
- **`graph_simple_demo.json`**: Graph definition for the simple demo `demo_simple_add.py`
- **`local_modules/`**: Sample algorithm blocks:
- `mod_source_constant.py`: Constant value source
- `mod_summer.py`: Addition operation
- `mod_sink_print.py`: Output print as display
## Remote Module Support
Load algorithm blocks from HTTP repositories by setting `flag_remote=True`:
```python
loader = GraphLoader("http://example.com/graph.json", flag_remote=True)
```
By default, `flag_remote=False`
## Support
Questions? Contact: bobobone@qq.com
## License
MIT License - see LICENSE file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "pyingraph",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "graph, algorithm, workflow, computational-graph, networkx, simulation, ai-workflow, python",
"author": null,
"author_email": "bobobone <bobobone@qq.com>",
"download_url": "https://files.pythonhosted.org/packages/22/c4/304be738a7eb6bd83a478a347de09488ff058c6b540c90bb613b0052d05a/pyingraph-0.1.8.tar.gz",
"platform": null,
"description": "# \ud83d\udc37 PyG: PyInGraph \r\n\r\n\r\n```txt\r\n\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\r\n+---------+ +---------+ +---------+\r\n| \ud83d\udc0d A | --> | \ud83d\udc0d B | --> | \ud83d\udc0d C |\r\n+---------+ +---------+ +---------+\r\n | |\r\n v v\r\n+---------+ +---------+\r\n| \ud83d\udc0d D | <------------------ | \ud83d\udc0d E |\r\n+---------+ +---------+\r\n\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\ud83d\udc37\r\n```\r\n\r\nA Python framework for creating computational graphs where each node represents an algorithm block. Perfect for integrating multiple algorithms into a visual, executable workflow.\r\n\r\n## GUI support\r\n2025-07-09: A litegraph-based graphical editor, [LiteGraph-PyG](https://github.com/wdsys/litegraph-PyG), is introduced to support the graph editing and running in users' local python environments. See [LiteGraph-PyG on GitHub](https://github.com/wdsys/litegraph-PyG)\r\n\r\n## Dependencies\r\n\r\n- Python >= 3.7\r\n- numpy, matplotlib, networkx\r\n- httpimport, requests (for remote modules)\r\n\r\n\r\n## Quick Start\r\n\r\n### Installation\r\n\r\nFrom PyPI (recommended):\r\n```bash\r\npip install pyingraph\r\n```\r\n\r\nFrom source:\r\n```bash\r\ncd PyInGraph\r\npip install -e .\r\n```\r\n\r\nThis demo loads a simple graph that adds two numbers using custom algorithm blocks.\r\n\r\n### Accessing Examples After Installation\r\n\r\nAfter installing PyInGraph via pip, you can easily access and copy examples to your working directory:\r\n\r\n```python\r\n# Copy all examples to current directory\r\nfrom pyingraph import copy_examples\r\ncopy_examples('.') # Creates 'pyingraph_examples' folder\r\n```\r\nThen try the demos:\r\n```bash\r\ncd pyingraph_examples\r\npython demo_simple_add.py\r\n```\r\nAnother demo for remote graph and modules:\r\n```bash\r\npython demo_control_system.py\r\n```\r\n\r\nOhter utils in the examples are:\r\n\r\n```python\r\n# List all available examples\r\nfrom pyingraph import list_examples\r\nexamples = list_examples()\r\nfor name, info in examples.items():\r\n print(f\"{name}: {info['description']}\")\r\n\r\n# Copy all examples to current directory\r\nfrom pyingraph import copy_examples\r\ncopy_examples('.') # Creates 'pyingraph_examples' folder\r\n\r\n# Copy a specific example\r\nfrom pyingraph import copy_example\r\ncopy_example('demo_simple_add', '.') # Copies demo and dependencies\r\n\r\n# Get examples location in installed package\r\nfrom pyingraph import get_examples_path\r\nprint(f\"Examples located at: {get_examples_path()}\")\r\n\r\n# Show usage help\r\nfrom pyingraph import show_example_usage\r\nshow_example_usage()\r\n```\r\n\r\n> [!TIP]\r\n> Use `copy_examples('.')` to get all examples in a `pyingraph_examples` folder, then navigate there to run the demos.\r\n\r\n## Key Features\r\n\r\n- **Easy Workflow Integration**: Algorithms, with inputs/outputs/internal-states, can be easily integrated via a graph structure. This graph-based approach, besides being code-efficient, enables a broad category of usages, such as system simulation, AI workflows, and network analysis (e.g., connectivity, condensation, etc.).\r\n- **Local & Remote Modules**: Load algorithms from files or HTTP repositories\r\n- **Built-in Visualization**: See your computational graph with NetworkX\r\n- **Parameter Management**: Configure algorithm parameters via JSON\r\n\r\n## How It Works\r\n\r\n### An Example Project Structure\r\n```\r\nyour_project/\r\n\u251c\u2500\u2500 local_modules/\r\n\u2502 \u251c\u2500\u2500 mod_source_constant.py\r\n\u2502 \u2514\u2500\u2500 mod_sink_print.py\r\n\u251c\u2500\u2500 graph_example.json\r\n\u2514\u2500\u2500 run_graph.py\r\n```\r\n\r\n### 1. Create Algorithm Blocks\r\n\r\nEach algorithm is a Python class that inherits from `BlockBase`:\r\n\r\n#### Block 1 of 2: `mod_source_constant.py`\r\n\r\n```python\r\nfrom pyingraph import BlockBase\r\n\r\nclass ConstantSource(BlockBase):\r\n \"\"\"\r\n A constant source block that outputs a user-specified constant value.\r\n \"\"\"\r\n def __init__(self):\r\n super().__init__()\r\n self.attrNamesArr = [\"value\"] # Parameter name for the constant value\r\n self.value = 0.0 # Default value\r\n \r\n def read_inputs(self, inputs: list) -> None:\r\n pass\r\n \r\n def compute_outputs(self, time: float = None) -> list:\r\n return [self.value]\r\n \r\n def reset(self) -> None:\r\n pass\r\n```\r\n\r\n#### Block 2 of 2: `mod_sink_print.py`\r\n\r\n```python\r\nfrom pyingraph import BlockBase\r\n\r\nclass SinkPrint(BlockBase):\r\n \"\"\"\r\n A print sink block that prints all inputs it receives.\r\n This is useful for debugging and monitoring data flow in the graph.\r\n \"\"\"\r\n def __init__(self):\r\n super().__init__()\r\n self.attrNamesArr = [] # No parameters needed\r\n \r\n def read_inputs(self, inputs: list) -> None:\r\n self.inputs_received = inputs\r\n \r\n def compute_outputs(self, time: float = None) -> list:\r\n print(f\"SinkPrint received: {self.inputs_received}\")\r\n return [] # Sink blocks typically don't produce outputs\r\n \r\n def reset(self) -> None:\r\n self.inputs_received = None\r\n```\r\n\r\n\r\n### 2. Define Your Graph\r\n\r\nCreate a JSON file `graph_example.json` describing your computational graph:\r\n\r\n```json\r\n{\r\n \"nodes\": [\r\n {\r\n \"id\": \"node1\",\r\n \"name\": \"Constant Source 1\",\r\n \"folder_url\": \"\",\r\n \"folder_path\": \"local_modules\",\r\n \"class_file\": \"mod_source_constant.py\",\r\n \"class_name\": \"ConstantSource\",\r\n \"parameters\": {\r\n \"value\": 1.0\r\n }\r\n },\r\n {\r\n \"id\": \"node2\",\r\n \"name\": \"Sink print\",\r\n \"folder_url\": \"\",\r\n \"folder_path\": \"local_modules\",\r\n \"class_file\": \"mod_sink_print.py\",\r\n \"class_name\": \"SinkPrint\",\r\n \"parameters\": {}\r\n }\r\n ],\r\n \"edges\": [\r\n {\r\n \"source_node_id\": \"node1\",\r\n \"target_node_id\": \"node2\",\r\n \"source_port_idx\": 0,\r\n \"target_port_idx\": 0,\r\n \"properties\": {}\r\n }\r\n ]\r\n}\r\n```\r\n\r\n### 3. Load and Run\r\n\r\nCreate a script `run_graph.py` to load and run your graph:\r\n\r\n\r\n```python\r\nfrom pyingraph import GraphLoader\r\n\r\nloader = GraphLoader(\"graph_example.json\", flag_remote=False)\r\nloader.load()\r\nnx_graph = loader.get_nx_graph()\r\nloader.visualize_graph(block=True) # See your graph\r\nloader.reset_graph_edges() # init the output data or graph edges\r\nloader.simple_traverse_graph() # Execute the graph\r\n```\r\n\r\nMake sure the project structure and the graph json follows [the recommended structure](#an-example-project-structure), particularly the `folder_path` field in the graph json.\r\n\r\n### JSON Schema Validation\r\n\r\nPyInGraph includes a comprehensive JSON schema (`graph_schema.json`) to help you validate and understand the structure of graph configuration files. The schema provides:\r\n\r\n- **Validation**: Ensure your JSON files are correctly formatted\r\n- **IDE Support**: Get auto-completion and error checking in modern editors\r\n- **Documentation**: Clear descriptions of all fields and their purposes\r\n- **Examples**: Sample configurations for reference\r\n\r\n## Examples Included\r\n\r\nThe `examples/` folder contains ready-to-run demos:\r\n\r\n- **`demo_simple_add.py`**: Simple demo of a basic graph that adds two numbers, using local modules and graph\r\n- **`demo_control_system.py`**: Control system simulation example, using remote modules and graph\r\n- **`graph_simple_demo.json`**: Graph definition for the simple demo `demo_simple_add.py`\r\n- **`local_modules/`**: Sample algorithm blocks:\r\n - `mod_source_constant.py`: Constant value source\r\n - `mod_summer.py`: Addition operation\r\n - `mod_sink_print.py`: Output print as display\r\n\r\n## Remote Module Support\r\n\r\nLoad algorithm blocks from HTTP repositories by setting `flag_remote=True`:\r\n\r\n```python\r\nloader = GraphLoader(\"http://example.com/graph.json\", flag_remote=True)\r\n```\r\n\r\nBy default, `flag_remote=False`\r\n\r\n## Support\r\n\r\nQuestions? Contact: bobobone@qq.com\r\n\r\n## License\r\n\r\nMIT License - see LICENSE file for details.\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python-based framework for graphically integrating multiple Python algorithms",
"version": "0.1.8",
"project_urls": null,
"split_keywords": [
"graph",
" algorithm",
" workflow",
" computational-graph",
" networkx",
" simulation",
" ai-workflow",
" python"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "98fa67cfffa799514950ccebc9f57a7c07cc83b2e6c6abd562e044007e0684ad",
"md5": "b8f642ddf9c30badf90e25d5cb02cfc3",
"sha256": "6b873272808208c3330a44fea7d838a7a5bbc27d6e2cd3db2948e3ef05211c46"
},
"downloads": -1,
"filename": "pyingraph-0.1.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b8f642ddf9c30badf90e25d5cb02cfc3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 21264,
"upload_time": "2025-07-09T08:16:01",
"upload_time_iso_8601": "2025-07-09T08:16:01.525974Z",
"url": "https://files.pythonhosted.org/packages/98/fa/67cfffa799514950ccebc9f57a7c07cc83b2e6c6abd562e044007e0684ad/pyingraph-0.1.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "22c4304be738a7eb6bd83a478a347de09488ff058c6b540c90bb613b0052d05a",
"md5": "0a4095e5312b2adfca04c350d229964b",
"sha256": "86cef2ee5120319b6fdc6bf8707a81b790fc29cc424fdb915d9f9d4a77a1d7ba"
},
"downloads": -1,
"filename": "pyingraph-0.1.8.tar.gz",
"has_sig": false,
"md5_digest": "0a4095e5312b2adfca04c350d229964b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 20308,
"upload_time": "2025-07-09T08:16:02",
"upload_time_iso_8601": "2025-07-09T08:16:02.805583Z",
"url": "https://files.pythonhosted.org/packages/22/c4/304be738a7eb6bd83a478a347de09488ff058c6b540c90bb613b0052d05a/pyingraph-0.1.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-09 08:16:02",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pyingraph"
}