Uploaded image for project: 'TinkerPop'
  1. TinkerPop
  2. TINKERPOP-1328

Provide [gremlin-python] as an code executor in docs

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.2.0-incubating
    • 3.2.1
    • documentation
    • None

    Description

      We currently support [gremlin-groovy] and [gremlin-groovy,modern]-style code block annotations. It would be good to also support [gremlin-python] and [gremlin-python,modern]. Likewise, ensure a generalization for the future when gremlin-ruby and gremlin-php are added.

      Gremlin-Python source can be evaluated using Groovy and the Jython ScriptEngine. Suppose the following code block:

      [gremlin-python,modern]
      g.V().out().name
      

      GROOVY

      First, assume the following startup code that should be evaluated once and only once into the Gremlin-Groovy script engine.

      import javax.script.ScriptEngineManager
      import javax.script.SimpleBindings
      loader = new URLClassLoader(new File("/Applications/jython-2.7.0/jython.jar").toURI().toURL()) // JYTHON_PATH is better
      jython = new ScriptEngineManager(loader).getEngineByName("jython")
      jython.eval("import sys");
      jython.eval("sys.path.append('/Users/marko/software/tinkerpop/gremlin-variant/src/main/jython/gremlin_python')"); // PYTHON_PATH is better
      jython.eval("from gremlin_python import *");
      
      jythonBindings = new SimpleBindings();
      jythonBindings.put("g", jython.eval("PythonGraphTraversalSource('g')"));
      jython.getContext().setBindings(jythonBindings, javax.script.ScriptContext.GLOBAL_SCOPE);
      

      The above requires that a global variable exist to where Jython is installed as well as to where Gremlin-Python is installed. Though, for the latter, you will simply be able to use pip to install dynamically (PYTHON_PATH) is set to the location of Gremlin-Python.

      From there, the code block above would be evaluated as:

      g = TinkerGraphFactory.createModern().traversal()
      Eval.me("g", g, jython.eval("g.V().out().name").toString())
      

      This looks as follow:

      gremlin> g = TinkerFactory.createModern().traversal()
      ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
      gremlin> Eval.me("g", g, jython.eval("g.V().out().name").toString())
      ==>lop
      ==>vadas
      ==>josh
      ==>ripple
      ==>lop
      ==>lop
      

      And thats that...

      Attachments

        Issue Links

          Activity

            People

              dkuppitz Daniel Kuppitz
              okram Marko A. Rodriguez
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: