Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions wasm/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "0.2.0",
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"copy-webpack-plugin": "^4.5.2",
"mini-css-extract-plugin": "^0.5.0",
"css-loader": "^2.0.1",
"html-webpack-plugin": "^3.2.0",
"css-loader": "^2.0.1"
"mini-css-extract-plugin": "^0.5.0",
"raw-loader": "^1.0.0",
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"scripts": {
"dev": "webpack-dev-server -d",
Expand Down
11 changes: 11 additions & 0 deletions wasm/demo/snippets/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
n1 = 0
n2 = 1
count = 0
until = 10

print(f"These are the first {until} numbers in the Fibonacci sequence:")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! f-strings!


while count < until:
print(n1)
n1, n2 = n2, n1 + n2
count += 1
7 changes: 7 additions & 0 deletions wasm/demo/snippets/fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for i in range(1, 100):
print(f"{i} ", end="")
if not i % 3:
print("fizz", end="")
if not i % 5:
print("buzz", end="")
print()
31 changes: 31 additions & 0 deletions wasm/demo/snippets/mandelbrot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# NOTE: will take a while, up to around a minute, to run.
# Expect this page to freeze.

w = 50.0
h = 50.0

y = 0.0
while y < h:
x = 0.0
while x < w:
Zr, Zi, Tr, Ti = 0.0, 0.0, 0.0, 0.0
Cr = 2 * x / w - 1.5
Ci = 2 * y / h - 1.0

i = 0
while i < 50 and Tr + Ti <= 4:
Zi = 2 * Zr * Zi + Ci
Zr = Tr - Ti + Cr
Tr = Zr * Zr
Ti = Zi * Zi
i = i + 1

if Tr + Ti <= 4:
print('*', end='')
else:
print('·', end='')

x = x + 1

print()
y = y + 1
29 changes: 14 additions & 15 deletions wasm/demo/src/index.html → wasm/demo/src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ <h1>RustPython Demo</h1>
<p>
RustPython is a Python interpreter written in Rust. This demo is
compiled from Rust to WebAssembly so it runs in the browser.<br>
Please input your Python code below and click <kbd>Run</kbd>, or you
can open up your browser's devtools and play with
<code>rp.pyEval('print("a")')</code>
Please input your Python code below and click <kbd>Run</kbd>
(or <kbd>Ctrl+Enter</kbd>), or you can open up your
browser's devtools and play with <code>rp.pyEval('print("a")')</code>
</p>
<textarea id="code">
n1 = 0
n2 = 1
count = 0
until = 10

print("These are the first {} numbers in the Fibonacci sequence:".format(until))

while count < until:
print(n1)
n1, n2 = n2, n1 + n2
count += 1
<div id="code-wrapper">
<textarea id="code">
<%= defaultSnippet %>
</textarea>
<select id="snippets">
<% for (const name of snippets) { %>
<option
<% if (name == defaultSnippetName) %> selected
><%= name %></option>
<% } %>
</select>
</div>
<button id="run-btn">Run &#9655;</button>
<div id="error"></div>
<h3>Standard Output</h3>
Expand Down
15 changes: 15 additions & 0 deletions wasm/demo/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ document
.getElementById('run-btn')
.addEventListener('click', runCodeFromTextarea);

const snippets = document.getElementById('snippets');

snippets.addEventListener('change', () => {
const selected = snippets.value;

// the require here creates a webpack context; it's fine to use it
// dynamically.
// https://webpack.js.org/guides/dependency-management/
const snippet = require(`raw-loader!../snippets/${selected}.py`);

editor.setValue(snippet);

runCodeFromTextarea();
});

runCodeFromTextarea(); // Run once for demo
11 changes: 11 additions & 0 deletions wasm/demo/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ textarea {
margin-top: 10px;
font-family: monospace;
}

#code-wrapper {
position: relative;
}

#snippets {
position: absolute;
right: 20px;
top: 5px;
z-index: 25;
}
19 changes: 17 additions & 2 deletions wasm/demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
const path = require('path');
const fs = require('fs');

module.exports = {
entry: './src/index.js',
Expand All @@ -12,13 +13,27 @@ module.exports = {
mode: 'development',
module: {
rules: [
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] }
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html'
template: 'src/index.ejs',
templateParameters: {
snippets: fs
.readdirSync(path.join(__dirname, 'snippets'))
.map(filename =>
path.basename(filename, path.extname(filename))
),
defaultSnippetName: 'fibonacci',
defaultSnippet: fs.readFileSync(
path.join(__dirname, 'snippets/fibonacci.py')
)
}
}),
new MiniCssExtractPlugin({
filename: 'styles.css'
Expand Down