Skip to content

Commit

Permalink
add python package
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronmoreau committed Oct 2, 2021
1 parent 6c0b712 commit 44bc587
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/run-wasm-python/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/run-wasm-python/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "run-wasm-python",
"version": "1.0.0",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/esm/index.d.ts",
"devDependencies": {
"typescript": "^4.4.3"
},
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:esm": "tsc",
"build:cjs": "tsc --module commonjs --outDir lib/cjs"
},
"dependencies": {
"pyodide": "^0.18.0"
}
}
37 changes: 37 additions & 0 deletions packages/run-wasm-python/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class PythonClient {
// <- [reference](https://stackoverflow.com/a/59571016/1375972)
// We redirect stdout to an IO string buffer so that it can be read later
private readonly setStdoutToOutput = `
import sys
import io
sys.stdout = io.StringIO()
`

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public constructor(protected pyodide: any) {
pyodide.runPython(this.setStdoutToOutput)
}

public async run({ code }: { code: string }): Promise<string> {
await this.loadPackages(code)
const output: string = this.pyodide.runPython(code) ?? ''
// Prepend the value of stdout before returning
const stdout: string = this.pyodide.runPython('sys.stdout.getvalue()')
console.log(stdout + output)
return stdout + output
}

private loadPackages(code: string): Promise<any> {
if (typeof this.pyodide.loadPackagesFromImports === 'function') {
console.log('Loading Python dependencies from code')
return this.pyodide.loadPackagesFromImports(code)
}
return this.pyodide.loadPackage([])
}
}

const createPythonClient = (pyodide: string): PythonClient => {
return new PythonClient(pyodide)
}

export { createPythonClient }
22 changes: 22 additions & 0 deletions packages/run-wasm-python/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"outDir": "lib/esm",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom", "es2016", "es2017"],
"jsx": "react",
"declaration": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true
},
"include": ["src"],
"exclude": ["node_modules", "lib", "example-nextjs"]
}
46 changes: 46 additions & 0 deletions packages/run-wasm-python/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


base-64@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-1.0.0.tgz#09d0f2084e32a3fd08c2475b973788eee6ae8f4a"
integrity sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg==

node-fetch@^2.6.1:
version "2.6.5"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.5.tgz#42735537d7f080a7e5f78b6c549b7146be1742fd"
integrity sha512-mmlIVHJEu5rnIxgEgez6b9GgWXbkZj5YZ7fx+2r94a2E+Uirsp6HsPTPlomfdHtpt/B0cdKviwkoaM6pyvUOpQ==
dependencies:
whatwg-url "^5.0.0"

pyodide@^0.18.0:
version "0.18.1"
resolved "https://registry.yarnpkg.com/pyodide/-/pyodide-0.18.1.tgz#9b987de7dc30de261aeabea64052bb7ab3f1fef7"
integrity sha512-QgkdYXu0nkWoJ5Ein2GAgpsT6gfuRCdmiR5C3JSTUEyHnMY0MqU0hx7hpD0/ZVXNSalG7ytVQ+9SgCL2RtVcqg==
dependencies:
base-64 "^1.0.0"
node-fetch "^2.6.1"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

typescript@^4.4.3:
version "4.4.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

0 comments on commit 44bc587

Please sign in to comment.