Skip to content

Commit 3a5246b

Browse files
authored
feat: add bun support (denosaurs#47)
1 parent b887916 commit 3a5246b

23 files changed

+358
-152
lines changed

.github/workflows/checks.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ jobs:
5454
with:
5555
deno-version: v1.x
5656

57+
- name: Setup Bun
58+
if: ${{ matrix.os != 'windows-latest' }}
59+
uses: oven-sh/setup-bun@v1
60+
5761
- name: Setup Python (Windows)
5862
uses: actions/setup-python@v2
5963
if: ${{ matrix.os == 'windows-latest' }}
@@ -65,3 +69,7 @@ jobs:
6569

6670
- name: Run deno test
6771
run: deno task test
72+
73+
- name: Run bun test
74+
if: ${{ matrix.os != 'windows-latest' }}
75+
run: bun test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
/*.bat
33
deno.lock
44
plug/
5+
bun.lockb
6+
node_modules/
7+
__pycache__/

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# deno_python
1+
# Python Bridge
22

33
[![Tags](https://img.shields.io/github/release/denosaurs/deno_python)](https://github.com/denosaurs/deno_python/releases)
44
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/python/mod.ts)
55
[![checks](https://github.com/denosaurs/deno_python/actions/workflows/checks.yml/badge.svg)](https://github.com/denosaurs/deno_python/actions/workflows/checks.yml)
66
[![License](https://img.shields.io/github/license/denosaurs/deno_python)](https://github.com/denosaurs/deno_python/blob/master/LICENSE)
77

8-
This module provides a seamless integration between deno and python by
9-
integrating with the [Python/C API](https://docs.python.org/3/c-api/index.html).
10-
It acts as a bridge between the two languages, enabling you to pass data and
11-
execute python code from within your deno applications. This enables access to
12-
the large and wonderful [python ecosystem](https://pypi.org/) while remaining
13-
native (unlike a runtime like the wonderful
14-
[pyodide](https://github.com/pyodide/pyodide) which is compiled to wasm,
15-
sandboxed and may not work with all python packages) and simply using the
16-
existing python installation.
8+
This module provides a seamless integration between JavaScript (Deno/Bun) and
9+
Python by integrating with the
10+
[Python/C API](https://docs.python.org/3/c-api/index.html). It acts as a bridge
11+
between the two languages, enabling you to pass data and execute python code
12+
from within your JS applications. This enables access to the large and wonderful
13+
[python ecosystem](https://pypi.org/) while remaining native (unlike a runtime
14+
like the wonderful [pyodide](https://github.com/pyodide/pyodide) which is
15+
compiled to wasm, sandboxed and may not work with all python packages) and
16+
simply using the existing python installation.
1717

1818
## Example
1919

@@ -40,6 +40,23 @@ permissions since enabling FFI effectively escapes the permissions sandbox.
4040
deno run -A --unstable <file>
4141
```
4242

43+
### Usage in Bun
44+
45+
You can import from the `bunpy` NPM package to use this module in Bun.
46+
47+
```ts
48+
import { python } from "bunpy";
49+
50+
const np = python.import("numpy");
51+
const plt = python.import("matplotlib.pyplot");
52+
53+
const xpoints = np.array([1, 8]);
54+
const ypoints = np.array([3, 10]);
55+
56+
plt.plot(xpoints, ypoints);
57+
plt.show();
58+
```
59+
4360
### Dependencies
4461

4562
Normally deno_python follows the default python way of resolving imports, going

bunfig.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preload = ["./plugin.ts"]

examples/hello_python.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ const { print, str } = python.builtins;
44
const { version } = python.import("sys");
55

66
print(str("Hello, World!").lower());
7-
print(`Python version: ${version}`);
7+
print("Python version:", version);

examples/import.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { add } from "./test.py";
2+
import { print } from "python:builtins";
3+
import * as np from "python:numpy";
4+
5+
console.log(add(1, 2));
6+
print("Hello, world!");
7+
console.log(np.array([1, 2, 3]));

examples/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def add(a, b):
2+
return a + b

ipy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import py, { Python } from "./mod.ts";
2+
import { Pip, pip } from "./ext/pip.ts";
3+
4+
declare global {
5+
const py: Python;
6+
const pip: Pip;
7+
}
8+
9+
Object.defineProperty(globalThis, "py", {
10+
value: py,
11+
writable: false,
12+
enumerable: false,
13+
configurable: false,
14+
});
15+
16+
Object.defineProperty(globalThis, "pip", {
17+
value: pip,
18+
writable: false,
19+
enumerable: false,
20+
configurable: false,
21+
});
22+
23+
export * from "./mod.ts";
24+
export * from "./ext/pip.ts";
25+
export default py;

mod.bun.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module "python:*";
2+
declare module "*.py";
3+
4+
import "./src/bun_compat.js";
5+
export * from "./mod.ts";

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "bunpy",
3+
"version": "0.3.3",
4+
"description": "JavaScript -> Python Bridge for Deno and Bun",
5+
"main": "mod.bun.ts",
6+
"directories": {
7+
"example": "examples",
8+
"test": "test"
9+
},
10+
"scripts": {
11+
"test": "deno task test && bun test"
12+
},
13+
"files": [
14+
"mod.ts",
15+
"ext/pip.ts",
16+
"src/bun_compat.js",
17+
"src/ffi.ts",
18+
"src/python.ts",
19+
"src/symbols.ts",
20+
"src/util.ts",
21+
"plugin.ts"
22+
],
23+
"keywords": [],
24+
"author": "DjDeveloperr",
25+
"license": "MIT"
26+
}

0 commit comments

Comments
 (0)