--
---
You received this message because you are subscribed to the Google Groups "Skulpt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skulpt+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
| <script type="text/javascript"> | |
| function outf(text) | |
| { | |
| var output = document.getElementById("output"); | |
| text = text.replace(/</g, '<'); | |
| output.innerHTML = output.innerHTML + text; | |
| } | |
| function run() | |
| { | |
| var prog = document.getElementById("code").value; | |
| var output = document.getElementById("output"); | |
| output.innerHTML = ''; | |
| Sk.configure({output:outf}); | |
| try | |
| { | |
| var module = Sk.importMainWithBody("<stdin>", false, prog); | |
| var Dog = Sk.ffi.buildClass(module, function($gbl, $loc) { | |
| $loc.__init__ = Sk.ffi.functionPy(function(selfPy) { | |
| Sk.ffi.referenceToPy({}, "Dog", undefined, selfPy); | |
| }); | |
| }, "Dog", []); | |
| var a = Sk.ffi.gattr(module, 'a'); | |
| var runMethod = Sk.ffi.gattr(a, 'run'); | |
| var retvalPy = Sk.ffi.callsim(runMethod, Sk.ffi.numberToIntPy(4)); | |
| alert(Sk.ffi.remapToJs(retvalPy)); | |
| var foo = Sk.ffi.gattr(module, 'foo'); | |
| Sk.ffi.callsim(foo, Sk.ffi.stringToPy("This string was passed to Python from JavaScript")); | |
| var bar = Sk.ffi.gattr(module, 'bar'); | |
| Sk.ffi.callsim(bar, Sk.ffi.remapToPy(function(argPy) { | |
| alert(Sk.ffi.remapToJs(argPy)); | |
| })); | |
|
| |
| var dogObject = Sk.ffi.callsim(Dog); | |
| Sk.ffi.callsim(foo, dogObject); | |
| } | |
| catch (e) | |
| { | |
| alert(e); | |
| } | |
| } | |
| </script> | |
| <form> | |
| <textarea id="code" rows="24" cols="80"> | |
| class Test: | |
| def run(self, b): | |
| self.a = 3 + b | |
| return self.a | |
| print "This string was defined and printed from Python" | |
| a = Test() | |
| def foo(arg): | |
| print type(arg) | |
| print arg | |
|
| |
| def bar(callback): | |
| callback("This string was generated in Python") | |
| def baz(): | |
| pass | |
| </textarea><br> | |
| <button onclick="run()" type="button">Run</button> | |
| </form> | |
| <pre id="output"></pre> |
Yes, you can do that. You define your function, then you define it as a property of the Sk.globals object. You then need to create a sort of "intermediate layer" to account for the way Skulpt handles it's objects and their values. Like this:Sk.globals.myFunction = function( pyName, pyJob ) {//Now, in Skulpt, string and number objects' values are stored as their 'value' property, so you have to access that.var name = pyName.value, job = pyJob.value;myFunction( name, job );}I'm actually working for an abstraction layer to make this simpler. It should be on my Github page fairly soon.
Thanks Ryan I'll try it that's exactly what I need
Cheers