Skip to content

Commit

Permalink
add option for zip downloads (#1299)
Browse files Browse the repository at this point in the history
Co-authored-by: Jamie Cook <jamie@outerloop.io>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
  • Loading branch information
4 people committed May 24, 2024
1 parent d6f42b2 commit d240382
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
12 changes: 2 additions & 10 deletions sphinx_gallery/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# License: 3-clause BSD

import os
import zipfile

from .utils import _replace_md5
from .utils import zip_files


CODE_ZIP_DOWNLOAD = """
Expand Down Expand Up @@ -53,14 +52,7 @@ def python_zip(file_list, gallery_path, extension=".py"):
elif extension == ".ipynb":
zipname += "_jupyter"
zipname = os.path.join(gallery_path, zipname + ".zip")
zipname_new = zipname + ".new"
with zipfile.ZipFile(zipname_new, mode="w") as zipf:
for fname in file_list:
if extension is not None:
fname = os.path.splitext(fname)[0] + extension
zipf.write(fname, os.path.relpath(fname, gallery_path))
_replace_md5(zipname_new)
return zipname
return zip_files(file_list, zipname, gallery_path, extension)


def list_downloadable_sources(target_dir, extensions=(".py",)):
Expand Down
16 changes: 15 additions & 1 deletion sphinx_gallery/gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from .utils import (
scale_image,
get_md5sum,
zip_files,
_replace_md5,
optipng,
status_iterator,
Expand Down Expand Up @@ -234,6 +235,12 @@ def __exit__(self, type_, value, tb):
:download:`Download Jupyter notebook: {0} <{0}>`
"""

ZIP_DOWNLOAD = """
.. container:: sphx-glr-download sphx-glr-download-zip
:download:`Download both (zipped): {0} <{0}>`
"""

RECOMMENDATIONS_INCLUDE = """\n
.. include:: {0}.recommendations
"""
Expand Down Expand Up @@ -1263,12 +1270,17 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf, seen_backrefs=No
)

save_thumbnail(image_path_template, src_file, script_vars, file_conf, gallery_conf)
files_to_zip = [target_file]

if target_file.suffix in gallery_conf["notebook_extensions"]:
example_nb = jupyter_notebook(script_blocks, gallery_conf, target_dir)
ipy_fname = target_file.with_suffix(".ipynb.new")
save_notebook(example_nb, ipy_fname)
_replace_md5(ipy_fname, mode="t")
files_to_zip += [target_file.with_suffix(".ipynb")]

# Produce the zip file of all sources
zip_files(files_to_zip, target_file.with_suffix(".zip"), target_dir)

# Write names
if gallery_conf["inspect_global_variables"]:
Expand Down Expand Up @@ -1479,9 +1491,11 @@ def save_rst_example(
example_rst += jupyterlite_rst

if save_notebook:
example_rst += NOTEBOOK_DOWNLOAD.format(example_file.with_suffix(".ipynb").name)
ipynb_download_file = example_file.with_suffix(".ipynb").name
example_rst += NOTEBOOK_DOWNLOAD.format(ipynb_download_file)

example_rst += CODE_DOWNLOAD.format(example_file.name, language)
example_rst += ZIP_DOWNLOAD.format(example_file.with_suffix(".zip").name)

if gallery_conf["recommender"]["enable"]:
# extract the filename without the extension
Expand Down
19 changes: 19 additions & 0 deletions sphinx_gallery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
from shutil import move, copyfile
import subprocess
import zipfile

from sphinx.errors import ExtensionError
import sphinx.util
Expand Down Expand Up @@ -167,6 +168,24 @@ def _replace_md5(fname_new, fname_old=None, method="move", mode="b"):
assert os.path.isfile(fname_old)


def zip_files(file_list, zipname, relative_to, extension=None):
"""
Creates a zip file with the given files.
A zip file named `zipname` will be created containing the files listed in
`file_list`. The zip file contents will be stored with their paths stripped to be
relative to `relative_to`.
"""
zipname_new = str(zipname) + ".new"
with zipfile.ZipFile(zipname_new, mode="w") as zipf:
for fname in file_list:
if extension is not None:
fname = os.path.splitext(fname)[0] + extension
zipf.write(fname, os.path.relpath(fname, relative_to))
_replace_md5(zipname_new)
return zipname


def _has_pypandoc():
"""Check if pypandoc package available."""
try:
Expand Down

0 comments on commit d240382

Please sign in to comment.