question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Displaying saved animations

See original GitHub issue

Can a saved animation be displayed in a generated file? I’m using

class Animator(matplotlib.animation.TimedAnimation):
    def __init__(self):
        animation.TimedAnimation.__init__(self, ...)

Animator().save(fps=..., savefig_kwargs=...)

where animation behavior is controlled in save, which I presume can’t be scraped. In my context it also fails to animate entirely but I’ve not yet reproduced minimally (a minimal example works fine).

I imagine something like

ani.save("path.mp4")
some_sphinx_gallery_function("path.mp4")

and I’m aware there’s ways to insert images via standard sphinx functionality, but don’t know how to do it inline for HTML generated from .py by sphinx-gallery.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
larsonercommented, Sep 26, 2022

‘image_scrapers’: (‘matplotlib’, CustomScraper()) appends my scraper to the existing one.

Yep, and they will run in that order (mpl then yours).

Scrapers execute after every cell.

Yes indeed, see:

https://sphinx-gallery.github.io/stable/advanced.html#write-a-custom-image-scraper

in particular the quote:

This function will be called once for each code block of your examples

sphinx-gallery doesn’t support .mp4 saved animations

SG internally just supports mpl (including animations if used properly) and mayavi, with PyVista and PyGMT maintaining their implementations. Other formats – including any saved to disk – are left to the user to support.

0reactions
OverLordGoldDragoncommented, Sep 23, 2022

What I learned:

  1. 'image_scrapers': ('matplotlib', CustomScraper()) appends my scraper to the existing one.
  2. Scrapers execute after every cell.
  3. sphinx-gallery doesn’t support .mp4 saved animations, so one must either 1) save as .gif instead, or 2) .mp4 -> .gif in the scraper. Useful: _anim_rst in scrapers.

Can you confirm? I’ve extended the PNGScraper to encompass all supported formats:

class PlotScraper(object):
    def __init__(self):
        self.seen = set()

    def __repr__(self):
        return 'PlotScraper'

    def __call__(self, block, block_vars, gallery_conf):
        # Find all supported plot files in the directory of this example.
        path_current_example = os.path.dirname(block_vars['src_file'])
        supported = ('png', 'jpg', 'svg', 'gif')
        files = [values for ext in supported for values in
                 sorted(glob(os.path.join(path_current_example, f'*.{ext}')))]

        # Iterate through files, copy them to the sphinx-gallery output directory
        file_names = list()
        image_path_iterator = block_vars['image_path_iterator']
        for file in files:
            if file not in self.seen:
                self.seen |= set(file)
                this_path = image_path_iterator.next()
                this_path = this_path.replace(Path(this_path).suffix,
                                              Path(file).suffix)
                file_names.append(this_path)
                shutil.move(file, this_path)
        # Use the `figure_rst` helper function to generate rST for image files
        return figure_rst(file_names, gallery_conf['src_dir'])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving Animations
It is possible to save the animation window to a local HTML file. You can then bookmark the saved file, and play back...
Read more >
Where are the animations I saved? - Google Photos Community
Google created a cool animation from my photos from yesterday. ... Search / Magnifying Glass in the App towards the bottom of the...
Read more >
SolidWorks Motion: Part 5 - Saving Animations - YouTube
This part will explain how you can save the animations you have created. The video will go over some of the different settings...
Read more >
How to save Matplotlib Animation? - GeeksforGeeks
In this article, we will learn How to save Matplotlib Animation. The animated graphs made with the help of matplotlib can be saved...
Read more >
Saving animations and screenshots - the DKRZ documentation!
Saving the animation Save your animation by clicking on File -> Save Animation… This opens a dialogue window, in which you select either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found