Python Bidi
Python Bidi is a library for handling bi-directional (BiDi) text layout, primarily for Right-to-Left (RTL) languages like Arabic and Hebrew. It offers two implementations: a pure Python algorithm and a more performant wrapper around the Rust `unicode-bidi` crate. Currently at version 0.6.7, it maintains an active development and release cadence.
Warnings
- breaking Version 0.5.0 introduced significant breaking changes, including dropping support for Python versions older than 3.9. The primary import path for `get_display` changed to `from bidi import get_display` (for the Rust-based implementation), and the `upper_is_rtl` parameter was removed from this new implementation.
- breaking Version 0.6.6 removed support for PyPy3.7 and PyPy3.8 due to an upgrade of the underlying `pyo3` library.
- gotcha When building `python-bidi` for platforms like Android using tools like Buildozer, issues may arise due to incompatible pre-built binary wheels (`.so` files). This can lead to runtime errors.
- gotcha When integrating `python-bidi` with other text processing libraries like `arabic-reshaper`, incorrect handling of string encoding (especially when reading from or writing to files) can lead to unexpected display issues or `AssertionError: LRI not allowed here`.
- deprecated The `upper_is_rtl` argument in `get_display` was a debugging feature specific to the pure Python implementation of the algorithm. It is ignored by the default Rust-based implementation (from `bidi import get_display`) in versions 0.5.0 and later.
Install
-
pip install python-bidi
Imports
- get_display
from bidi import get_display
- get_display (Python algo)
from bidi.algorithm import get_display
Quickstart
from bidi import get_display
# Example Hebrew text (read as: 'Shalom')
HEBREW_TEXT = ''.join([
'ש', # Shin
'ל', # Lamed
'ו', # Vav
'ם' # Final Mem
])
displayed_text = get_display(HEBREW_TEXT)
print(f"Original: {HEBREW_TEXT}")
print(f"Displayed: {displayed_text}")
# Example with mixed LTR/RTL
mixed_text = "Hello שלום World"
displayed_mixed = get_display(mixed_text)
print(f"Original: {mixed_text}")
print(f"Displayed: {displayed_mixed}")