Skip to content

Commit 50575af

Browse files
committed
Use qtgallery for sphinx gallery
1 parent e9730ce commit 50575af

File tree

6 files changed

+13
-26
lines changed

6 files changed

+13
-26
lines changed

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# import os
1414
# import sys
1515
# sys.path.insert(0, os.path.abspath('.'))
16-
16+
import qtgallery
1717

1818
# -- Project information -----------------------------------------------------
1919

@@ -31,6 +31,8 @@
3131

3232
sphinx_gallery_conf = {
3333
"filename_pattern": ".",
34+
"image_scrapers": (qtgallery.qtscraper,),
35+
"reset_modules": (qtgallery.reset_qapp,),
3436
}
3537

3638
# Add any paths that contain templates here, relative to this directory.

examples/histogram.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import napari
66
from skimage import data
77

8-
from napari_matplotlib import HistogramWidget
9-
108
viewer = napari.Viewer()
119
viewer.add_image(data.brain())
1210

13-
widget = HistogramWidget(viewer)
14-
widget
11+
viewer.window.add_plugin_dock_widget(
12+
plugin_name="napari-matplotlib", widget_name="Histogram"
13+
)
14+
15+
if __name__ == "__main__":
16+
napari.run()

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ napari.manifest =
5050
[options.extras_require]
5151
docs =
5252
pydata-sphinx-theme
53+
qtgallery
5354
sphinx
5455
sphinx-gallery
5556
testing =

src/napari_matplotlib/_widget.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from base64 import b64encode
2-
from io import BytesIO
3-
41
import napari
52
import numpy as np
63
from matplotlib.backends.backend_qt5agg import FigureCanvas
@@ -10,15 +7,6 @@
107
__all__ = ["HistogramWidget"]
118

129

13-
def _figure_to_base64(fig):
14-
# Converts a matplotlib Figure to a base64 UTF-8 string
15-
buf = BytesIO()
16-
fig.savefig(
17-
buf, format="png", facecolor="none"
18-
) # works better than transparent=True
19-
return b64encode(buf.getvalue()).decode("utf-8")
20-
21-
2210
class NapariMPLWidget(QWidget):
2311
"""
2412
Attributes
@@ -37,19 +25,13 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
3725
super().__init__()
3826

3927
self.viewer = napari_viewer
40-
self.figure = Figure(figsize=(5, 3))
28+
self.figure = Figure(figsize=(5, 3), tight_layout=True)
4129
self.canvas = FigureCanvas(self.figure)
4230
self.axes = self.canvas.figure.subplots()
4331

4432
self.setLayout(QVBoxLayout())
4533
self.layout().addWidget(self.canvas)
4634

47-
def _repr_html_(self):
48-
buf = BytesIO()
49-
self.figure.savefig(buf, format="png", facecolor="none")
50-
src = b64encode(buf.getvalue()).decode("utf-8")
51-
return f"<img src='data:image/png;base64,{src}'/>"
52-
5335

5436
class HistogramWidget(NapariMPLWidget):
5537
"""

src/napari_matplotlib/napari.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ contributions:
88

99
widgets:
1010
- command: napari-matplotlib.histogram
11-
display_name: Matplotlib histogram
11+
display_name: Histogram

test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
viewer = napari.Viewer()
44
viewer.open_sample("napari", "kidney")
55
viewer.window.add_plugin_dock_widget(
6-
plugin_name="napari-matplotlib", widget_name="Matplotlib histogram"
6+
plugin_name="napari-matplotlib", widget_name="Histogram"
77
)
88

99
if __name__ == "__main__":

0 commit comments

Comments
 (0)