A comprehensive collection of testing, debugging, and architectural patterns for building production-ready Pyodide applications.
This project serves as a "cookbook" and reference implementation for developers working with Pyodide. It consolidates best practices for:
- Testing: Robust end-to-end testing with Playwright and Pytest.
- Debugging: Tactics for diagnosing issues in the browser (console logs, network errors, Web Workers).
- Loading: Strategies for efficient package loading and error handling, including runtime errors.
- Workers: Patterns for running Python code in Web Workers, including load failure handling.
The project is organized into "patterns", each demonstrating a specific concept or solution.
pyodide-patterns/
├── src/ # Python package source
├── tests/
│ └── patterns/ # The core content
│ ├── debugging/ # Debugging scenarios (race conditions, etc.)
│ ├── loading/ # Package loading and error handling
│ ├── workers/ # Web Worker patterns
│ └── testing/ # General testing patterns
└── examples/ # HTML/JS examples for the tests
- Python 3.8+
- Hatch (recommended for project management)
- Clone the repository.
- Install dependencies and run tests using Hatch:
hatch build
cd _my_local_package && hatch build && cd ..
hatch run testOr manually with pip:
pip install pytest pytest-playwright pytest-asyncio
playwright install
pytest- End-to-End Testing: How to test full user flows in the browser.
- Functional Testing: How to verify specific features.
- Test Suite Isolation: How to resolve event loop conflicts between
pytest-asyncioandpytest-playwright(inspired by the Imposition project).
- Race Conditions: How to identify and fix async race conditions in tests.
- Console Logs: How to capture and assert against browser console logs.
- Network Failures: How to simulate and test network errors (e.g., 404s).
- Basic Worker: Running Pyodide in a web worker.
- Advanced Worker: Handling errors and package loading inside workers.
- Data Cloning: Understanding limitations of data transfer between main thread and workers.
- Load Failure: Documenting the difficulty of reliably capturing worker script load failures.
- Package Loading: Strategies for loading packages via
micropip. - Error Handling: Gracefully handling load failures and runtime errors.
- Runtime Error: Demonstrates handling of Python runtime errors within Pyodide.
Contributions are welcome! If you have a new pattern or recipe, please see our contributing guide for more details.