Skip to main content

A Streamlit component for NiiVue neuroimaging viewer

Project description

NiiVue Streamlit Component

A modern Streamlit component for visualizing neuroimaging data using NiiVue, built with TypeScript, Preact, and Vite.

๐Ÿš€ Quick Start

Simple Installation & Usage

  1. Install the component:

    pip install --index-url https://test.pypi.org/simple/ --no-deps niivue-streamlit
    
  2. Use in your Streamlit app:

    import streamlit as st
    from niivue_component import niivue_viewer
    
    uploaded_file = st.file_uploader("Choose a NIFTI file", type=["nii", "nii.gz"])
    
    if uploaded_file is not None:
        result = niivue_viewer(
            nifti_data=uploaded_file.getvalue(),
            filename=uploaded_file.name,
            height=700
        )
    
        # Handle click events
        if result:
            st.write(f"Clicked voxel: {result['voxel']}, Value: {result['value']}")
    

โœจ Features

  • ๐ŸŽจ Two Component Modes:
    • StyledViewer: Full-featured viewer with interactive menu and controls
    • UnstyledCanvas: Minimal canvas-only viewer for embedding
  • ๐Ÿ”„ Multiple View Modes:
    • Axial, Coronal, Sagittal slices
    • 3D render view
    • Multiplanar view with render
  • ๐Ÿ“Š Advanced Capabilities:
    • Multiple overlay images with custom colormaps
    • Configurable display settings (crosshair, radiological convention, colorbar, interpolation)
    • Bidirectional communication (click events from viewer to Python)
    • DICOM support

๐Ÿ“– Advanced Usage

With Overlays

from niivue_component import niivue_viewer

result = niivue_viewer(
    nifti_data=main_image_bytes,
    filename="brain.nii.gz",
    overlays=[
        {
            "data": overlay_bytes,
            "name": "activation.nii.gz",
            "colormap": "hot",
            "opacity": 0.7
        }
    ],
    view_mode="multiplanar",
    styled=True,
    settings={
        "crosshair": True,
        "radiological": False,
        "colorbar": True,
        "interpolation": True
    },
    height=800
)

Minimal Viewer (No Menu)

# Perfect for embedding in complex layouts
result = niivue_viewer(
    nifti_data=image_bytes,
    filename="scan.nii",
    styled=False,  # Hide menu
    view_mode="axial",
    height=400
)

๐Ÿ“š API Reference

niivue_viewer()

Parameters:

  • nifti_data (bytes, optional): Raw NIFTI file data
  • filename (str): Displayed filename
  • overlays (list[dict], optional): Overlay images list
    • data (bytes): Overlay data
    • name (str): Overlay name
    • colormap (str): Colormap (default: 'red')
    • opacity (float): 0-1 (default: 0.5)
  • height (int): Height in pixels (default: 600)
  • view_mode (str): 'axial', 'coronal', 'sagittal', '3d', 'multiplanar' (default)
  • styled (bool): Show menu (default: True)
  • settings (dict, optional):
    • crosshair (bool): default True
    • radiological (bool): default False
    • colorbar (bool): default False
    • interpolation (bool): default True
  • key (str, optional): Component key

Returns:

dict or None with click event data:

  • type: 'voxel_click'
  • voxel: [x, y, z]
  • mm: [x, y, z]
  • value: float
  • filename: str

๐Ÿ› ๏ธ Development

Frontend Development

Built with modern web technologies:

  • Vite: Fast build tool
  • TypeScript: Type safety
  • Preact: Lightweight React
  • Tailwind CSS: Styling
  • niivue-react: Shared components
cd niivue_component/frontend

# Start dev server (requires backend running)
pnpm dev

# Build for production
pnpm build

Backend Development

Toggle dev/production in __init__.py:

_RELEASE = False  # Dev mode: localhost:3001
_RELEASE = True   # Prod mode: built files

Running Examples

# Simple example
streamlit run app.py

# Advanced example with all features
streamlit run app_advanced.py

๐Ÿ“ Supported Formats

  • NIFTI (.nii, .nii.gz)
  • DICOM (.dcm)
  • MINC (.mnc, .mnc.gz)
  • MHA/MHD
  • NRRD
  • MGH/MGZ

๐Ÿ—๏ธ Architecture

niivue_component/
โ”œโ”€โ”€ __init__.py                 # Python API
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ StyledViewer.tsx
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ UnstyledCanvas.tsx
โ”‚   โ”‚   โ”œโ”€โ”€ types.ts
โ”‚   โ”‚   โ””โ”€โ”€ utils.ts
โ”‚   โ”œโ”€โ”€ vite.config.ts
โ”‚   โ””โ”€โ”€ package.json
โ””โ”€โ”€ build/                      # Compiled assets (generated, not in git)

๐Ÿ”ง Building the Component

The build files are not committed to git. To build the component locally:

cd niivue_component/frontend
pnpm install
pnpm build

This generates the build/ directory with compiled assets that are included in the Python package during distribution.

๐Ÿ“„ License

BSD-2-Clause

๐Ÿ™ Credits

Built on top of:

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

niivue_streamlit-0.2.2b2.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

niivue_streamlit-0.2.2b2-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

Details for the file niivue_streamlit-0.2.2b2.tar.gz.

File metadata

  • Download URL: niivue_streamlit-0.2.2b2.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for niivue_streamlit-0.2.2b2.tar.gz
Algorithm Hash digest
SHA256 6d507f195e5dd04e73a2b85d53a55636522055022de723811b301b68a5e27837
MD5 fcc1a11b3c9047848c13c6737e7ae9eb
BLAKE2b-256 f875125d94030054fc139f9eda3629e9e5c99bac84b0e426f29d4a23bb87daa4

See more details on using hashes here.

File details

Details for the file niivue_streamlit-0.2.2b2-py3-none-any.whl.

File metadata

File hashes

Hashes for niivue_streamlit-0.2.2b2-py3-none-any.whl
Algorithm Hash digest
SHA256 3df4222d49dbc26b54cfb20db134f7b87a7c5044b784fb7327cad6d480c8b547
MD5 4aeeb7d5d8a272f8f47622edca83d362
BLAKE2b-256 6eef5a733ef0044f808625287fef1735944d11831bfe362e3cb67137b5e73fb9

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