aboutsummaryrefslogtreecommitdiff
path: root/demos/python.html
blob: 6fae490a977c765127ef55f191cc2cc9b4221d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<html>
<head>
  <title>
    Emscripten: Python
  </title>
  <link id="bespin_base" href="skywriter/"/>
  <script src="skywriter/BespinEmbedded.js"></script>
  <style type="text/css">
      .bespin {
          width: 80%;
          height: 30%;
      }
  </style>

  <!-- Changes from automatically generated code: arguments and run(), and in _PyRun_SimpleStringFlags, add
       if(!this.RAN_ALREADY){this.RAN_ALREADY=true;throw "halting, since this is the first run" }           -->
  <script src="python.js"></script>

  <script>
    // print function which the Python engine will call
    var lines = [], printed = false;

    function print(text) {
      lines.push(text);
      printed = true;
    }

    function execute(text) {
      lines = [];
      printed = false;

      var element = document.getElementById('output');
      if (!element) return; // perhaps during startup

      var ptr = Module.Pointer_make(Module.intArrayFromString(text), 0, 2); // leak!
      try {
        Module._PyRun_SimpleStringFlags(ptr, 0);
      } catch(e) {
        if (e === 'halting, since this is the first run') return;
        element.innerHTML = 'JS crash: |<b>' + e + '</b>|. Please let us know about this problem!<hr>' + element.innerHTML;
        return;
      }

      if (printed) {
        element.innerHTML = lines.map(function(line) { return line.replace('<', '&lt;', 'g') }).join('<br>') + '<hr>' + element.innerHTML;
      } else {
        element.innerHTML = '<small><i>(no output)</i></small><hr>' + element.innerHTML;
      }
    }

    var editor;

    function doRun() {
      args = ['-S', '-c', 'print ""'];
      try {
        Module.run(args);
      } catch (e) {
        if (e !== 'halting, since this is the first run') throw e;
      }
      execute("");

      setTimeout(function() { 
        if (!bespin.useBespin) setTimeout(arguments.callee, 10);
        bespin.useBespin(document.getElementById('the_input'), { "stealFocus":true, "syntax": "python" }).then(function(env) {
          editor = env.editor;
        });
      }, 10);
    }

  </script>
</head>
<body onload="doRun(); document.getElementById('the_input').focus()">
  <p>
    This is CPython, the standard <a href="http://www.python.org">Python</a> implementation, compiled from C to
    JavaScript using <a href="http://emscripten.org">Emscripten</a>,
    running in your browser (without any plugins).
  </p>
  <p>
    <ul>
      <li>Most core language stuff should work, except for importing non-static modules (in other words, <code>import sys</code> will
          work, but other modules won't).</li>
      <li>Please report bugs if you find them!</li>
      <li>Note that this is an unoptimized build (see <a href="http://code.google.com/p/emscripten/issues/detail?id=8">issue 8</a>),
          so it may run slowly. Speed may depend on the browser - Chrome 8 in particular is slow, dunno why (maybe
          <a href="http://code.google.com/p/v8/issues/detail?id=947">V8 issue 947</a>?). Firefox 4 beta 7+ is recommended.</li>
      <li>The editor is <a href="https://mozillalabs.com/skywriter/">Skywriter</a>.
    </ul>
  </p>
  <hr>
  <!-- Call Python's execution function -->
  <form onsubmit="execute(editor.value); return false">
    <b>Enter some Python</b>:
    <input type="submit" value="execute">
    <div id="the_input">import sys
print 'Hello world! This is Python {} on {}'.format(sys.version, sys.platform)

print 'Here are some numbers:', [2*x for x in range(5)][:4]
    </div>
  </form>
  <hr>
  <div id="output" style="font-family: Courier New,Courier,monospace;"></div>
</body>
</html>