cookbook/ Python SDK Patterns
Last Updated: October 20, 2018Commented Python examples for managing TigerGraph using pyTigerGraph.
Python SDK Cookbook
Practical examples for integrating TigerGraph into Python applications using the pyTigerGraph library.
1. Connection & Authentication
Simple Connection
pythonterminalimport pyTigerGraph as tg # Initialize connection conn = tg.TigerGraphConnection( host="https://your-cluster.i.tgcloud.io", graphname="SocialGraph", username="tigergraph", password="password" ) # Fetch API Token (Required for most operations) conn.getToken(conn.createSecret()) print("Connected!")
2. Query Execution
Running an Installed Query
pythonterminal# Pass parameters as a dictionary params = { "source": "user_1", "dist": 2 } results = conn.runInstalledQuery("find_friends_at_distance", params) print(results[0]["Start"])
Running Ad-hoc GSQL
pythonterminal# Useful for administrative tasks gsql_cmd = "ls" response = conn.gsql(gsql_cmd) print(response)
3. Data Science & Machine Learning
Fetching Data for Pandas
pythonterminalimport pandas as pd # Extract vertices to a DataFrame users_df = conn.getVertices("User", select="name, age", limit=100) df = pd.DataFrame(users_df) print(df.head())
Neighborhood Sampling (GNN Pre-processing)
pythonterminal# Sample neighbors for a GNN input nodes = ["user_1", "user_2"] # Returns a subgraph centered around the target nodes subgraph = conn.getNeighborVertices( sourceVertices=nodes, edgeTypes=["FOLLOWS"], depth=2 )
4. Large Scale Upserts
Batch Upserting Data
pythonterminal# Efficiently insert thousands of edges edge_data = [ ("user_1", "user_2", {"connect_day": "2024-01-01"}), ("user_3", "user_4", {"connect_day": "2024-01-02"}) ] # upsertEdges automatically handles batching for performance conn.upsertEdges("User", "FOLLOWS", "User", edge_data)
5. Schema Management
Dynamically Adding a Vertex
pythonterminal# Check if vertex exists, then create if "NewType" not in conn.getVertexTypes(): conn.gsql("CREATE VERTEX NewType (PRIMARY_ID id STRING)") conn.gsql("ADD VERTEX NewType TO GRAPH SocialGraph")
[!NOTE] Always use
conn.getToken()after initializing your connection, as most REST++ endpoints require a valid bearer token for authorization.
On this page
- Python SDK Cookbook
- 1. Connection & Authentication
- Simple Connection
- Initialize connection
- Fetch API Token (Required for most operations)
- 2. Query Execution
- Running an Installed Query
- Pass parameters as a dictionary
- Running Ad-hoc GSQL
- Useful for administrative tasks
- 3. Data Science & Machine Learning
- Fetching Data for Pandas
- Extract vertices to a DataFrame
- Neighborhood Sampling (GNN Pre-processing)
- Sample neighbors for a GNN input
- Returns a subgraph centered around the target nodes
- 4. Large Scale Upserts
- Batch Upserting Data
- Efficiently insert thousands of edges
- upsertEdges automatically handles batching for performance
- 5. Schema Management
- Dynamically Adding a Vertex
- Check if vertex exists, then create
TigerGraph Book
v1.0 Curated