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!

Coming from wgpu.gui? Check from_wgpu_canvas.md.

Purpose

  • Provide a generic canvas API to render to.
  • Provide an event loop for scheduling events and draws.
  • Provide a simple but powerful event system with standardized event objects.
  • Provide various canvas implementations:
    • One that is light and easily installed (glfw).
    • For various GUI libraries (e.g. qt and wx), so visuzalizations can be embedded in a GUI.
    • For specific platforms (e.g. Jupyter, browser).

The main use-case is rendering with wgpu, but rendercanvascan be used by anything that can render based on a window-id or by producing bitmap images.

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_context("bitmap")

@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-drived framework like Qt. If you like callbacks, loop.call_later() always works. If you like async, use loop.add_task(). Event handlers can always be async. 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.1.1.tar.gz (61.0 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.1.1-py3-none-any.whl (73.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rendercanvas-2.1.1.tar.gz
  • Upload date:
  • Size: 61.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rendercanvas-2.1.1.tar.gz
Algorithm Hash digest
SHA256 f4138bd76b9674255f76c6a7cd88750a558c4f2dba8aaaa864c03670b5f15017
MD5 899c57df3eec51f364720d096db80d8f
BLAKE2b-256 8f66685a1c7b223c4c228ad9b71d529282e52d92d1414b460992600c52db42df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rendercanvas-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 73.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rendercanvas-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef192aa5ad6aed23165f5f7b8cf3419716d8891b7ca6e3779fa63c0e29940df8
MD5 e479b3dde9f5d60fa807d593564acde6
BLAKE2b-256 fdbcad515363e1b6cbc34f67b3b0b7584d13ecb0a114a1e583a1809c855adf4d

See more details on using hashes here.

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