GithubHelp home page GithubHelp logo

olopez32 / slurpy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from niedbalski/slurpy

0.0 1.0 0.0 241 KB

Python/Javascript framework for call python code from javascript and viceversa.

HTML 14.66% Python 45.43% JavaScript 39.91%

slurpy's Introduction

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.
import slurpy
import os

def 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 + b

def on_js_sum_response(value):
    print "The js_sum return =  %d" % value

s = 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.
<html>
    <head>
       <script type="text/javascript" src="http://localhost:51711/slurpy/js"></script>
       <script type="text/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);
                });

            });

        </script>
    </head>
    <body>
    </body>
</html>
  • 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:
    <html>
    <head>
       <script type="text/javascript" src="http://localhost:51711/slurpy/js"></script>
       <script type="text/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);
                });

            });

        </script>
    </head>
    <body>
    </body>
</html>

slurpy's People

Contributors

niedbalski avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.