Skip to content

Commit d02e835

Browse files
committed
Allow for multiple demo snippets to be selected
1 parent c675a59 commit d02e835

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

wasm/demo/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
},
99
"devDependencies": {
1010
"@wasm-tool/wasm-pack-plugin": "0.2.0",
11-
"webpack": "^4.16.3",
12-
"webpack-cli": "^3.1.0",
13-
"webpack-dev-server": "^3.1.5",
1411
"copy-webpack-plugin": "^4.5.2",
15-
"mini-css-extract-plugin": "^0.5.0",
12+
"css-loader": "^2.0.1",
1613
"html-webpack-plugin": "^3.2.0",
17-
"css-loader": "^2.0.1"
14+
"mini-css-extract-plugin": "^0.5.0",
15+
"raw-loader": "^1.0.0",
16+
"webpack": "^4.16.3",
17+
"webpack-cli": "^3.1.0",
18+
"webpack-dev-server": "^3.1.5"
1819
},
1920
"scripts": {
2021
"dev": "webpack-dev-server -d",

wasm/demo/src/index.html renamed to wasm/demo/src/index.ejs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ <h1>RustPython Demo</h1>
99
<p>
1010
RustPython is a Python interpreter written in Rust. This demo is
1111
compiled from Rust to WebAssembly so it runs in the browser.<br>
12-
Please input your Python code below and click <kbd>Run</kbd>, or you
13-
can open up your browser's devtools and play with
14-
<code>rp.pyEval('print("a")')</code>
12+
Please input your Python code below and click <kbd>Run</kbd>
13+
(or <kbd>Ctrl+Enter</kbd>), or you can open up your
14+
browser's devtools and play with <code>rp.pyEval('print("a")')</code>
1515
</p>
16-
<textarea id="code">
17-
n1 = 0
18-
n2 = 1
19-
count = 0
20-
until = 10
21-
22-
print("These are the first {} numbers in the Fibonacci sequence:".format(until))
23-
24-
while count < until:
25-
print(n1)
26-
n1, n2 = n2, n1 + n2
27-
count += 1
16+
<div id="code-wrapper">
17+
<textarea id="code">
18+
<%= defaultSnippet %>
2819
</textarea>
20+
<select id="snippets">
21+
<% for (const name of snippets) { %>
22+
<option
23+
<% if (name == defaultSnippetName) %> selected
24+
><%= name %></option>
25+
<% } %>
26+
</select>
27+
</div>
2928
<button id="run-btn">Run &#9655;</button>
3029
<div id="error"></div>
3130
<h3>Standard Output</h3>

wasm/demo/src/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ document
4646
.getElementById('run-btn')
4747
.addEventListener('click', runCodeFromTextarea);
4848

49+
const snippets = document.getElementById('snippets');
50+
51+
snippets.addEventListener('change', () => {
52+
const selected = snippets.value;
53+
54+
// the require here creates a webpack context; it's fine to use it
55+
// dynamically.
56+
// https://webpack.js.org/guides/dependency-management/
57+
const snippet = require(`raw-loader!./snippets/${selected}.py`);
58+
59+
editor.setValue(snippet);
60+
61+
runCodeFromTextarea();
62+
});
63+
4964
runCodeFromTextarea(); // Run once for demo
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n1 = 0
2+
n2 = 1
3+
count = 0
4+
until = 10
5+
6+
print(f"These are the first {until} numbers in the Fibonacci sequence:")
7+
8+
while count < until:
9+
print(n1)
10+
n1, n2 = n2, n1 + n2
11+
count += 1

wasm/demo/src/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ textarea {
2525
margin-top: 10px;
2626
font-family: monospace;
2727
}
28+
29+
#code-wrapper {
30+
position: relative;
31+
}
32+
33+
#snippets {
34+
position: absolute;
35+
right: 20px;
36+
top: 5px;
37+
z-index: 25;
38+
}

wasm/demo/webpack.config.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
22
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
33
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
44
const path = require('path');
5+
const fs = require('fs');
56

67
module.exports = {
78
entry: './src/index.js',
@@ -12,13 +13,27 @@ module.exports = {
1213
mode: 'development',
1314
module: {
1415
rules: [
15-
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] }
16+
{
17+
test: /\.css$/,
18+
use: [MiniCssExtractPlugin.loader, 'css-loader']
19+
}
1620
]
1721
},
1822
plugins: [
1923
new HtmlWebpackPlugin({
2024
filename: 'index.html',
21-
template: 'src/index.html'
25+
template: 'src/index.ejs',
26+
templateParameters: {
27+
snippets: fs
28+
.readdirSync(path.join(__dirname, 'src/snippets'))
29+
.map(filename =>
30+
path.basename(filename, path.extname(filename))
31+
),
32+
defaultSnippetName: 'fibonacci',
33+
defaultSnippet: fs.readFileSync(
34+
path.join(__dirname, 'src/snippets/fibonacci.py')
35+
)
36+
}
2237
}),
2338
new MiniCssExtractPlugin({
2439
filename: 'styles.css'

0 commit comments

Comments
 (0)