aboutsummaryrefslogtreecommitdiff
path: root/demos/python.html
diff options
context:
space:
mode:
authorAlon Zakai <azakai@mozilla.com>2010-12-12 21:26:15 -0800
committerAlon Zakai <azakai@mozilla.com>2010-12-12 21:26:15 -0800
commit45c53b2520a326b8d870321a6f39ac17fbc297c9 (patch)
tree04e6e4e36f4d471b992ca7c25ce33822b684e86f /demos/python.html
parent4118243d177251dce74f2beb10c013aa3aa2f53f (diff)
python web demo
Diffstat (limited to 'demos/python.html')
-rw-r--r--demos/python.html95
1 files changed, 95 insertions, 0 deletions
diff --git a/demos/python.html b/demos/python.html
new file mode 100644
index 00000000..d56fc436
--- /dev/null
+++ b/demos/python.html
@@ -0,0 +1,95 @@
+<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>
+ <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 {
+ 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 modules. 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>)</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">
+print "Hello world! Here are some numbers:", [2*x for x in range(5)][:4]
+print "Here is a random language feature:", dir(None)
+ </div>
+ </form>
+ <hr>
+ <div id="output" style="font-family: Courier New,Courier,monospace;"></div>
+</body>
+</html>
+