Skip to content

Use Brython to run any Python code with JS at any time using run function that returns promise

License

Notifications You must be signed in to change notification settings

MP3Martin/jsRUNpy.js

Repository files navigation

CodeFactor

jsRUNpy - use Brython to run any Python code with JS

  • returns promise that is resolved when the code finishes
  • if the Python code returns something, it gets set as the promise's value
  • allows you to pass dictionary of varibles that can be used by the python script

Warnings

  • All of the Brython code is owned by the authors of gh brython-dev/brython and i have no intention to misuse the code
  • This package is 1MB - 4MB (depending on File chooser) in size, not recommended for production
  • This package is used standalone, all dependencies are included
  • Not tested concurrently with Brython
  • Use at your own risk, bugs may be present!

Installation

1. Find your file in File chooser below and click on it's name

2. Was the step 1 too confusing?

  • Just add
<script src="https://cdn.jsdelivr.net/gh/MP3Martin/jsRUNpy.js@1/jsRUNpy.min.js"></script>
  • in your <head> tags

File chooser

Click on the file's name and read what the page says

  1. jsRUNpy.js - uncompressed file, nearly 4MB, not recommended

  2. jsRUNpy.min.js - compressed file, nearly 4MB, ✅recommended✅, (not located in this repo, it is automatically generated using jsDelivr)

  3. jsRUNpy.min.extreme.js - extremely compressed file using gh streamich/crunchme, nearly 1MB, not recommended, ¼ size of the recommended file, but can lag your browser for a few seconds (depending on the hardware)

Fully working example

Functions

Emoji Meaning
Mandatory argument
🟨 Optional argument
  • jsRUNpy.run()

    • jsRUNpy.run(code: string , 🟨variables: object )

Usage / examples

jsRUNpy.run("print(test)", {test: "Hello World!"}) 

is same as

jsRUNpy.run("print('Hello World!')") 

jsRUNpy.run("return 33 + 77").then(out => {console.log("Python code outputted: " + out)})

is same as

(async () => {
   out = await jsRUNpy.run("return 33 + 77")
   console.log("Python code outputted: " + out)
})()

jsRUNpy.run(`
print(1)
print(2)
`)

is same as

jsRUNpy.run("print(1)\nprint(2)")

and is same as

for (i = 1; i <= 2; i++) {
  jsRUNpy.run("print(i)", {i: i})
}

test = "Hello!"

jsRUNpy.run(`
from browser import window
window["test"] = "Hi!"
`).then(()=>{console.log(test)})

for (i = 0; i <= 10; i++) {
  jsRUNpy.run("print(i)", {i: i})
}

About

Use Brython to run any Python code with JS at any time using run function that returns promise

Topics

Resources

License

Stars

Watchers

Forks