I am aware of the graph-notebook project that allows Gremlin queries to be submitted using magic commands. However, sometimes I need to code in Python and connect to the server using code, from within a regular Jupyter notebook cell. If, using the Gremlin Python 3.5.2 client I try to do something like this:
server = '<your server endpoint goes here>'
port = 8182
endpoint = f'wss://{server}:{port}/gremlin'
connection = DriverRemoteConnection(endpoint,'g')
g = traversal().withRemote(connection)
an error is thrown because the Jupyter event loop is already running.
Is there a way around this?
There is an additional parameter that can be specified while creating the Remote Connection that tells the Python Client to nest the event loops. You just need to create the connection along these lines:
from gremlin_python.driver.aiohttp.transport import AiohttpTransport
# Other imports not shown
server = '<your server endpoint goes here>'
port = 8182
endpoint = f'wss://{server}:{port}/gremlin'
print(endpoint)
connection = DriverRemoteConnection(endpoint,'g',
transport_factory=lambda:AiohttpTransport(call_from_event_loop=True))
g = traversal().withRemote(connection)
The key difference is that a custom transport_factory
is provided that is actually just a lambda
wrapper around the regular AiohttpTransport
, with the call_from_event_loop
parameter set to True
.
This extra configuration tells the Gremlin Python client to apply the appropriate internal changes to nest the event loops.