Skip to content

Instantly share code, notes, and snippets.

@svdgraaf
Created September 24, 2010 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svdgraaf/595273 to your computer and use it in GitHub Desktop.
Save svdgraaf/595273 to your computer and use it in GitHub Desktop.
Embedding V8 in Python is awesome
# import PyV8, duh!
import PyV8
# define a class which methods should be available in Javascript
class epicClass(PyV8.JSClass):
def ponies(self):
return "Ponies!"
def doubletime(self, what, amount):
for i in range(0, amount):
print what
# define a bit of javascript, or fetch it from disk/database/POST, whatever
javascript = """awesome = ponies();
awesome = awesome + " and rainbows!";
doubletime(awesome, 2);"""
# create a javascript context
context = PyV8.JSContext(epicClass())
# enter the context, and run!
context.enter();
context.eval(javascript); # outputs "Ponies! and rainbows!" Twice... Amazing!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment