Skip to content

Refactor fix thumbnails. #347

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

Merged
merged 4 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions _thumbnails/thumbnails/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Thumbnails will automatically be generated an placed in this directory by the thumbnail_extractor.py script.
15 changes: 8 additions & 7 deletions examples/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"sphinx_codeautolink",
"notfound.extension",
"sphinx_gallery.load_style",
"gallery_generator",
"thumbnail_extractor",
]

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

from glob import glob

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

def builder_inited(app: Sphinx):
Expand Down Expand Up @@ -145,7 +145,8 @@ def setup(app: Sphinx):
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["../_static", "../_images", "../_templates"]
html_static_path = ["../_static"]
html_extra_path = ["../_thumbnails"]
html_css_files = ["custom.css"]
templates_path = ["../_templates"]
html_sidebars = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

from matplotlib import image

DOC_SRC = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_IMG_LOC = os.path.join(os.path.dirname(DOC_SRC), "pymc-examples/_static", "PyMC.png")
import sphinx

logger = sphinx.util.logging.getLogger(__name__)

DOC_SRC = os.path.dirname(os.path.abspath(__file__))
DEFAULT_IMG_LOC = os.path.join(os.path.dirname(DOC_SRC), "_static", "PyMC.png")


def create_thumbnail(infile, width=275, height=275, cx=0.5, cy=0.5, border=4):
Expand Down Expand Up @@ -52,7 +56,7 @@ class NotebookGenerator:
def __init__(self, filename, target_dir):
self.basename = os.path.basename(filename)
stripped_name = os.path.splitext(self.basename)[0]
self.image_dir = os.path.join(target_dir, "_static")
self.image_dir = os.path.join(target_dir, "_thumbnails", "thumbnails")
self.png_path = os.path.join(self.image_dir, f"{stripped_name}.png")
with open(filename) as fid:
self.json_source = json.load(fid)
Expand All @@ -75,18 +79,21 @@ def gen_previews(self):
with open(self.png_path, "wb") as buff:
buff.write(preview)
else:
print(f"didn't find for {self.png_path}")
logger.warning(
f"Didn't find any pictures in {self.basename}", type="thumbnail_extractor"
)
shutil.copy(self.default_image_loc, self.png_path)
create_thumbnail(self.png_path)


def main(app):
logger.info("Starting thumbnail extractor.")
from glob import glob

nb_paths = glob("examples/*/*.ipynb")
nb_paths = glob("*/*.ipynb")

for nb_path in nb_paths:
nbg = NotebookGenerator(nb_path, "")
nbg = NotebookGenerator(nb_path, "..")
nbg.gen_previews()


Expand Down