docs/md/explanation/python.md
perspective-pythonPerspective 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.
A simple example which loads an Apache Arrow and computes a "Group By" operation, returning a new Arrow.
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.
The perspective module exports several tools:
Server the constructor for a new instance of the Perspective data engine.perspective.widget module exports PerspectiveWidget, the JupyterLab
widget for interactive visualization in a notebook cell.perspective.handlers modules exports web frameworks handlers that
interface with a perspective-client in JavaScript.
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.
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:
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.