{ "info": { "author": "Stefano Dissegna", "author_email": "", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: GNU General Public License v2 (GPLv2)", "Programming Language :: JavaScript", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development :: Interpreters" ], "description": "Introduction\r\n############\r\n\r\nPyduktape is a python wrapper around `Duktape `_,\r\nan embeddable Javascript interpreter.\r\n\r\nOn top of the interpreter wrapper, pyduktape offers easy integration\r\nbetween the Python and the Javascript environments. You can pass\r\nPython objects to Javascript, call methods on them and access their\r\nattributes. Similarly, you can pass Javascript objects to Python.\r\n\r\nObjects are never copied or serialized. Instead, they are passed\r\nbetween the two environments using proxy objects. Proxy objects\r\ndelegate the execution to the original object environment.\r\n\r\nThreading\r\n#########\r\n\r\nIt is possible to invoke Javascript code from multiple threads. Each\r\nthread will need to use its own embedded interpreter. Javascript\r\nobjects returned to the Python environment will only be usable on the\r\nsame thread that created them. The runtime always checks this\r\ncondition automatically, and raises a ``DuktapeThreadError`` if it's\r\nviolated.\r\n\r\nGetting Started\r\n###############\r\n\r\nInstallation\r\n------------\r\n\r\nTo install from pypi::\r\n\r\n $ pip install -U setuptools\r\n $ pip install pyduktape\r\n\r\nTo install the latest version from github::\r\n\r\n $ git clone https://github.com/stefano/pyduktape.git\r\n $ cd pyduktape\r\n $ pip install -U setuptools\r\n $ python setup.py install\r\n\r\nRunning Javascript code\r\n-----------------------\r\n\r\nTo run Javascript code, you need to create an execution context and\r\nuse the method ``eval_js``::\r\n\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.eval_js(\"print('Hello, world!');\")\r\n\r\nEach execution context starts its own interpreter. Each context is\r\nindependent, and tied to the Python thread that created it. Memory is\r\nautomatically managed.\r\n\r\nTo evaluate external Javascript files, use ``eval_js_file``::\r\n\r\n // helloWorld.js\r\n print('Hello, World!');\r\n\r\n # in the Python interpreter\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.eval_js_file('helloWorld.js')\r\n\r\nPyduktape supports Javascript modules::\r\n\r\n // js/helloWorld.js\r\n exports.sayHello = function () {\r\n print('Hello, World!');\r\n };\r\n\r\n // js/main.js\r\n var helloWorld = require('js/helloWorld');\r\n helloWorld.sayHello();\r\n\r\n # in the Python interpreter\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.eval_js_file('js/main')\r\n\r\nThe ``.js`` extension is automatically added if missing. Relative\r\npaths are relative to the current working directory, but you can\r\nchange the base path using ``set_base_path``::\r\n\r\n # js/helloWorld.js\r\n print('Hello, World!');\r\n\r\n # in the Python interpreter\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.set_base_path('js')\r\n context.eval_js_file('helloWorld')\r\n\r\nPython and Javascript integration\r\n---------------------------------\r\n\r\nYou can use ``set_globals`` to set Javascript global variables::\r\n\r\n import pyduktape\r\n\r\n def say_hello(to):\r\n print 'Hello, {}!'.format(to)\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.set_globals(sayHello=say_hello, world='World')\r\n context.eval_js(\"sayHello(world);\")\r\n\r\nYou can use ``get_global`` to access Javascript global variables::\r\n\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.eval_js(\"var helloWorld = 'Hello, World!';\")\r\n print context.get_global('helloWorld')\r\n\r\n``eval_js`` returns the value of the last expression::\r\n\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n hello_world = context.eval_js(\"var helloWorld = 'Hello, World!'; helloWorld\")\r\n print hello_world\r\n\r\nYou can seamlessly use Python objects and functions within Javascript\r\ncode. There are some limitations, though: any Python callable can\r\nonly be used as a function, and other attributes cannot be\r\naccessed. Primitive types (int, float, string, None) are converted to\r\nequivalent Javascript primitives. The following code shows how to\r\ninteract with a Python object from Javascript::\r\n\r\n import pyduktape\r\n\r\n class Hello(object):\r\n def __init__(self, what):\r\n self.what = what\r\n\r\n def say(self):\r\n print 'Hello, {}!'.format(self.what)\r\n\r\n context = pyduktape.DuktapeContext()\r\n context.set_globals(Hello=Hello)\r\n context.eval_js(\"var helloWorld = Hello('World'); helloWorld.say();\")\r\n\r\nIn the same way, you can use Javascript objects in Python. You can\r\nuse the special method `new` to instantiate an object::\r\n\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n Hello = context.eval_js(\"\"\"\r\n function Hello(what) {\r\n this.what = what;\r\n }\r\n\r\n Hello.prototype.say = function () {\r\n print('Hello, ' + this.what + '!');\r\n };\r\n\r\n Hello\r\n \"\"\")\r\n\r\n hello_world = Hello.new('World')\r\n hello_world.say()\r\n\r\nYou can use Python lists and dicts from Javascript, and viceversa::\r\n\r\n import pyduktape\r\n\r\n context = pyduktape.DuktapeContext()\r\n res = context.eval_js('[1, 2, 3]')\r\n\r\n for item in res:\r\n print item\r\n\r\n context.set_globals(lst=[4, 5, 6])\r\n context.eval_js('for (var i = 0; i < lst.length; i++) { print(lst[i]); }')\r\n\r\n res = context.eval_js('var x = {a: 1, b: 2}; x')\r\n for key, val in res.items():\r\n print key, '=', val\r\n res.c = 3\r\n context.eval_js('print(x.c);')\r\n\r\n context.set_globals(x=dict(a=1, b=2))\r\n context.eval_js(\"\"\"\r\n var items = x.items();\r\n for (var i = 0; i < items.length; i++) {\r\n print(items[i][0] + ' = ' + items[i][1]);\r\n }\r\n \"\"\")\r\n context.set_globals(x=dict(a=1, b=2))\r\n context.eval_js('for (var k in x) { print(k + ' = ' + x[k]); }')\r\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/stefano/pyduktape", "keywords": "javascript duktape embed", "license": "GPL", "maintainer": "", "maintainer_email": "", "name": "pyduktape", "package_url": "https://pypi.org/project/pyduktape/", "platform": "", "project_url": "https://pypi.org/project/pyduktape/", "project_urls": { "Homepage": "https://github.com/stefano/pyduktape" }, "release_url": "https://pypi.org/project/pyduktape/0.0.6/", "requires_dist": null, "requires_python": "", "summary": "Python integration for the Duktape Javascript interpreter", "version": "0.0.6" }, "last_serial": 3092111, "releases": { "0.0.1": [], "0.0.2": [ { "comment_text": "", "digests": { "md5": "955ed7e950c57324a95a816dca60e282", "sha256": "d74e0e4605396c668c19836012a94fb1c846b24f1820eee1fd18ee2e2b5a711f" }, "downloads": -1, "filename": "pyduktape-0.0.2.tar.gz", "has_sig": false, "md5_digest": "955ed7e950c57324a95a816dca60e282", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10129, "upload_time": "2015-11-15T12:28:13", "url": "https://files.pythonhosted.org/packages/25/31/b237fa9a7c251a7725dd3270644618663b2061e1ec728c9750b7a0cdb1ca/pyduktape-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "2d067f551006ab7b9f10840022590fb7", "sha256": "089c44411a68118a043bf3c1850bb8d816eceb54505fa028d851d31b00e7f3ea" }, "downloads": -1, "filename": "pyduktape-0.0.3.tar.gz", "has_sig": false, "md5_digest": "2d067f551006ab7b9f10840022590fb7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 611395, "upload_time": "2015-11-15T12:50:18", "url": "https://files.pythonhosted.org/packages/84/04/80a66f6895dd7a9d139b58c68395e6e6d331d997f24ce457ea064d1c0951/pyduktape-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "d2210a38429f3cef289ed42fad941a2b", "sha256": "6d554b313bc91fe67ef286a8f295f81c7aa48eb2230ea6e90c06f1917d684957" }, "downloads": -1, "filename": "pyduktape-0.0.4.tar.gz", "has_sig": false, "md5_digest": "d2210a38429f3cef289ed42fad941a2b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 822011, "upload_time": "2016-12-24T11:17:50", "url": "https://files.pythonhosted.org/packages/9e/d5/3e630b8a906907f2fd9dbdb99c6880a0e5e481150946cd33e5cb2af649bf/pyduktape-0.0.4.tar.gz" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "683bb10090d4547c55223b4dd19bd7bd", "sha256": "8c699b8d6fb5c327947f0fc8f9e42e672f36d299995c5dacdd7c4d846259b5ae" }, "downloads": -1, "filename": "pyduktape-0.0.5.tar.gz", "has_sig": false, "md5_digest": "683bb10090d4547c55223b4dd19bd7bd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 822011, "upload_time": "2017-01-15T19:47:00", "url": "https://files.pythonhosted.org/packages/25/e6/0ba3783ca003731ee2d932d7065ce8588b1c6fdd533e200d19c927fe8b3e/pyduktape-0.0.5.tar.gz" } ], "0.0.6": [ { "comment_text": "", "digests": { "md5": "30af618366c78f608ee511e0da281dc8", "sha256": "e1613d9f444456e4ede4e1a14e72d9352fddbe2080152ba9d5ce86304b2533bc" }, "downloads": -1, "filename": "pyduktape-0.0.6.tar.gz", "has_sig": false, "md5_digest": "30af618366c78f608ee511e0da281dc8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 825107, "upload_time": "2017-08-12T12:13:33", "url": "https://files.pythonhosted.org/packages/5e/5a/ebecf8b609ce02202a4f64e46212b24ba71fe46c853b6149731822a64f34/pyduktape-0.0.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "30af618366c78f608ee511e0da281dc8", "sha256": "e1613d9f444456e4ede4e1a14e72d9352fddbe2080152ba9d5ce86304b2533bc" }, "downloads": -1, "filename": "pyduktape-0.0.6.tar.gz", "has_sig": false, "md5_digest": "30af618366c78f608ee511e0da281dc8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 825107, "upload_time": "2017-08-12T12:13:33", "url": "https://files.pythonhosted.org/packages/5e/5a/ebecf8b609ce02202a4f64e46212b24ba71fe46c853b6149731822a64f34/pyduktape-0.0.6.tar.gz" } ] }