Draw neo4j graphs with vis.js
Project description
:snake: neo4jvis
Installation
Neo4jvis requires Neo4j driver. You can install neo4jvis with:
pip install neo4jvis
Usage
Here's how you can use it: First import libraries, initialise the driver and create the graph container:
1. Import and init
from neo4j import GraphDatabase
from neo4jvis.model.styled_graph import StyledGraph
driver = GraphDatabase.driver(uri="bolt://url:port", auth=("my user", "my pass"))
graph = StyledGraph(driver)
2. Retrieve graph from neo4j
2.1 Retrieve whole graph
Here's how you can generate an html file with all the nodes and relationships of the database:
graph.generate_whole_graph()
graph.draw("output.html")
2.2 Generate graph from a query
Here's is how you can generate a graph from a given query provided by the developer:
QUERY = "MATCH p=()-[]->() RETURN p"
graph.add_from_query(QUERY)
graph.draw("output.html")
2.3 Generate graph from neomodel StructuredRel
Neomodel is a Object Graph Mapper for Neo4j database. Neo4jvis allows the retrieval of the network from a list of neomodel StructuredRel objects. Here's an example of the usage:
class Connected(StructuredRel):
pass
class Usuarios(StructuredNode):
id = StringProperty()
connected = RelationshipTo("Usuarios", "CONNECTED", model = Connected)
QUERY = """
MATCH (n1:Usuarios)-[r:CONNECTED]->()
RETURN r
"""
Z = neomodel.db.cypher_query(
QUERY,
resolve_objects=True)
relationships_list = [rel[0] for rel in Z[0]]
graph.add_from_neomodel_relationships(relationships_list)
graph.draw("output.html")
3. Style graph
3.1 Change nodes color
RED = "#ff0000"
WHITE = "#FFFFFF"
for node in list(graph.nodes.values()):
node["color"] = {
"border": WHITE,
"background": RED
}
3.2 Change nodes size
for node in list(graph.nodes.values()):
node["value"] = randrange(1, 10)
3.3. Change other node properties supported by vis.js
Neo4jvis supports all the options for nodes of vis.js.
Example usage:
node["shape"] = "triangle"
node["borderWidth"] = 10
node["label"] = "EXAMPLE"
node["font"] = {
"color": "#e8c2b0",
"size": 20,
}
3.4. Change edge properties
for edge in graph.edges:
edge["value"] = random.randrange(1, 10)
edge["color"] = {
"color": "#e8c2b0"
}
3.5. Change other edge properties
Similarly as with nodes, neo4jvis supports all the options for edges of vis.js.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for neo4jvis-0.0.0a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eaf770525ce24b3f51a3ea54ce47865d2a7d79d7b0785679ba255b973daa25a |
|
MD5 | b48da7a9541a99090193df15329a3c69 |
|
BLAKE2b-256 | 198d8d17ba1f61bc820657b5d84e088f999f33c128403c07ccc0d5a3825483d1 |