Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/niedbalski/slurpy
Python/Javascript framework for call python code from javascript and viceversa.
https://github.com/niedbalski/slurpy
Last synced: about 2 months ago
JSON representation
Python/Javascript framework for call python code from javascript and viceversa.
- Host: GitHub
- URL: https://github.com/niedbalski/slurpy
- Owner: niedbalski
- Created: 2012-10-02T18:52:07.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-02-27T19:27:35.000Z (almost 7 years ago)
- Last Synced: 2024-10-31T12:46:26.389Z (2 months ago)
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 23
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Slurpy
======Slurpy is an RPC mechanism to call python methods from javascript and viceversa.
Installation
------------Install the package using pip
$ pip install slurpy
Basic usage
-----------* Create a new Slurpy server instance.
With slurpy you can call javascript methods from the python function
using the slurpy.javascript object and using the python object from the
javascript side.```python
import slurpy
import osdef sum(a,b):
// is possible to call a javascript method and pass a python
// callback on result
slurpy.javascript.js_sum(1, 2, callback=on_js_sum_response)
return a + bdef on_js_sum_response(value):
print "The js_sum return = %d" % values = slurpy.Slurpy()
s.register(os)
s.register(sum)s.start()
```* Then integrate on your HTML document , defining the function to be called
by the server.
```javascript
//method invoked by the python code
var js_sum = function(a, b) {
return a + b;
}//wait for loaded event
python.on('loaded', function(evt) {
python.dirname('/etc/passwd', function(response) {
console.log("Directory name" + response);
});//module invoke
python.os.getenv("HOME", function(response) {
console.log("Home variable " + response);
});//module invoke
python.os.getuid(function(uid) {
console.log("Current UID " + uid);
});
//direct method invoke
python.sum(10, 1000, function(response) {
console.log("Sum result " + response);
});});
```
* Start the server
$ python your_slurpy_server.py
Writing Plugins:
================Starting from version 0.1.6 slurpy supports extension via Plugin.
* Available hooks: Work in progress.
* Example:
``` javascript
//callback invoked by the python code
var js_sum = function(a, b) {
return a + b;
}var plugin = new python.Plugin("Sniffer");
plugin.hooks = {
init: function() {
//does nothing
},on_execute: function(message) {
console.log("Before execution " + message);
return message;
}
}//plugin.disable()
plugin.activate();python.on('ready', function(evt) {
python.dirname('/etc/passwd', function(response) {
console.log("Directory name" + response);
});python.os.getenv("HOME", function(response) {
console.log("Home variable " + response);
});
python.os.getuid(function(uid) {
console.log("Current UID " + uid);
});python.sum(10, 1000, function(response) {
console.log("Sum result " + response);
});});
```