Menu

[r8520]: / branches / mathtex / doc / sphinxext / gen_gallery.py  Maximize  Restore  History

Download this file

104 lines (81 with data), 3.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
# generate a thumbnail gallery of examples
template = """\
{%% extends "layout.html" %%}
{%% set title = "Thumbnail gallery" %%}
{%% block body %%}
<h3>Click on any image to see full size image and source code</h3>
<br/>
%s
{%% endblock %%}
"""
import os, glob, re, sys, warnings
import matplotlib.image as image
multiimage = re.compile('(.*)_\d\d')
def out_of_date(original, derived):
return (not os.path.exists(derived) or
os.stat(derived).st_mtime < os.stat(original).st_mtime)
def gen_gallery(app, doctree):
if app.builder.name != 'html':
return
outdir = app.builder.outdir
rootdir = 'plot_directive/mpl_examples'
# images we want to skip for the gallery because they are an unusual
# size that doesn't layout well in a table, or because they may be
# redundant with other images or uninteresting
skips = set([
'mathtext_examples',
'matshow_02',
'matshow_03',
'matplotlib_icon',
])
print
print "generating gallery: ",
data = []
for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' ):
origdir = os.path.join('build', rootdir, subdir)
thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
if not os.path.exists(thumbdir):
os.makedirs(thumbdir)
print subdir,
for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
if filename.endswith("hires.png"):
continue
path, filename = os.path.split(filename)
basename, ext = os.path.splitext(filename)
if basename in skips:
sys.stdout.write('[skipping %s]' % basename)
sys.stdout.flush()
continue
# Create thumbnails based on images in tmpdir, and place
# them within the build tree
orig_path = str(os.path.join(origdir, filename))
thumb_path = str(os.path.join(thumbdir, filename))
if out_of_date(orig_path, thumb_path):
image.thumbnail(orig_path, thumb_path, scale=0.3)
m = multiimage.match(basename)
if m is None:
pyfile = '%s.py'%basename
else:
basename = m.group(1)
pyfile = '%s.py'%basename
data.append((subdir, basename,
os.path.join(rootdir, subdir, 'thumbnails', filename)))
sys.stdout.write(".")
sys.stdout.flush()
print
link_template = """\
<a href="%s"><img src="%s" border="0" alt="%s"/></a>
"""
if len(data) == 0:
warnings.warn("No thumbnails were found")
rows = []
for (subdir, basename, thumbfile) in data:
if thumbfile is not None:
link = 'examples/%s/%s.html'%(subdir, basename)
rows.append(link_template%(link, thumbfile, basename))
fh = file(os.path.join(app.builder.srcdir, '_templates', 'gallery.html'),
'w')
fh.write(template%'\n'.join(rows))
fh.close()
def setup(app):
app.connect('env-updated', gen_gallery)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.