Skulpt

Skulptとは:
PythonをJavascriptからクライアントサイド(Webブラウザ)で使えるライブラリ。
iPadからでも動く!

Official:

Reference:



Call Python Function from Javascript


# Python Code
def test(a):
    print(a)

-----------------------------------------------------------------------------------

// JS Code
var module = Sk.importMainWithBody("<stdin>", false, editor.getValue(), true); //editor.getValue() == Python Code
var test = module.tp$getattr('test');
console.log(Sk.misceval.callsim(test, Sk.builtins.str("hoge"))); //call!



Call Javascript Function from Python


// JS Code
// target function
function myFunction(a, b) {
    console.log(a, b);
    return a + b;
}

// wrap function
Sk.builtins.myFunction = function (pyArg1, pyArg2) {
    //Now, in Skulpt, string, list, tuple, and number objects' values are stored as their 'v' property, so you have to access that.
    var a1 = pyArg1.v,
         a2 = pyArg2.v,
         res = myFunction(a1, a2);
    return new Sk.builtin.str(res);
};

-----------------------------------------------------------------------------------

# Python Code
res = myFunction("hoge", "piyo") #call!
print(res)   # hogepiyo

Create python class by javascript without a separate js file

// JS Code
var TestClass = function () {
    if (!(this instanceof Sk.builtin.TestClass)) {
        return new Sk.builtin.TestClass();
    }
    return this;
};
Sk.builtin.TestClass = TestClass;
Sk.abstr.setUpInheritance(name, TestClass, Sk.builtin.object);
Sk.builtin.TestClass.prototype.piyo = new Sk.builtin.func(function (self) {
    console.log("piyopiyo");
});

// before Sk.importMainWithBody(...)
Sk.builtins.TestClass = Sk.builtin.TestClass;

-----------------------------------------------------------------------------------

# Python Code
ins = TestClass()
print(dir(ins))    # ['__eq__', '__ge__', '__getattr__', '__gt__', '__hash__', '__le__', '__lt__', '__ne__', '__repr__', '__setattr__', '__str__', 'piyo']
ins.piyo()         # "piyopiyo"

Create python class by javascript with a separate js file
to wrap javasctipt class
// JSCode

// some_sculpt_initailize_file.js
Sk.externalLibraries = {
  "ummath" : {
    path: 'js/pyummath.js',
  }
};

// 
js/pyummath.js
$builtinmodule = function(name) {
  var mod = {},
    ummath = window.ummath;  // <= my javascript module

  vec3 = function ($gbl, $loc) {
    $loc.__init__ = new Sk.builtin.func(function (self, x, y, z) {
      if (x && y && z) {
        self.vec = new ummath.UMVec3d(x.v, y.v, z.v);
      } else {
        self.vec = new ummath.UMVec3d(0, 0, 0);
      }
    });

    $loc.__getitem__ = new Sk.builtin.func(function (self, key) {
      return Sk.builtin.float_(self.vec.xyz[key.v]);
    });

    $loc.__setitem__ = new Sk.builtin.func(function (self, key, value) {
      self.vec.xyz[key.v] = value.v;
    });

    $loc.dot = new Sk.builtin.func(function (self, src) {
      return Sk.builtin.float_(self.vec.dot(src.vec));
    });

    $loc.scale = new Sk.builtin.func(function (self, scale) {
      self.vec.scale(scale.v);
      return self;
    });

    $loc.cross = new Sk.builtin.func(function (self, src) {
      var dst = Sk.misceval.callsim(mod.vec3);  // <= calling python to create new instance
      dst.vec = self.vec.cross(src.vec);
      return dst;
    });

    $loc.add = new Sk.builtin.func(function (self, src) {
      var dst = Sk.misceval.callsim(mod.vec3);
      dst.vec = self.vec.add(src.vec);
      return dst;
    });

    $loc.sub = new Sk.builtin.func(function (self, src) {
      var dst = Sk.misceval.callsim(mod.vec3);
      dst.vec = self.vec.sub(src.vec);
      return dst;
    });
  };

  mod.vec3 = Sk.misceval.buildClass(mod, vec3, 'vec3', []);
  return mod;
};


-----------------------------------------------------------------------------------

# Python Code
import ummath
from ummath import *

u = vec3(1, 2, 3)
v = vec3(0, 1, 2)
a = u.cross(v)
b = u.add(v)
c = u.sub(v)
print(a[0], a[1], a[2])
print(b[0], b[1], b[2])
print(c[0], c[1], c[2])







Builtin

 float Sk.builtin.float_(value)
 int Sk.builtin.int_(value)
 str Sk.builtin.str(value)
  Sk.builtin.dict
  Sk.builtin.tuple


Module


TODO





Comments