Getting a Range Slider on napari

Hi,

I’m trying to get a range slider working as a widget on napari. Since it is not available on magicgui, my best guess was to adapt the RangeSlider from matplotlib (e.g. from this example). The code I tried is the following, but unfortunately the slider remains static:

import napari
from qtpy import QtWidgets
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import RangeSlider

def return_mpl_widget():
    mpl_widget = FigureCanvas(Figure(figsize=(2, 3)))
    ax = mpl_widget.figure.subplots()
    RangeSlider(ax, "Threshold", 0, 10)
    return mpl_widget 

with napari.gui_qt():
    viewer = napari.Viewer()
    
    #add mpl widget
    mpl_widget =return_mpl_widget()
    viewer.window.add_dock_widget(mpl_widget)

I confess I’m not that familiar with matplotlib api so maybe there’s a way to instantiate an ax object that is not static?

Otherwise, I tried using the Plotly slider with no luck either. The ipywidgets sliders look really nice as well, but I believe since they are web-oriented as well, it wouldn’t be trivial to incorporate them in napari either (?).

we have a range slider that you can use in napari (and do plan on adding one to magicgui soon hopefully):

import napari
from napari._qt.widgets.qt_range_slider import QHRangeSlider

viewer = napari.Viewer()
slider = QHRangeSlider(initial_values=(1, 8), data_range=(0, 10))
viewer.window.add_dock_widget(slider)
napari.run()

(see here for available methods)

3 Likes

Wow, thank you so much, it’s exactly what I needed! I feel a bit dumb for asking now, is this slider referenced anywhere else than in the source code?

no, so don’t feel dumb :joy:
there is still a lot of functionality that is unfortunately not well (or not at all) documented at napari.org … but Qt stuff specifically is not something that we have made a big effort to document (since it’s changing often and more used for “app” development than for public API). I think magicgui is probably a better place for widgets like this in the long run, so we’ll try to get on that!

2 Likes

The need for a good Qt range-slider for python comes up so often… so I worked on a better solution for this over the last couple days and released it as an independent package if you’re interested in trying it out:

7 Likes

Wow thank you so much, that’s really great :grinning: