Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ipython-gremlin 0.0.1 | ||
|
||
###A simple extension for the Gremlin Server based on [aiogremlin](https://pypi.python.org/pypi/aiogremlin/0.0.8). Works in both the IPython interpreter and the jupyter notebook. Requires Python 3.3 with Asyncio or Python 3.4. | ||
|
||
[Example notebook](https://github.com/davebshow/ipython-gremlin/blob/master/example.ipynb) | ||
|
||
|
||
Uses sessions to persist Gremlin across cells. | ||
|
||
Example use: | ||
|
||
```python | ||
>>> %load_ext gremlin | ||
>>> %gremlin 1 + 1 | ||
|
||
>>> %%gremlin | ||
... graph = TinkerFactory.createModern() | ||
... g = graph.traversal(standard()) | ||
... g.V().has('name','marko').out('knows').values('name') | ||
... | ||
['vadas', 'josh'] | ||
|
||
# store results in a variable like such: | ||
>>> nodes = %gremlin g.V() | ||
>>> nodes[0] | ||
{'id': 1, | ||
'label': 'person', | ||
'properties': {'age': [{'id': 1, 'properties': {}, 'value': 29}], | ||
'name': [{'id': 0, 'properties': {}, 'value': 'marko'}]}, | ||
'type': 'vertex'} | ||
|
||
# Change to a new session. | ||
>>> import uuid | ||
>>> new_session = str(uuid.uuid4()) | ||
>>> %session $new_session | ||
|
||
# g is not defined so now this will throw error. | ||
>>> %gremlin g.V() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
**alpha** | ||
|
||
|
||
`Official Documentation`_ | ||
|
||
|
||
.. _Official Documentation: https://github.com/davebshow/ipython-gremlin |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .magic import * | ||
__version__ = "0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import asyncio | ||
import uuid | ||
|
||
import aiogremlin | ||
|
||
# from IPython.config.configurable import Configurable | ||
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, | ||
needs_local_scope) | ||
# from IPython.utils.traitlets import Int, Float, Unicode, Bool | ||
|
||
|
||
@magics_class | ||
class GremlinMagic(Magics): | ||
"""Provides the Gremlin magic. This will be configurable etc. (eventually) | ||
""" | ||
def __init__(self, shell): | ||
super().__init__(shell) | ||
self._loop = asyncio.get_event_loop() | ||
self._pool = aiogremlin.WebSocketPool("ws://localhost:8182/", | ||
loop=self._loop) | ||
self._session = str(uuid.uuid4()) | ||
|
||
@needs_local_scope | ||
@line_magic("gremlin") | ||
@cell_magic("gremlin") | ||
def execute(self, line, cell="", local_ns={}): | ||
query = """{}\n{}""".format(line, cell) | ||
query = query.lstrip("\n") | ||
@asyncio.coroutine | ||
def run(): | ||
results = [] | ||
with (yield from self._pool) as conn: | ||
client = aiogremlin.GremlinClient(connection=conn, | ||
processor="session", | ||
loop=self._loop) | ||
resp = yield from client.submit(query, session=self._session) | ||
while True: | ||
mssg = yield from resp.stream.read() | ||
if mssg is None: | ||
break | ||
results += mssg.data | ||
return results | ||
return self._loop.run_until_complete(run()) | ||
|
||
@line_magic("session") | ||
def session(self, line): | ||
self._session = line | ||
print(self._session) | ||
|
||
|
||
def load_ipython_extension(ip): | ||
"""Load the extension in IPython.""" | ||
ip.register_magics(GremlinMagic) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from setuptools import setup | ||
|
||
|
||
setup( | ||
name="ipython-gremlin", | ||
version="0.0.1", | ||
license="MIT", | ||
author="davebshow", | ||
author_email="davebshow@gmail.com", | ||
url='https://github.com/davebshow/ipython-gremlin', | ||
description="Runs scripts agains the Gremlin Server from IPython", | ||
long_description=open("README.txt").read(), | ||
packages=["gremlin"], | ||
install_requires=[ | ||
"aiogremlin==0.0.8" | ||
], | ||
classifiers=[ | ||
'Development Status :: 3 - Alpha', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: MIT License', | ||
'Operating System :: OS Independent', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.4', | ||
'Programming Language :: Python :: 3 :: Only' | ||
] | ||
) |