Skip to content

Commit e9730ce

Browse files
committed
Working sphinx gallery
1 parent e1b78ca commit e9730ce

File tree

7 files changed

+84
-28
lines changed

7 files changed

+84
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ instance/
5959

6060
# Sphinx documentation
6161
docs/_build/
62+
docs/auto_examples
6263

6364
# MkDocs documentation
6465
/site/

docs/conf.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,37 @@
1717

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

20-
project = 'matplotlib-napari'
21-
copyright = '2022, David Stansby'
22-
author = 'David Stansby'
20+
project = "matplotlib-napari"
21+
copyright = "2022, David Stansby"
22+
author = "David Stansby"
2323

2424

2525
# -- General configuration ---------------------------------------------------
2626

2727
# Add any Sphinx extension module names here, as strings. They can be
2828
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
2929
# ones.
30-
extensions = [
31-
]
30+
extensions = ["sphinx_gallery.gen_gallery"]
31+
32+
sphinx_gallery_conf = {
33+
"filename_pattern": ".",
34+
}
3235

3336
# Add any paths that contain templates here, relative to this directory.
34-
templates_path = ['_templates']
37+
templates_path = ["_templates"]
3538

3639
# List of patterns, relative to source directory, that match files and
3740
# directories to ignore when looking for source files.
3841
# This pattern also affects html_static_path and html_extra_path.
39-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
42+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
4043

4144

4245
# -- Options for HTML output -------------------------------------------------
4346

4447
# The theme to use for HTML and HTML Help pages. See the documentation for
4548
# a list of builtin themes.
4649
#
47-
html_theme = 'pydata_sphinx_theme'
50+
html_theme = "pydata_sphinx_theme"
4851

4952
# Add any paths that contain custom static files (such as style sheets) here,
5053
# relative to this directory. They are copied after the builtin static files,

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Welcome to matplotlib-napari's documentation!
1010
:maxdepth: 2
1111
:caption: Contents:
1212

13+
auto_examples/index
14+
1315

1416

1517
Indices and tables

examples/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Examples
2+
========

examples/histogram.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Histograms
3+
==========
4+
"""
5+
import napari
6+
from skimage import data
7+
8+
from napari_matplotlib import HistogramWidget
9+
10+
viewer = napari.Viewer()
11+
viewer.add_image(data.brain())
12+
13+
widget = HistogramWidget(viewer)
14+
widget

setup.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ project_urls =
3131
packages = find:
3232
install_requires =
3333
matplotlib
34-
numpy
3534
napari
35+
numpy
3636
python_requires = >=3.8
3737
include_package_data = True
3838
package_dir =
@@ -50,13 +50,15 @@ napari.manifest =
5050
[options.extras_require]
5151
docs =
5252
pydata-sphinx-theme
53+
sphinx
54+
sphinx-gallery
5355
testing =
5456
napari[pyqt5]
5557
pytest
5658
pytest-cov
5759
pytest-qt
58-
pytest-xvfb ; sys_platform == 'linux'
5960
tox
61+
pytest-xvfb;sys_platform == 'linux'
6062

6163
[options.package_data]
6264
* = *.yaml

src/napari_matplotlib/_widget.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from base64 import b64encode
2+
from io import BytesIO
3+
14
import napari
25
import numpy as np
36
from matplotlib.backends.backend_qt5agg import FigureCanvas
@@ -7,32 +10,61 @@
710
__all__ = ["HistogramWidget"]
811

912

10-
class HistogramWidget(QWidget):
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+
22+
class NapariMPLWidget(QWidget):
23+
"""
24+
Attributes
25+
----------
26+
viewer : napari.viewer.Viewer
27+
Main napari viewer.
28+
figure : matplotlib.figure.Figure
29+
Matplotlib figure.
30+
canvas : matplotlib.backends.backend_qt5agg.FigureCanvas
31+
Matplotlib canvas.
32+
axes : matplotlib.axes.Axes
33+
Matplotlib axes.
34+
"""
35+
1136
def __init__(self, napari_viewer: napari.viewer.Viewer):
12-
"""
13-
Widget to display a histogram of the currently selected layer.
14-
15-
Attributes
16-
----------
17-
viewer : napari.viewer.Viewer
18-
Main napari viewer.
19-
layer : napari.layers.Layer
20-
Current layer being histogrammed.
21-
canvas : matplotlib.backends.backend_qt5agg.FigureCanvas
22-
Matplotlib canvas.
23-
axes : matplotlib.axes.Axes
24-
Matplotlib axes.
25-
"""
2637
super().__init__()
27-
self.viewer = napari_viewer
28-
self.layer = self.viewer.layers[-1]
2938

30-
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
39+
self.viewer = napari_viewer
40+
self.figure = Figure(figsize=(5, 3))
41+
self.canvas = FigureCanvas(self.figure)
3142
self.axes = self.canvas.figure.subplots()
3243

3344
self.setLayout(QVBoxLayout())
3445
self.layout().addWidget(self.canvas)
3546

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+
53+
54+
class HistogramWidget(NapariMPLWidget):
55+
"""
56+
Widget to display a histogram of the currently selected layer.
57+
58+
Attributes
59+
----------
60+
layer : napari.layers.Layer
61+
Current layer being histogrammed.
62+
"""
63+
64+
def __init__(self, napari_viewer: napari.viewer.Viewer):
65+
super().__init__(napari_viewer)
66+
self.layer = self.viewer.layers[-1]
67+
3668
self.viewer.dims.events.current_step.connect(self.hist_current_layer)
3769
self.viewer.layers.selection.events.active.connect(self.update_layer)
3870

0 commit comments

Comments
 (0)