Skip to main content

One canvas API, multiple backends

Project description

CI Documentation Status PyPI version EffVer Versioning


rendercanvas

One canvas API, multiple backends 🚀

This project is part of pygfx.org

Introduction

See how the two windows above look the same? That's the idea; they also look the same to the code that renders to them. Yet, the GUI systems are very different (Qt vs glfw in this case). Now that's a powerful abstraction!

Purpose

Providing a generic API for:

  • managing a canvas window (BaseRenderCanvas).
  • presenting rendered results with wgpu (WgpuContext).
  • presenting rendered results as a bitmap (BitmapContext).
  • working with events that have standardized behavior.

Implement that on top of a variety of backends:

  • Running on desktop with a light backend (glfw).
  • Running in the browser (with Pyodide or PyScript).
  • Running from a (Jupyter) notebook.
  • Embedding as a widget in a GUI library.
    • Qt
    • wx
  • In addition to the GUI libraries mentioned above, the following event loops are supported:
    • asyncio
    • trio
    • raw

Installation

pip install rendercanvas

To have at least one backend, we recommend:

pip install rendercanvas glfw

Usage

Also see the online documentation and the examples.

A minimal example that renders noise:

import numpy as np
from rendercanvas.auto import RenderCanvas, loop

canvas = RenderCanvas(update_mode="continuous")
context = canvas.get_bitmap_context()

@canvas.request_draw
def animate():
    w, h = canvas.get_logical_size()
    bitmap = np.random.uniform(0, 255, (h, w)).astype(np.uint8)
    context.set_bitmap(bitmap)

loop.run()

Run wgpu visualizations:

from rendercanvas.auto import RenderCanvas, loop
from rendercanvas.utils.cube import setup_drawing_sync


canvas = RenderCanvas(
    title="The wgpu cube example on $backend", update_mode="continuous"
)
draw_frame = setup_drawing_sync(canvas)
canvas.request_draw(draw_frame)

loop.run()

Embed in a Qt application:

from PySide6 import QtWidgets
from rendercanvas.qt import QRenderWidget

class Main(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        splitter = QtWidgets.QSplitter()
        self.canvas = QRenderWidget(splitter)
        ...


app = QtWidgets.QApplication([])
main = Main()
app.exec()

Async or not async

We support both; a render canvas can be used in a fully async setting using e.g. Asyncio or Trio, or in an event-driven framework like Qt. If you like callbacks, loop.call_later() always works. If you like async, use loop.add_task(). See the docs on async for details.

License

This code is distributed under the 2-clause BSD license.

Developers

  • Clone the repo.
  • Install rendercanvas and developer deps using pip install -e .[dev].
  • Use ruff format to apply autoformatting.
  • Use ruff check to check for linting errors.
  • Optionally, if you install pre-commit hooks with pre-commit install, lint fixes and formatting will be automatically applied on git commit.
  • Use pytest tests to run the tests.
  • Use pytest examples to run a subset of the examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rendercanvas-2.6.1.tar.gz (89.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rendercanvas-2.6.1-py3-none-any.whl (104.7 kB view details)

Uploaded Python 3

File details

Details for the file rendercanvas-2.6.1.tar.gz.

File metadata

  • Download URL: rendercanvas-2.6.1.tar.gz
  • Upload date:
  • Size: 89.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rendercanvas-2.6.1.tar.gz
Algorithm Hash digest
SHA256 8d17ff8edb76adb368fd3d7c7f90e1f6e291f204d15f6eabb69813a52ad476fb
MD5 df08eef9690cbae70f5ff2c8aa0737a0
BLAKE2b-256 edfc51803cd58b05bbc933871dd3be3d9d382c41b25cc4edd0c173f1450dbc00

See more details on using hashes here.

Provenance

The following attestation bundles were made for rendercanvas-2.6.1.tar.gz:

Publisher: ci.yml on pygfx/rendercanvas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rendercanvas-2.6.1-py3-none-any.whl.

File metadata

  • Download URL: rendercanvas-2.6.1-py3-none-any.whl
  • Upload date:
  • Size: 104.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rendercanvas-2.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 190b1ca72deb18b03f25450087c4cd3ae672ec3680e02e3875aac76863e96e3f
MD5 4747921eca4691200e6538a215397b11
BLAKE2b-256 c03629f083899019b6e7cb987d711c93edea061a3b7d0e0ee79f4c6bbc3ef7c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for rendercanvas-2.6.1-py3-none-any.whl:

Publisher: ci.yml on pygfx/rendercanvas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page