diff --git a/wasm/example/src/main.js b/wasm/example/src/main.js index fcd32165e..0ba7fa42c 100644 --- a/wasm/example/src/main.js +++ b/wasm/example/src/main.js @@ -1,11 +1,6 @@ -import * as py from 'rustpython_wasm'; +import * as rp from 'rustpython_wasm'; import pyCode from 'raw-loader!./main.py'; -fetch('https://github-trending-api.now.sh/repositories') - .then(r => r.json()) - .then(repos => { - const result = py.pyEval(pyCode, { - vars: { repos } - }); - alert(result); - }); +const vm = rp.vmStore.get('main'); + +vm.exec(pyCode); diff --git a/wasm/example/src/main.py b/wasm/example/src/main.py index e2d590a3f..8ba2ec00b 100644 --- a/wasm/example/src/main.py +++ b/wasm/example/src/main.py @@ -1,8 +1,13 @@ -repos = js_vars['repos'] +from browser import fetch -star_sum = 0 +def fetch_handler(repos): + star_sum = 0 + for repo in repos: + star_sum += repo['stars'] + print(f'Average github trending star count: {star_sum / len(repos)}') -for repo in repos: - star_sum += repo['stars'] -return 'Average github trending star count: ' + str(star_sum / len(repos)) +fetch( + 'https://github-trending-api.now.sh/repositories', + response_format='json', +).then(fetch_handler) \ No newline at end of file