|
| 1 | +# Graph Visualization for Python by Neo4j |
| 2 | + |
| 3 | +[](https://pypi.org/project/neo4j-viz/) |
| 4 | +[](https://pypi.org/project/neo4j-viz/) |
| 5 | + |
| 6 | +[](https://neo4j.com/docs/nvl-python/preview/) |
| 7 | +[](https://discord.gg/neo4j) |
| 8 | +[](https://community.neo4j.com) |
| 9 | +[](https://pypi.org/project/neo4j-viz/) |
| 10 | + |
| 11 | +`neo4j-viz` is a Python package for creating interactive graph visualizations based on data from Neo4j products. |
| 12 | + |
| 13 | +The output is of type `IPython.display.HTML` and can be viewed directly in a Jupyter Notebook, Streamlit. |
| 14 | +Alternatively, you can export the output to a file and view it in a web browser. |
| 15 | + |
| 16 | +The package wraps the [Neo4j Visualization JavaScript library (NVL)](https://neo4j.com/docs/nvl/current/). |
| 17 | + |
| 18 | +Proper documentation is forthcoming. |
| 19 | + |
| 20 | +> [!WARNING] |
| 21 | +> This package is still in development and the API is subject to change. |
| 22 | +
|
| 23 | + |
| 24 | + |
| 25 | +## Some notable features |
| 26 | + |
| 27 | +- Easy to import graphs represented as: |
| 28 | + - projections in the Neo4j Graph Data Science (GDS) library |
| 29 | + - pandas DataFrames |
| 30 | +- Node features: |
| 31 | + - Sizing |
| 32 | + - Colors |
| 33 | + - Captions |
| 34 | + - Pinning |
| 35 | +- Relationship features: |
| 36 | + - Colors |
| 37 | + - Captions |
| 38 | +- Graph features: |
| 39 | + - Zooming |
| 40 | + - Panning |
| 41 | + - Moving nodes |
| 42 | + - Using different layouts |
| 43 | +- Additional convenience functionality for: |
| 44 | + - Resizing nodes, optionally including scale normalization |
| 45 | + - Coloring nodes based on a property |
| 46 | + - Toggle whether nodes should be pinned or not |
| 47 | + |
| 48 | +Please note that this list is by no means exhaustive. |
| 49 | + |
| 50 | +## Getting started |
| 51 | + |
| 52 | +### Installation |
| 53 | + |
| 54 | +Simply install with pip: |
| 55 | + |
| 56 | +```sh |
| 57 | +pip install neo4j-viz |
| 58 | +``` |
| 59 | + |
| 60 | +### Basic usage |
| 61 | + |
| 62 | +We will use a small toy graph representing the purchase history of a few people and products. |
| 63 | + |
| 64 | +We start by instantiating the [Nodes](https://neo4j.com/docs/nvl-python/preview/api-reference/node.html) and |
| 65 | +[Relationships](https://neo4j.com/docs/nvl-python/preview/api-reference/relationship.html) we want in our graph. |
| 66 | +The only mandatory fields for a node are the "id", and "source" and "target" for a relationship. |
| 67 | +But the other fields can optionally be used to customize the appearance of the nodes and relationships in the |
| 68 | +visualization. |
| 69 | + |
| 70 | +Lastly we create a |
| 71 | +[VisualizationGraph](https://neo4j.com/docs/nvl-python/preview/api-reference/visualization-graph.html) object with the |
| 72 | +nodes and relationships we created, and call its `render` method to display the graph. |
| 73 | + |
| 74 | +```python |
| 75 | +from neo4j_viz import Node, Relationship, VisualizationGraph |
| 76 | + |
| 77 | +nodes = [ |
| 78 | + Node(id=0, size=10, caption="Person"), |
| 79 | + Node(id=1, size=10, caption="Product"), |
| 80 | + Node(id=2, size=20, caption="Product"), |
| 81 | + Node(id=3, size=10, caption="Person"), |
| 82 | + Node(id=4, size=10, caption="Product"), |
| 83 | +] |
| 84 | +relationships = [ |
| 85 | + Relationship( |
| 86 | + source=0, |
| 87 | + target=1, |
| 88 | + caption="BUYS", |
| 89 | + ), |
| 90 | + Relationship( |
| 91 | + source=0, |
| 92 | + target=2, |
| 93 | + caption="BUYS", |
| 94 | + ), |
| 95 | + Relationship( |
| 96 | + source=3, |
| 97 | + target=2, |
| 98 | + caption="BUYS", |
| 99 | + ), |
| 100 | +] |
| 101 | + |
| 102 | +VG = VisualizationGraph(nodes=nodes, relationships=relationships) |
| 103 | + |
| 104 | +VG.render() |
| 105 | +``` |
| 106 | + |
| 107 | +This will return a `IPython.display.HTML` object that can be rendered in a Jupyter Notebook or streamlit application. |
| 108 | + |
| 109 | +### Examples |
| 110 | + |
| 111 | +For some Jupyter Notebook and streamlit examples, checkout the [/examples](/examples) directory. |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +If you would like to contribute to this project, please follow our [Contributor Guidelines](./CONTRIBUTING.md). |
0 commit comments