Skip to content

Commit 8d78c67

Browse files
committed
Created a demo page so user can try their code online
1 parent 5734fc2 commit 8d78c67

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

wasm/app/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,27 @@
33
<head>
44
<meta charset="utf-8">
55
<title>RustPython Starter Application</title>
6+
<style type="text/css" media="screen">
7+
#code {
8+
height: 70vh;
9+
width: 95vw;
10+
}
11+
12+
#run-btn {
13+
width: 10em;
14+
height: 5em;
15+
}
16+
</style>
617
</head>
718
<body>
19+
<h1>RustPython Demo</h1>
20+
<p>Please input your python code below and click <kbd>Run</kbd>:</p>
21+
<textarea id="code">x = 1
22+
y = 2
23+
print('Hello! x + y equals to ' + str(x+y))
24+
</textarea>
25+
<button id="run-btn">Run</button>
26+
<h2>Open the browser console (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd> or <kbd>F12</kbd>) to see the output</h2>
827
<script src="./bootstrap.js"></script>
928
</body>
1029
</html>

wasm/app/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
import * as rp from "rustpython_wasm";
22

3-
rp.run_code("print('Hello Python!')\n");
3+
function runCodeFromTextarea(_) {
4+
const code = document.getElementById('code').value;
5+
if (!code.endsWith('\n')) { // HACK: if the code doesn't end with newline it crashes.
6+
rp.run_code(code + '\n');
7+
return;
8+
}
9+
rp.run_code(code);
10+
}
11+
document.getElementById('run-btn').addEventListener('click', runCodeFromTextarea);
12+
13+
runCodeFromTextarea(); // Run once for demo

0 commit comments

Comments
 (0)