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

https://github.com/ivaniscoding/hands-on-pyo3

Extending Python with Rust: a hands-on introduction to PyO3
https://github.com/ivaniscoding/hands-on-pyo3

Last synced: 3 months ago
JSON representation

Extending Python with Rust: a hands-on introduction to PyO3

Awesome Lists containing this project

README

          

# Extending Python with Rust: a hands-on introduction to PyO3

This is an example project using [PyO3](https://github.com/PyO3/pyo3). The goal
of this project is to provide a practical example.

To see it in action: [![Notebook Viewer](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IvanIsCoding/hands-on-pyo3/blob/main/00_Example_Usage.ipynb)

## Structure

```
.
├── 00_Example_Usage.ipynb # Installs the extension from source and tests it
├── Cargo.toml # Defines the Rust manifest: crate name, deps, and more.
├── pyproject.toml # Configuration for Python: defines the build system and deps, and more.
├── Cargo.lock # Lockfile generated by Cargo. This is auto-generated.
├── src/ # Rust implementation using PyO3 and jxl-oxide
├── slides # Slides source in markdown + rendered PDF
├── .cargo # Config files to make this project compile for macOS
└── README.md # This file!
```

## Purpose

This example extension lets users load [JPEG XL](https://en.wikipedia.org/wiki/JPEG_XL)
images into Python as NumPy arrays and [Pillow Images](https://pillow.readthedocs.io/en/stable/reference/Image.html).

We leverage [jxl-oxide](https://docs.rs/jxl-oxide/0.11.4/jxl_oxide/) to read the JPEG XL images. This demo code wraps the Rust crate and makes it available for Python users.

## Usage

To install from source, firstly ensure you have a Rust compiler available.

Then, simply run:

```bash
pip install .
```

Once that is done, the extension module should be available in Python:

```python
import jxl_demo
```

The `jxl_demo` module has two functions: `decode_jxl` and `decode_jxl_as_array`.