-
Notifications
You must be signed in to change notification settings - Fork 649
Description
Summary
This feature request proposes adding a Fastplotlib component to Preswald to support high-performance scientific plotting. Fastplotlib is a plotting library built on pygfx, which utilizes modern GPU APIs (Vulkan, DX12, Metal) via WGPU for fast rendering.
This integration will enable users to incorporate interactive, high-performance plots into Preswald applications.
Why This is Important
- Performance: Fastplotlib is optimized for large-scale, real-time visualizations.
- Expressiveness: It provides an easy-to-use API for complex scientific plotting.
- Compatibility: Fits well within Preswald’s interactive data app framework.
Implementation Plan
1. Modify Backend (components.py)
Add a new function in preswald/preswald/interfaces/components.py to define the Fastplotlib component:
import hashlib
from typing import Optional, Any, Dict
from preswald.service import PreswaldService
def fastplotlib(
label: str,
data: Any,
size: float = 1.0,
config: Optional[Dict] = None
) -> str:
"""Create a Fastplotlib component with a unique ID"""
service = PreswaldService.get_instance()
# Generate a consistent ID for the component
component_id = f"fastplotlib-{hashlib.md5(label.encode()).hexdigest()[:8]}"
component = {
"type": "fastplotlib_component",
"id": component_id,
"label": label,
"data": data, # Data for plotting
"size": size,
"config": config or {}, # Optional configuration
}
service.append_component(component)
return component_id2. Register the Component in __init__.py
Modify preswald/preswald/interfaces/__init__.py to make fastplotlib available:
from .components import fastplotlib3. Create the Frontend Component
Create a new file:
preswald/frontend/src/components/widgets/FastplotlibWidget.jsx
import React, { useEffect, useRef } from 'react';
import { Card } from '@/components/ui/card';
import { cn } from '@/lib/utils';
// Import Fastplotlib
import { figure } from 'fastplotlib';
const FastplotlibWidget = ({ _label, data, className, config }) => {
const plotRef = useRef(null);
useEffect(() => {
if (plotRef.current) {
const fig = figure();
plotRef.current.appendChild(fig.canvas);
// Assume data is passed as a structured array or object
fig.plot(data, config);
}
}, [data, config]);
return (
<Card className={cn('w-full', className)}>
<div ref={plotRef} />
</Card>
);
};
export default FastplotlibWidget;4. Register in DynamicComponents.jsx
Modify preswald/frontend/src/components/DynamicComponents.jsx to include the new component:
import FastplotlibWidget from '@/components/widgets/FastplotlibWidget';
case 'fastplotlib_component':
return (
<FastplotlibWidget
{...commonProps}
data={component.data}
config={component.config}
className={component.className}
/>
);5. Test the Integration
Modify examples/iris/hello.py to test the new component:
from preswald import fastplotlib
import numpy as np
# Generate some test data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Call the component
fastplotlib(label="Sine Wave", data={"x": x.tolist(), "y": y.tolist()})Run the example:
cd examples/iris
preswald runExpected result: The Fastplotlib component should render an interactive sine wave plot.