Back to Perspective

What is `perspective-python`

docs/md/explanation/python.md

5.2.03.6 KB
Original Source

What is perspective-python

Perspective for Python uses the exact same C++ data engine used by the WebAssembly version and Rust version. The library consists of many of the same abstractions and API as in JavaScript, as well as Python-specific data loading support for NumPy, Pandas (and Apache Arrow, as in JavaScript).

Additionally, perspective-python provides a session manager suitable for integration into server systems such as Tornado websockets, AIOHTTP, or Starlette/FastAPI, which allows fully virtual Perspective tables to be interacted with by multiple <perspective-viewer> in a web browser. You can also interact with a Perspective table from python clients, and to that end client libraries are implemented for both Tornado and AIOHTTP.

Example

A simple example which loads an Apache Arrow and computes a "Group By" operation, returning a new Arrow.

python
from perspective import Server

client = Server().new_local_client()
table = client.table(arrow_bytes_data)
view = table.view(group_by = ["CounterParty", "Security"])
arrow = view.to_arrow()

More Examples are available on GitHub.

What's included

The perspective module exports several tools:

  • Server the constructor for a new instance of the Perspective data engine.
  • The perspective.widget module exports PerspectiveWidget, the JupyterLab widget for interactive visualization in a notebook cell.
  • The perspective.handlers modules exports web frameworks handlers that interface with a perspective-client in JavaScript.
    • perspective.handlers.tornado.PerspectiveTornadoHandler for Tornado
    • perspective.handlers.starlette.PerspectiveStarletteHandler for Starlette and FastAPI
    • perspective.handlers.aiohttp.PerspectiveAIOHTTPHandler for AIOHTTP,

Virtual UI server

As <perspective-viewer> or any other Perspective Client will only consume the data necessary to render the current screen (or whatever else was requested via the API), this runtime mode allows large datasets without the need to copy them entirely to the Browser, at the expense of network latency on UI interaction/API calls.

Notebooks

PerspectiveWidget is an AnyWidget that implements the same API as <perspective-viewer>, and runs such a viewer in either server or client (via WebAssembly) mode.

The widget is bundled entirely inside the perspective-python wheel, so there is no per-host extension to install. It runs identically in JupyterLab, classic Jupyter Notebook, VSCode notebooks, Google Colab and Marimo. Install the jupyter extra to pull in anywidget:

bash
pip install "perspective-python[jupyter]"

Separately, the optional @perspective-dev/jupyterlab package provides convenient builtin viewers for csv, json, or arrow files in JupyterLab. With it installed, right-click a file of one of these types and choose the appropriate Perspective option from the context menu.