Python SDK guide¶
Set up the Python SDK to connect to NebulaGraph Database Developer Edition and run basic queries.
For a complete workflow from installing dependencies to verifying an application endpoint, see Build a service with the Python SDK.
Prerequisites¶
- Python 3.10 or later is installed.
- A NebulaGraph Database Developer Edition service group is running. For more information, see Getting started.
Steps¶
-
Install the SDK with either of the following methods:
-
Install from PyPI:
pip install nebula5_python==5.3.0
-
Install from source code:
git clone -b dev https://github.com/vesoft-inc/nebula-python.git cd nebula-python pip install -e .
-
-
Use the SDK to connect to NebulaGraph Database Developer Edition and perform queries as follows:
from nebulagraph_python.client import NebulaClient # Create client client = NebulaClient( addresses="<host>:<port>", user_name="<username>", password="<password>", ) query = """ RETURN 1 AS a, 2 AS b """ # Execute query try: result = client.execute(query) finally: client.close()Replace
<host>:<port>,<username>, and<password>with the actual connection information. -
To inspect the query result, refer to the following methods:
-
Print the result in table style
result.print()Return:
╔═══╤═══╗ ║ │ ║ ║ a │ b ║ ║ │ ║ ╟───┼───╢ ║ │ ║ ║ 1 │ 2 ║ ║ │ ║ ╚═══╧═══╝ Summary ├── Rows: 1 └── Latency: 1450μs
-
Retrieve specific values from the query set
# Get one row row = result.one() # Get one value cell = row["a"].cast_primitive() # Print its value print(cell, type(cell))Return:
1 <class 'int'>
-
Retrieve primitive values from the result set
print(result.as_primitive_by_column()) print(list(result.as_primitive_by_row()))Return:
{'a': [1], 'b': [2]} [{'a': 1, 'b': 2}]
-
Retrieve a Pandas DataFrame from the query set
result = client.execute(query) df = result.as_pandas_df()Return:
a b 0 1 2
-