Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use napari_scraper instead of qtgallery #207

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from pathlib import Path
from urllib.parse import urlparse, urlunparse

import qtgallery
from jinja2.filters import FILTERS
from sphinx_gallery import scrapers
from sphinx_gallery.sorting import ExampleTitleSortKey

import napari
from napari._version import __version_tuple__
Expand Down Expand Up @@ -164,12 +165,12 @@
python_version_range = '3.8–3.10'

myst_substitutions = {
"napari_conda_version": f"`napari={version_string}`",
"napari_version": version_string,
"python_version": python_version,
"python_version_range": python_version_range,
"python_version_code": f"`python={python_version}`",
"conda_create_env": f"```sh\nconda create -y -n napari-env -c conda-forge python={python_version}\nconda activate napari-env\n```",
"napari_conda_version": f"`napari={version_string}`",
"napari_version": version_string,
"python_version": python_version,
"python_version_range": python_version_range,
"python_version_code": f"`python={python_version}`",
"conda_create_env": f"```sh\nconda create -y -n napari-env -c conda-forge python={python_version}\nconda activate napari-env\n```",
}

nb_output_stderr = 'show'
Expand Down Expand Up @@ -197,19 +198,50 @@
napoleon_custom_sections = [('Events', 'params_style')]


def reset_napari_theme(gallery_conf, fname):
def reset_napari(gallery_conf, fname):
from napari.settings import get_settings
from qtpy.QtWidgets import QApplication

settings = get_settings()
settings.appearance.theme = 'dark'
qtgallery.reset_qapp(gallery_conf, fname)

# Disabling `QApplication.exec_` means example scripts can call `exec_`
# (scripts work when run normally) without blocking example execution by
# sphinx-gallery. (from qtgallery)
QApplication.exec_ = lambda _: None


def napari_scraper(block, block_vars, gallery_conf):
"""Basic napari window scraper.

Looks for any QtMainWindow instances and takes a screenshot of them.

`app.processEvents()` allows Qt events to propagateo and prevents hanging.
"""
imgpath_iter = block_vars['image_path_iterator']

if app := napari.qt.get_app():
app.processEvents()
else:
return ""

img_paths = []
for win, img_path in zip(
reversed(napari._qt.qt_main_window._QtMainWindow._instances),
imgpath_iter,
):
img_paths.append(img_path)
win._window.screenshot(img_path, canvas_only=False)

napari.Viewer.close_all()
app.processEvents()

return scrapers.figure_rst(img_paths, gallery_conf['src_dir'])

from sphinx_gallery.sorting import ExampleTitleSortKey

sphinx_gallery_conf = {
#'examples_dirs': '../../napari/examples', # path to your example scripts
# this value is set in the Makefile
# path to your example scripts (this value is set in the Makefile)
# 'examples_dirs': '../../napari/examples',
'gallery_dirs': 'gallery', # path to where to save gallery generated output
'filename_pattern': '/*.py',
'ignore_pattern': 'README.rst|/*_.py',
Expand All @@ -218,8 +250,8 @@ def reset_napari_theme(gallery_conf, fname):
'download_all_examples': False,
'min_reported_time': 10,
'only_warn_on_example_error': True,
'image_scrapers': ("matplotlib", qtgallery.qtscraper,),
'reset_modules': (reset_napari_theme,),
'image_scrapers': ("matplotlib", napari_scraper,),
'reset_modules': (reset_napari,),
'reference_url': {'napari': None},
'within_subsection_order': ExampleTitleSortKey,
}
Expand Down Expand Up @@ -262,7 +294,7 @@ def get_attributes(item, obj, modulename):
'https://napari.zulipchat.com/',
'../_tags',
'https://en.wikipedia.org/wiki/Napari#/media/File:Tabuaeran_Kiribati.jpg',
]
]


def rewrite_github_anchor(app, uri: str):
Expand Down
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ sphinx_autodoc_typehints==1.12.0
myst-nb
napari-sphinx-theme
matplotlib
qtgallery
lxml
imageio-ffmpeg
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ sphinx_autodoc_typehints==1.12.0
myst-nb
napari-sphinx-theme
matplotlib
qtgallery
lxml
imageio-ffmpeg