calling_from_js.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <script src="../dist/skulpt.min.js" type="text/javascript"></script>
  2. <script type="text/javascript">
  3. function outf(text)
  4. {
  5. var output = document.getElementById("output");
  6. text = text.replace(/</g, '&lt;');
  7. output.innerHTML = output.innerHTML + text;
  8. }
  9. function runit()
  10. {
  11. var prog = document.getElementById("code").value;
  12. var output = document.getElementById("output");
  13. output.innerHTML = '';
  14. Sk.configure({output:outf});
  15. try {
  16. var module = Sk.importMainWithBody("<stdin>", false, prog);
  17. var obj = module.tp$getattr('a');
  18. var runMethod = obj.tp$getattr('run');
  19. var ret = Sk.misceval.callsim(runMethod, 10);
  20. alert(ret.v);
  21. } catch (e) {
  22. alert(e);
  23. }
  24. }
  25. </script>
  26. <form>
  27. <textarea id="code" rows="24" cols="80">
  28. class Test:
  29. def run(self, b):
  30. self.a = 10 + b
  31. return self.a
  32. print "Hello World"
  33. a = Test()
  34. </textarea><br>
  35. <button onclick="runit()" type="button">Run</button>
  36. </form>
  37. <pre id="output"></pre>