Skip to main content

hugegraph-python-client

Python client for Apache HugeGraph, distributed as hugegraph-python and imported as pyhugegraph.

It is used to define graph structures, perform CRUD operations on graph data, manage schemas, and execute Gremlin queries. Both the hugegraph-llm and hugegraph-ml modules depend on this foundational library.

Installation

Install the released package (Stable)

To install the released hugegraph-python distribution (Python 3.10+):

# uv is optional, you can use pip directly
uv pip install hugegraph-python

Install from Source (Latest Code)

To install from the source, clone the repository and install the required dependencies:

git clone https://github.com/apache/hugegraph-ai.git
cd hugegraph-ai

# Use uv sync to install dependencies (workspace member)
uv sync --extra python-client  # Installs hugegraph-python from hugegraph-python-client/
source .venv/bin/activate  # Activate once - all commands below assume this environment

Usage

Defining Graph Structures

You can use the hugegraph-python-client to define graph structures. Below is an example of how to define a graph:

from pyhugegraph.client import PyHugeClient

# Initialize the client
# For HugeGraph API version ≥ v3: (Or enable graphspace function)
# - The 'graphspace' parameter becomes relevant if graphspaces are enabled.(default name is 'DEFAULT')
# - Otherwise, the graphspace parameter is optional and can be ignored.
client = PyHugeClient("127.0.0.1", "8080", user="admin", pwd="admin", graph="hugegraph", graphspace="DEFAULT")

""""
Note:
Could refer to the official REST-API doc of your HugeGraph version for accurate details.
If some API is not as expected, please submit a issue or contact us.
"""
schema = client.schema()
schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("birthDate").asText().ifNotExist().create()
schema.vertexLabel("Person").properties("name", "birthDate").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()
schema.vertexLabel("Movie").properties("name").usePrimaryKeyId().primaryKeys("name").ifNotExist().create()
schema.edgeLabel("ActedIn").sourceLabel("Person").targetLabel("Movie").ifNotExist().create()

print(schema.getVertexLabels())
print(schema.getEdgeLabels())
print(schema.getRelations())

# Init Graph
g = client.graph()
v_al_pacino = g.addVertex("Person", {"name": "Al Pacino", "birthDate": "1940-04-25"})
v_robert = g.addVertex("Person", {"name": "Robert De Niro", "birthDate": "1943-08-17"})
v_godfather = g.addVertex("Movie", {"name": "The Godfather"})
v_godfather2 = g.addVertex("Movie", {"name": "The Godfather Part II"})
v_godfather3 = g.addVertex("Movie", {"name": "The Godfather Coda The Death of Michael Corleone"})

g.addEdge("ActedIn", v_al_pacino.id, v_godfather.id, {})
g.addEdge("ActedIn", v_al_pacino.id, v_godfather2.id, {})
g.addEdge("ActedIn", v_al_pacino.id, v_godfather3.id, {})
g.addEdge("ActedIn", v_robert.id, v_godfather2.id, {})

res = g.getVertexById(v_al_pacino.id).label
print(res)
g.close()

Schema Management

The hugegraph-python-client provides comprehensive schema management capabilities.

Define Property Keys

# Define a property key
client.schema().propertyKey("name").dataType("STRING").cardinality("SINGLE").create()

Define Vertex Labels

# Define a vertex label
client.schema().vertexLabel("person").properties("name", "age").primaryKeys("name").create()

Define Edge Labels

# Define an edge label
client.schema().edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("since").create()

Define Index Labels

# Define an index label
client.schema().indexLabel("personByName").onV("person").by("name").secondary().create()

CRUD Operations

The client allows you to perform CRUD operations on the graph data. Below are examples of how to create, read, update, and delete vertices and edges:

Create Vertices and Edges

# Create vertices
v1 = client.graph().addVertex("person").property("name", "John").property("age", 29).create()
v2 = client.graph().addVertex("person").property("name", "Jane").property("age", 25).create()

# Create an edge
client.graph().addEdge(v1, "knows", v2).property("since", "2020").create()

Read Vertices and Edges

# Get a vertex by ID
vertex = client.graph().getVertexById(v1.id)
print(vertex)

# Get an edge by ID
edge = client.graph().getEdgeById(edge.id)
print(edge)

Update Vertices and Edges

# Update a vertex
client.graph().updateVertex(v1.id).property("age", 30).update()

# Update an edge
client.graph().updateEdge(edge.id).property("since", "2021").update()

Delete Vertices and Edges

# Delete a vertex
client.graph().deleteVertex(v1.id)

# Delete an edge
client.graph().deleteEdge(edge.id)

Execute Gremlin Queries

The client also supports executing Gremlin queries:

# Execute a Gremlin query
g = client.gremlin()
res = g.exec("g.V().limit(5)")
print(res)

Other info is under 🚧 (Welcome to add more docs for it, users could refer java-client-doc for similar usage)

Contributing

  • Welcome to contribute to hugegraph-python-client. Please see the Guidelines for more information.
  • Code format: Please run ./style/code_format_and_analysis.sh to format your code before submitting a PR.

Thank you to all the people who already contributed to hugegraph-python-client!

Contact Us

Metadata

Release files for hugegraph-python 1.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hugegraph-python 1.7.0
File Size Uploaded
hugegraph_python-1.7.0.tar.gz 29.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hugegraph-python 1.7.0
File Interpreter ABI Platform
hugegraph_python-1.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 92.1 kB

Release files / hugegraph_python-1.7.0.tar.gz

Download URL hugegraph_python-1.7.0.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1b3f44c0f30bd97a2036378bcfcb3b9663357742248b38032c4b5fb3239a1bc5
BLAKE2b-256 checksum
How to use checksums
09bce31031ed4c614a218ca3b5cd70a527ddf92d2215a15290ac85c44319aad2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / hugegraph_python-1.7.0-py3-none-any.whl

Download URL hugegraph_python-1.7.0-py3-none-any.whl
Size 62.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98330cc35f1af7510af9737fc013b1fc4146973d0f885d31207f36d01f6d1b3a
BLAKE2b-256 checksum
How to use checksums
df51332c82a94f860771cbe7a7a71f0deafa3e7e68f57f09425005579702b74f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

1.7.1

2 release files

This release

1.7.0 This release

2 release files

1.5.0

2 release files

1.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page