-
Notifications
You must be signed in to change notification settings - Fork 200
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
Mayavi support #42
Mayavi support #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.. _mayavi-examples-index: | ||
|
||
Gallery of Examples using Mayavi | ||
================================ | ||
|
||
|
||
.. _general_mayavi_examples: | ||
|
||
Mayavi examples | ||
--------------- | ||
|
||
Examples from the sphinx-gallery using Mayavi for embedding 3d plots. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
==================================== | ||
Plotting simple 3D graph with Mayavi | ||
==================================== | ||
|
||
A simple example of the plot of a 3D graph with Mayavi | ||
in order to test the autonomy of the gallery. | ||
""" | ||
|
||
# Code source: Alex Gramfort | ||
# License: BSD 3 clause | ||
|
||
from mayavi import mlab | ||
|
||
mlab.test_plot3d() | ||
|
||
mlab.figure() | ||
mlab.test_contour3d() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,6 +409,8 @@ def embed_code_links(app, exception): | |
"""Embed hyperlinks to documentation into example code""" | ||
if exception is not None: | ||
return | ||
if not app.builder.config.plot_gallery: | ||
return # no need to embed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed that in my previous reviews. Why don't you want to put hyperlinks inside the example in case plot_gallery is False? Is it because it can fail if there is a parsing error of the script ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then could you improve the comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And also because when writing narrative doc you don't want to plot and wait for all the embedding
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@lesteve please take over. I am mostly on my phone these days. It will be
faster if you do a cosmit on your side. Je te donne carte blanche :)
|
||
print('Embedding documentation hyperlinks in examples..') | ||
|
||
if app.builder.name == 'latex': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,7 +225,7 @@ def _plots_are_current(src_file, image_file): | |
return not needs_replot | ||
|
||
|
||
def save_figures(image_path, fig_count): | ||
def save_figures(image_path, fig_count, gallery_conf): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping @Titan-C, do you know off the top of your head if there is a way to access the sphinx application config without having to pass it around as an argument in each function like this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in this case. There is a copy of the config in app.config.sphinxgallery_conf if the function is directly called by sphinx. |
||
"""Save all open matplotlib figures of the example code-block | ||
|
||
Parameters | ||
|
@@ -257,6 +257,22 @@ def save_figures(image_path, fig_count): | |
current_fig = image_path.format(fig_count + fig_mngr.num) | ||
fig.savefig(current_fig, **kwargs) | ||
figure_list.append(current_fig) | ||
|
||
if gallery_conf.get('find_mayavi_figures', False): | ||
from mayavi import mlab | ||
e = mlab.get_engine() | ||
last_matplotlib_fig_num = len(figure_list) | ||
total_fig_num = last_matplotlib_fig_num + len(e.scenes) | ||
mayavi_fig_nums = range(last_matplotlib_fig_num, total_fig_num) | ||
|
||
for scene, mayavi_fig_num in zip(e.scenes, mayavi_fig_nums): | ||
current_fig = image_path.format(mayavi_fig_num) | ||
mlab.savefig(current_fig, figure=scene) | ||
# make sure the image is not too large | ||
scale_image(current_fig, current_fig, 850, 999) | ||
figure_list.append(current_fig) | ||
mlab.close(all=True) | ||
|
||
return figure_list | ||
|
||
|
||
|
@@ -339,7 +355,7 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): | |
sorted_listdir = [fname for fname in sorted(os.listdir(src_dir)) | ||
if fname.endswith('py')] | ||
for fname in sorted_listdir: | ||
generate_file_rst(fname, target_dir, src_dir) | ||
generate_file_rst(fname, target_dir, src_dir, gallery_conf) | ||
new_fname = os.path.join(src_dir, fname) | ||
intro = extract_intro(new_fname) | ||
write_backreferences(seen_backrefs, gallery_conf, | ||
|
@@ -361,7 +377,7 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): | |
|
||
|
||
def execute_script(code_block, example_globals, image_path, fig_count, | ||
src_file): | ||
src_file, gallery_conf): | ||
"""Executes the code block of the example file""" | ||
time_elapsed = 0 | ||
stdout = '' | ||
|
@@ -392,7 +408,7 @@ def execute_script(code_block, example_globals, image_path, fig_count, | |
if my_stdout: | ||
stdout = CODE_OUTPUT.format(indent(my_stdout, ' ' * 4)) | ||
os.chdir(cwd) | ||
figure_list = save_figures(image_path, fig_count) | ||
figure_list = save_figures(image_path, fig_count, gallery_conf) | ||
|
||
# Depending on whether we have one or more figures, we're using a | ||
# horizontal list or a single rst call to 'image'. | ||
|
@@ -405,6 +421,7 @@ def execute_script(code_block, example_globals, image_path, fig_count, | |
image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/') | ||
|
||
except Exception: | ||
figure_list = [] | ||
image_list = '%s is not compiling:' % src_file | ||
print(80 * '_') | ||
print(image_list) | ||
|
@@ -420,7 +437,7 @@ def execute_script(code_block, example_globals, image_path, fig_count, | |
return code_output, time_elapsed, fig_count + len(figure_list) | ||
|
||
|
||
def generate_file_rst(fname, target_dir, src_dir): | ||
def generate_file_rst(fname, target_dir, src_dir, gallery_conf): | ||
""" Generate the rst file for a given example.""" | ||
|
||
src_file = os.path.join(src_dir, fname) | ||
|
@@ -460,7 +477,8 @@ def generate_file_rst(fname, target_dir, src_dir): | |
example_globals, | ||
image_path, | ||
fig_count, | ||
src_file) | ||
src_file, | ||
gallery_conf) | ||
|
||
time_elapsed += rtime | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but now this link will not exist unless mayavi is present... maybe it's just a sphinx warning so no big deal...