Skip to content

[FEATURE] Add Support for Fastplotlib Component in Preswald #467

@amrutha97

Description

@amrutha97

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_id

2. Register the Component in __init__.py

Modify preswald/preswald/interfaces/__init__.py to make fastplotlib available:

from .components import fastplotlib

3. 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 run

Expected result: The Fastplotlib component should render an interactive sine wave plot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions