Skip to main content

Interactive napari viewer inside Google Colab via noVNC

Project description

napari-colab

                         _  ____      _       _
 _ __   __ _ _ __   __ _| |/ ___|___ | | __ _| |__
| '_ \ / _` | '_ \ / _` | | |   / _ \| |/ _` | '_ \
| | | | (_| | |_) | (_| | | |__| (_) | | (_| | |_) |
|_| |_|\__,_| .__/ \__,_|_|\____\___/|_|\__,_|_.__/
             |_|

napari :heart: Colab — because microscopy images deserve a real viewer, even in the cloud.

Interactive napari viewer inside Google Colab via noVNC.

Warning: This package is designed exclusively for Google Colab. It relies on root access via apt-get, a virtual framebuffer (Xvfb), and the Colab proxy to tunnel ports to your browser. It will not work on local machines, JupyterHub, or other hosted notebook services. If you can run napari locally, you should — it will be faster and more responsive.

Installation

pip install napari-colab

Note: the system packages (xvfb, x11vnc, scrot, xcb libraries) are installed automatically by setup() on first run via apt-get.

Quick start

# Cell 1 — once per session
from napari_colab import setup, open_viewer, screenshot, shutdown

# Cell 2 — launch viewer (URL printed here)
viewer = open_viewer(width=1800, height=1000)

# Cell 3 — load your data
from pathlib import Path
images = sorted(Path('my_images/').glob('*.tiff'))
labels = sorted(Path('my_labels/').glob('*.tiff'))

viewer.open(images, stack=True, name='raw')
viewer.open(labels, stack=True, layer_type='labels', name='segmentation')

# Cell 4 — optional extras
viewer.dims.ndisplay = 3
viewer.camera.angles = (30, 45, 0)
screenshot()   # inline preview

API

Function Description
setup() Install deps, start Xvfb + x11vnc + noVNC
open_viewer(width, height) Launch napari subprocess, return ViewerProxy
screenshot() Capture virtual display inline in notebook
shutdown() Kill napari and display helpers

ViewerProxy

viewer.open(paths, stack=True, name='layer', layer_type='image'|'labels')
viewer.add_image(np_array, name='...', colormap='...', blending='...')
viewer.add_labels(np_array, name='...')
viewer.dims.ndisplay = 2 | 3
viewer.camera.angles = (rx, ry, rz)
viewer.camera.zoom   = 1.5
viewer.reset_view()
viewer.window.resize(w, h)

How it works

Xvfb (:99)   <- napari + Qt render here (software OpenGL / Mesa)
    |
x11vnc       <- captures framebuffer -> VNC (port 5900)
    |
websockify   <- VNC -> WebSocket bridge (port 6080)
    |
noVNC        <- web client at /vnc.html
    |
Colab proxy  <- tunnels port 6080 to your browser tab

napari runs in its own subprocess so the Colab kernel never crashes. Commands (open, add_image, camera, etc.) are forwarded via a JSON-over-TCP socket. A QTimer drains the command queue every 200 ms on the Qt main thread — the only thread allowed to touch Qt objects.

Troubleshooting

OpenCV Qt conflict (Cellpose and similar packages)

Packages like Cellpose pull in opencv-python, which bundles its own Qt plugins. These conflict with the Qt used by napari, causing a fatal xcb plugin error:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb"

napari-colab depends on opencv-python-headless (which is functionally identical but Qt-free) to avoid this. However, if you install a package that depends on opencv-python after napari-colab, pip may re-install the full OpenCV and reintroduce the conflict.

Fix:

pip uninstall -y opencv-python && pip install opencv-python-headless

Prevention: install OpenCV-dependent packages (cellpose, etc.) before napari-colab so that the headless variant wins:

pip install cellpose
pip install napari-colab

License

MIT

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

napari_colab-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

napari_colab-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file napari_colab-0.1.1.tar.gz.

File metadata

  • Download URL: napari_colab-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for napari_colab-0.1.1.tar.gz
Algorithm Hash digest
SHA256 83fc2ed8c2d20e9712c41b587dd6ff1c6f3ce0a6161869d19fc6465dec6bd71a
MD5 231f71f034d5f5eb5466f9973c489acf
BLAKE2b-256 d1fa5eb169feea76ec5418076506f6a5479b1d98f9c286b932a02c37eb26d4ec

See more details on using hashes here.

File details

Details for the file napari_colab-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: napari_colab-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for napari_colab-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e96a6cf9b501eb271a6a906431a49f9eacb2acc439ba0b2542b828a9e2fb05bd
MD5 68dc229e428c9c0ff651e0203657d923
BLAKE2b-256 73c8111d18b615677982e0c71c7f1e30f061ddc8b2a812384b5b895db1a5988c

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