Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mp3martin/jsrunpy.js

Use Brython to run any Python code with JS at any time using run function that returns promise
https://github.com/mp3martin/jsrunpy.js

javascript js library python python3 web

Last synced: 27 days ago
JSON representation

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

Awesome Lists containing this project

README

        

[![CodeFactor](https://www.codefactor.io/repository/github/MP3Martin/jsRUNpy.js/badge)](#/)
[](#/)
[](#/)
[](#/)
[](#/)
----
[](#/)

# 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](https://github.com/brython-dev/brython) **[brython-dev/brython](https://github.com/brython-dev/brython)** **and i have no intention to misuse the code** ⚠
* ⚠ **This package is 1MB - 4MB (depending on [File chooser](#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](#file-chooser)** below and click on it's name

#### 2. Was the step 1 too confusing?
* Just add
```html

```
* in your `` 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](https://www.jsdelivr.com/))*

3. jsRUNpy.min.extreme.js - extremely compressed file using [gh](https://github.com/brython-dev/brython) **[streamich/crunchme](https://github.com/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
* can be found [here](https://codesandbox.io/s/github/MP3Martin/jsRUNpy.js/tree/main/examples/example-multiply?file=/index.html), source [here](https://github.com/MP3Martin/jsRUNpy.js/blob/main/examples/example-multiply/index.html)

## Functions

|Emoji|Meaning|
|--|--|
|✅|Mandatory argument|
|🟨|Optional argument|

* ### **`jsRUNpy.run()`**
* **`jsRUNpy.run(`** ✅**code**: *string* **`,`** 🟨**variables**: *object* **`)`**

## Usage / examples
> ```js
> jsRUNpy.run("print(test)", {test: "Hello World!"})
> ```

> is same as

> ```js
> jsRUNpy.run("print('Hello World!')")
> ```

---

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

> is same as

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

---

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

> is same as

> ```js
> jsRUNpy.run("print(1)\nprint(2)")
> ```

> and is same as

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

---

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

---

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