Skip to content

Commit 179304b

Browse files
committed
Update WASM example crate/app
1 parent 1d193e7 commit 179304b

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

wasm/example/src/main.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import * as py from 'rustpython_wasm';
1+
import * as rp from 'rustpython_wasm';
22
import pyCode from 'raw-loader!./main.py';
33

4-
fetch('https://github-trending-api.now.sh/repositories')
5-
.then(r => r.json())
6-
.then(repos => {
7-
const result = py.pyEval(pyCode, {
8-
vars: { repos }
9-
});
10-
alert(result);
11-
});
4+
const vm = rp.vmStore.get('main');
5+
6+
vm.exec(pyCode);

wasm/example/src/main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
repos = js_vars['repos']
1+
from browser import fetch
22

3-
star_sum = 0
3+
def fetch_handler(repos):
4+
star_sum = 0
5+
for repo in repos:
6+
star_sum += repo['stars']
7+
print(f'Average github trending star count: {star_sum / len(repos)}')
48

5-
for repo in repos:
6-
star_sum += repo['stars']
79

8-
return 'Average github trending star count: ' + str(star_sum / len(repos))
10+
fetch(
11+
'https://github-trending-api.now.sh/repositories',
12+
response_format='json',
13+
).then(fetch_handler)

0 commit comments

Comments
 (0)