Skip to content

Commit 1a13e1f

Browse files
authored
Refactor fix thumbnails. (#347)
* Rename gallery_generator to thumbnail_extractor. Add debug output. Write thumbnails to separate directory. * Fix relative path issues. * Fix path issues. * Add _thumbnails to .gitignore.
1 parent ff0156d commit 1a13e1f

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
_build
77
jupyter_execute
8+
_thumbnails

_thumbnails/thumbnails/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Thumbnails will automatically be generated an placed in this directory by the thumbnail_extractor.py script.

examples/conf.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"sphinx_codeautolink",
2626
"notfound.extension",
2727
"sphinx_gallery.load_style",
28-
"gallery_generator",
28+
"thumbnail_extractor",
2929
]
3030

3131
# List of patterns, relative to source directory, that match files and
@@ -54,13 +54,13 @@ def hack_nbsphinx(app: Sphinx) -> None:
5454

5555
from glob import glob
5656

57-
nb_paths = glob("examples/*/*.ipynb")
57+
nb_paths = glob("*/*.ipynb")
5858
nbsphinx_thumbnails = {}
5959
for nb_path in nb_paths:
60-
png_file = os.path.join("_static", os.path.splitext(os.path.split(nb_path)[-1])[0] + ".png")
61-
nb_path_rel = os.path.splitext(
62-
os.path.join(*os.path.normpath(nb_path).split(os.path.sep)[1:])
63-
)[0]
60+
png_file = os.path.join(
61+
"thumbnails", os.path.splitext(os.path.split(nb_path)[-1])[0] + ".png"
62+
)
63+
nb_path_rel = os.path.splitext(nb_path)[0]
6464
nbsphinx_thumbnails[nb_path_rel] = png_file
6565

6666
def builder_inited(app: Sphinx):
@@ -145,7 +145,8 @@ def setup(app: Sphinx):
145145
# Add any paths that contain custom static files (such as style sheets) here,
146146
# relative to this directory. They are copied after the builtin static files,
147147
# so a file named "default.css" will overwrite the builtin "default.css".
148-
html_static_path = ["../_static", "../_images", "../_templates"]
148+
html_static_path = ["../_static"]
149+
html_extra_path = ["../_thumbnails"]
149150
html_css_files = ["custom.css"]
150151
templates_path = ["../_templates"]
151152
html_sidebars = {

sphinxext/gallery_generator.py renamed to sphinxext/thumbnail_extractor.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515

1616
from matplotlib import image
1717

18-
DOC_SRC = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19-
DEFAULT_IMG_LOC = os.path.join(os.path.dirname(DOC_SRC), "pymc-examples/_static", "PyMC.png")
18+
import sphinx
19+
20+
logger = sphinx.util.logging.getLogger(__name__)
21+
22+
DOC_SRC = os.path.dirname(os.path.abspath(__file__))
23+
DEFAULT_IMG_LOC = os.path.join(os.path.dirname(DOC_SRC), "_static", "PyMC.png")
2024

2125

2226
def create_thumbnail(infile, width=275, height=275, cx=0.5, cy=0.5, border=4):
@@ -52,7 +56,7 @@ class NotebookGenerator:
5256
def __init__(self, filename, target_dir):
5357
self.basename = os.path.basename(filename)
5458
stripped_name = os.path.splitext(self.basename)[0]
55-
self.image_dir = os.path.join(target_dir, "_static")
59+
self.image_dir = os.path.join(target_dir, "_thumbnails", "thumbnails")
5660
self.png_path = os.path.join(self.image_dir, f"{stripped_name}.png")
5761
with open(filename) as fid:
5862
self.json_source = json.load(fid)
@@ -75,18 +79,21 @@ def gen_previews(self):
7579
with open(self.png_path, "wb") as buff:
7680
buff.write(preview)
7781
else:
78-
print(f"didn't find for {self.png_path}")
82+
logger.warning(
83+
f"Didn't find any pictures in {self.basename}", type="thumbnail_extractor"
84+
)
7985
shutil.copy(self.default_image_loc, self.png_path)
8086
create_thumbnail(self.png_path)
8187

8288

8389
def main(app):
90+
logger.info("Starting thumbnail extractor.")
8491
from glob import glob
8592

86-
nb_paths = glob("examples/*/*.ipynb")
93+
nb_paths = glob("*/*.ipynb")
8794

8895
for nb_path in nb_paths:
89-
nbg = NotebookGenerator(nb_path, "")
96+
nbg = NotebookGenerator(nb_path, "..")
9097
nbg.gen_previews()
9198

9299

0 commit comments

Comments
 (0)