Beispiel #1
0
    def to_jshtml(self, fps):
        # Exports a javascript enabled html object that is
        # ready for jupyter notebook embedding.
        # modified from matplotlib/animation.py code.

        # Only works with Python3
        import sys
        if sys.version_info[0] < 3:
            warnings.warn(
                'JSHTML output is not supported with python2 builds.')
            return ""

        from uuid import uuid4
        from matplotlib._animation_data import (DISPLAY_TEMPLATE,
                                                INCLUDED_FRAMES, JS_INCLUDE,
                                                STYLE_INCLUDE)

        # save the frames to an html file
        fill_frames = self._embedded_frames(self._saved_frames,
                                            self.frame_format)
        Nframes = len(self._saved_frames)
        mode_dict = dict(once_checked='', loop_checked='', reflect_checked='')
        mode_dict[self.default_mode + '_checked'] = 'checked'

        interval = 1000 // fps

        html_string = ""
        html_string += JS_INCLUDE
        html_string += STYLE_INCLUDE
        html_string += DISPLAY_TEMPLATE.format(id=uuid4().hex,
                                               Nframes=Nframes,
                                               fill_frames=fill_frames,
                                               interval=interval,
                                               **mode_dict)
        return JS_Animation(html_string)
Beispiel #2
0
    def to_jshtml(self, fps):
        """
        Outputs an interactable JSHTML animation object that is embeddable in Jupyter
        notebooks. The object is packaged with controls to manipulate the video's
        playback. User must specify a frame rate `fps` in frames per second.
        """
        # Exports a javascript enabled html object that is
        # ready for jupyter notebook embedding.
        # modified from matplotlib/animation.py code.

        # Only works with Python3 and matplotlib > 3.1.0
        from distutils.version import LooseVersion
        import matplotlib
        if LooseVersion(matplotlib.__version__) < LooseVersion("3.1.0"):
            print('-------------------------------')
            print(
                'Warning: JSHTML output is not supported with your current matplotlib build. Consider upgrading to 3.1.0+'
            )
            print('-------------------------------')
            return
        if mp.am_master():
            from uuid import uuid4
            from matplotlib._animation_data import (DISPLAY_TEMPLATE,
                                                    INCLUDED_FRAMES,
                                                    JS_INCLUDE, STYLE_INCLUDE)

            # save the frames to an html file
            fill_frames = self._embedded_frames(self._saved_frames,
                                                self.frame_format)
            Nframes = len(self._saved_frames)
            mode_dict = dict(once_checked='',
                             loop_checked='',
                             reflect_checked='')
            mode_dict[self.default_mode + '_checked'] = 'checked'

            interval = 1000 // fps

            html_string = ""
            html_string += JS_INCLUDE
            html_string += STYLE_INCLUDE
            html_string += DISPLAY_TEMPLATE.format(id=uuid4().hex,
                                                   Nframes=Nframes,
                                                   fill_frames=fill_frames,
                                                   interval=interval,
                                                   **mode_dict)
            return JS_Animation(html_string)