Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[WIP] Expose examples sortkey #278

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def setup(app):
'matplotlib': ('https://matplotlib.org/', None),
}

from sphinx_gallery.sorting import ExplicitOrder
from sphinx_gallery.sorting import ExplicitOrder, amount_of_code
examples_dirs = ['../examples', '../tutorials']
gallery_dirs = ['auto_examples', 'tutorials']

Expand Down Expand Up @@ -330,6 +330,7 @@ def setup(app):
'subsection_order': ExplicitOrder(['../examples/sin_func',
'../examples/no_output',
'../tutorials/seaborn']),
'within_subsection_order': amount_of_code,
'find_mayavi_figures': find_mayavi_figures,
'expected_failing_examples': ['../examples/no_output/plot_raise.py',
'../examples/no_output/plot_syntaxerror.py']
Expand Down
1 change: 1 addition & 0 deletions sphinx_gallery/gen_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'filename_pattern': re.escape(os.sep) + 'plot',
'examples_dirs': os.path.join('..', 'examples'),
'subsection_order': None,
'within_subsection_order': None,
'gallery_dirs': 'auto_examples',
'backreferences_dir': None,
'doc_module': (),
Expand Down
25 changes: 9 additions & 16 deletions sphinx_gallery/gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,10 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs):

if not os.path.exists(target_dir):
os.makedirs(target_dir)
sorted_listdir = [fname for fname in sorted(os.listdir(src_dir))
if fname.endswith('.py')]
listdir = [fname for fname in os.listdir(src_dir)
if fname.endswith('.py')]
sorted_listdir = sorted(
listdir, key=gallery_conf['within_subsection_order'](src_dir))
entries_text = []
computation_times = []
build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir'])
Expand All @@ -385,29 +387,25 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs):
'Generating gallery for %s ' % build_target_dir,
length=len(sorted_listdir))
for fname in iterator:
intro, amount_of_code, time_elapsed = generate_file_rst(
intro, time_elapsed = generate_file_rst(
fname,
target_dir,
src_dir,
gallery_conf)
computation_times.append((time_elapsed, fname))
new_fname = os.path.join(src_dir, fname)
this_entry = _thumbnail_div(build_target_dir, fname, intro) + """

.. toctree::
:hidden:

/%s\n""" % os.path.join(build_target_dir, fname[:-3]).replace(os.sep, '/')
entries_text.append((amount_of_code, this_entry))
entries_text.append(this_entry)

if gallery_conf['backreferences_dir']:
write_backreferences(seen_backrefs, gallery_conf,
target_dir, fname, intro)

# sort to have the smallest entries in the beginning
entries_text.sort()

for _, entry_text in entries_text:
for entry_text in entries_text:
fhindex += entry_text

# clear at the end of the section
Expand Down Expand Up @@ -531,8 +529,6 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
-------
intro: str
The introduction of the example
amount_of_code : int
character count of the corresponding python script in file
time_elapsed : float
seconds required to run the script
"""
Expand All @@ -541,13 +537,10 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
example_file = os.path.join(target_dir, fname)
shutil.copyfile(src_file, example_file)
file_conf, script_blocks = split_code_and_text_blocks(src_file)
amount_of_code = sum([len(bcontent)
for blabel, bcontent, lineno in script_blocks
if blabel == 'code'])
intro = extract_intro(fname, script_blocks[0][1])

if md5sum_is_current(example_file):
return intro, amount_of_code, 0
return intro, 0

image_dir = os.path.join(target_dir, 'images')
if not os.path.exists(image_dir):
Expand Down Expand Up @@ -646,4 +639,4 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
if block_vars['execute_script']:
logger.debug("%s ran in : %.2g seconds", src_file, time_elapsed)

return intro, amount_of_code, time_elapsed
return intro, time_elapsed
22 changes: 22 additions & 0 deletions sphinx_gallery/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import types

from .py_source_parser import split_code_and_text_blocks


class ExplicitOrder(object):
"""Sorting key for all galleries subsections
Expand Down Expand Up @@ -46,3 +48,23 @@ def __call__(self, item):
raise ValueError('If you use an explicit folder ordering, you '
'must specify all folders. Explicit order not '
'found for {}'.format(item))


def amount_of_code(src_dir):
"""Sort examples in src_dir by amount of code """
def sort_files(filename):
src_file = os.path.normpath(os.path.join(src_dir, filename))
file_conf, script_blocks = split_code_and_text_blocks(src_file)
amount_of_code = sum([len(bcontent)
for blabel, bcontent, lineno in script_blocks
if blabel == 'code'])
return amount_of_code
return sort_files


def file_size(src_dir):
"""Sort examples in src_dir by file size"""
def sort_files(filename):
src_file = os.path.normpath(os.path.join(src_dir, filename))
return os.stat(src_file).st_size
return sort_files