From 179304bc93cc1a114c2db89cdbff0baab4a79427 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Tue, 26 Feb 2019 23:46:53 -0600 Subject: [PATCH] Update WASM example crate/app --- wasm/example/src/main.js | 13 ++++--------- wasm/example/src/main.py | 15 ++++++++++----- 2 files changed, 14 insertions(+), 14 deletions(-) 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