|
1 | | -# python-node |
| 1 | +# @platformatic/python-node |
2 | 2 |
|
3 | | -Work-in-progress implementation of a Python ASGI server running in Node.js |
4 | | -via Rust. |
| 3 | +A high-performance Node.js native addon that enables running **ASGI-compatible Python applications** directly within Node.js environments. The Node.js host can communicate directly with the Python guest app without any network involved, allowing near-zero communcation overhead. |
| 4 | + |
| 5 | +This module provides a bridge between Node.js and Python, allowing you to: |
| 6 | + |
| 7 | +- **Run Python ASGI applications** (FastAPI, Starlette, Django ASGI, etc.) inside Node.js processes |
| 8 | +- **Handle HTTP requests** with ASGI 3.0 protocol support |
| 9 | +- **Process requests concurrently** using async Python code execution |
| 10 | +- **Integrate Python services** into existing Node.js applications seamlessly |
| 11 | +- **Support virtual environments** automatically for proper Python dependency isolation |
| 12 | + |
| 13 | +The module implements an ASGI server that translates between Node.js HTTP requests and Python ASGI applications, enabling you to leverage Python's rich ecosystem within Node.js applications. |
| 14 | + |
| 15 | +### Key Features |
| 16 | + |
| 17 | +- **ASGI 3.0 Support**: HTTP and Lifespan protocols (WebSocket support planned) |
| 18 | +- **Async/Sync Methods**: Both `handleRequest()` and `handleRequestSync()` |
| 19 | +- **Virtual Environment Detection**: Automatic Python environment discovery |
| 20 | +- **Cross-Platform**: Native binaries for macOS and Linux |
| 21 | +- **High Performance**: Rust-based implementation with minimal overhead |
| 22 | + |
| 23 | +### Requirements |
| 24 | + |
| 25 | +- **Node.js**: ≥ 20.0.0 |
| 26 | +- **Python**: ≥ 3.8 with asyncio support |
| 27 | +- **System**: macOS (arm64, x64) or Linux (x64-gnu) |
| 28 | + |
| 29 | +The module automatically detects and uses Python virtual environments via the `VIRTUAL_ENV` environment variable. |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install @platformatic/python-node |
| 35 | +``` |
| 36 | + |
| 37 | +## API Reference |
| 38 | + |
| 39 | +For complete API documentation, see [REFERENCE.md](./REFERENCE.md). |
| 40 | + |
| 41 | +## Basic Usage |
| 42 | + |
| 43 | +```python |
| 44 | +# fastapi_app.py |
| 45 | +from fastapi import FastAPI |
| 46 | +from pydantic import BaseModel |
| 47 | + |
| 48 | +app = FastAPI() |
| 49 | + |
| 50 | +class Item(BaseModel): |
| 51 | + name: str |
| 52 | + price: float |
| 53 | + |
| 54 | +@app.post("/items/") |
| 55 | +async def create_item(item: Item): |
| 56 | + return {"name": item.name, "price": item.price} |
| 57 | +``` |
| 58 | + |
| 59 | +```javascript |
| 60 | +import { Python, Request } from '@platformatic/python-node' |
| 61 | + |
| 62 | +const python = new Python({ |
| 63 | + docroot: './python-apps', |
| 64 | + appTarget: 'fastapi_app:app' |
| 65 | +}) |
| 66 | + |
| 67 | +// POST with JSON body |
| 68 | +const response = await python.handleRequest(new Request({ |
| 69 | + method: 'POST', |
| 70 | + url: '/items/', |
| 71 | + headers: { |
| 72 | + 'Content-Type': 'application/json' |
| 73 | + }, |
| 74 | + body: Buffer.from(JSON.stringify({ |
| 75 | + name: 'Widget', |
| 76 | + price: 29.99 |
| 77 | + })) |
| 78 | +})) |
| 79 | + |
| 80 | +const result = JSON.parse(response.body.toString()) |
| 81 | +console.log(result) // { name: 'Widget', price: 29.99 } |
| 82 | +``` |
0 commit comments