krlawrence / graph

Practical Gremlin - An Apache TinkerPop Tutorial
Apache License 2.0
836 stars 254 forks source link

Add a section showing how to talk to a Gremlin Server from the Python console #138

Open krlawrence opened 5 years ago

krlawrence commented 5 years ago

It is fairly straightforward to bootstrap an environment inside the Python console so that it can be used with a Gremlin Server. This has the advantage of being able to mix Gremlin and Python (and also use Python variables to manipulate results).

# Configure the environment so the Python Console can connect to
# a Gremlin Server using gremlin-python and a web socket connection.
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.traversal import 
import os
server = os.environ["GREMLIN_SERVER_NAME"]
port = os.environ["GREMLIN_SERVER_PORT"]

endpoint = 'ws://' + server + ':' + port + '/gremlin'
print(endpoint)

graph=Graph()
connection = DriverRemoteConnection(endpoint,'g')
g = graph.traversal().withRemote(connection)
fridex commented 5 years ago

Let me know if I can be somehow helpful here.

We are using JanusGraph with Python to analyze Python packages in the Python ecosystem. Here is an example notebook published (the repo it lives in has more notebooks) and here is our graph database adapter for using Gramlin queries and connecting to JanusGraph.