示例#1
0
    def embed_image(self, output_buffer, file_ext):
        """
        Embed Image in the SageNB worksheet

        SageNB scans per-cell directories for image files, so all we
        have to do here is to save the image at the right place.

        INPUT:

        - ``output_buffer`` --
          :class:`~sage.repl.rich_output.buffer.Buffer`. A buffer
          holding the image data.
        
        - ``file_ext`` -- string. The file extension to use for saving
          the image.

        OUTPUT:

        Nothing is returned. The image file is saved in the
        appropriate place for SageNB.

        EXAMPLES::

            sage: from sage.repl.rich_output import get_display_manager
            sage: dm = get_display_manager()
            sage: rich_output = dm.types.OutputImagePng.example()
            sage: os.path.exists('sage0.png')
            False
            sage: dm._backend.embed_image(rich_output.png, '.png')
            sage: os.path.exists('sage0.png')
            True
        """
        filename = graphics_filename(ext=file_ext)
        output_buffer.save_as(filename)
        world_readable(filename)
示例#2
0
    def show(self, filename=None, ImageSize=600):
        r"""
        Show a mathematica expression or plot in the Sage notebook.

        EXAMPLES::

            sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
            sage: show(P)                                        # optional - mathematica
            sage: P.show(ImageSize=800)                          # optional - mathematica
            sage: Q = mathematica('Sin[x Cos[y]]/Sqrt[1-x^2]')   # optional - mathematica
            sage: show(Q)                                        # optional - mathematica
            <html><div class="math">\frac{\sin (x \cos (y))}{\sqrt{1-x^2}}</div></html>
        """
        P = self._check_valid()
        if P.eval('InputForm[%s]' % self.name()).strip().startswith('Graphics['):
            if filename is None:
                from sage.misc.temporary_file import graphics_filename
                filename = graphics_filename()
            orig_dir = P.eval('Directory[]').strip()
            P.chdir(os.path.abspath("."))
            s = 'Export["%s", %s, ImageSize->%s]'%(filename, self.name(), ImageSize)
            P.eval(s)
            P.chdir(orig_dir)
        else:
            print '<html><div class="math">%s</div></html>' % self._latex_()
示例#3
0
    def embed_image(self, output_buffer, file_ext):
        """
        Embed Image in the SageNB worksheet

        SageNB scans per-cell directories for image files, so all we
        have to do here is to save the image at the right place.

        INPUT:

        - ``output_buffer`` --
          :class:`~sage.repl.rich_output.buffer.Buffer`. A buffer
          holding the image data.
        
        - ``file_ext`` -- string. The file extension to use for saving
          the image.

        OUTPUT:

        Nothing is returned. The image file is saved in the
        appropriate place for SageNB.

        EXAMPLES::

            sage: from sage.repl.rich_output import get_display_manager
            sage: dm = get_display_manager()
            sage: rich_output = dm.types.OutputImagePng.example()
            sage: os.path.exists('sage0.png')
            False
            sage: dm._backend.embed_image(rich_output.png, '.png')
            sage: os.path.exists('sage0.png')
            True
        """
        filename = graphics_filename(ext=file_ext)
        output_buffer.save_as(filename)
        world_readable(filename)
    def show(self, filename=None, ImageSize=600):
        r"""
        Show a mathematica expression or plot in the Sage notebook.

        EXAMPLES::

            sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
            sage: show(P)                                        # optional - mathematica
            sage: P.show(ImageSize=800)                          # optional - mathematica
            sage: Q = mathematica('Sin[x Cos[y]]/Sqrt[1-x^2]')   # optional - mathematica
            sage: show(Q)                                        # optional - mathematica
            <html><div class="math">\frac{\sin (x \cos (y))}{\sqrt{1-x^2}}</div></html>
        """
        P = self._check_valid()
        if P.eval('InputForm[%s]' %
                  self.name()).strip().startswith('Graphics['):
            if filename is None:
                from sage.misc.temporary_file import graphics_filename
                filename = graphics_filename()
            orig_dir = P.eval('Directory[]').strip()
            P.chdir(os.path.abspath("."))
            s = 'Export["%s", %s, ImageSize->%s]' % (filename, self.name(),
                                                     ImageSize)
            P.eval(s)
            P.chdir(orig_dir)
        else:
            print '<html><div class="math">%s</div></html>' % self._latex_()
示例#5
0
 def embed_video(self, video_output):
     filename = graphics_filename(ext=video_output.ext)
     video_output.video.save_as(filename)
     world_readable(filename)
     html(video_output.html_fragment(
         url='cell://' + filename,
         link_attrs='class="file_link"',
     ))
示例#6
0
 def embed_video(self, video_output):
     filename = graphics_filename(ext=video_output.ext)
     video_output.video.save_as(filename)
     world_readable(filename)
     html(
         video_output.html_fragment(
             url='cell://' + filename,
             link_attrs='class="file_link"',
         ))
示例#7
0
文件: animate.py 项目: Etn40ff/sage
    def show(self, delay=20, iterations=0):
        r"""
        Show this animation.

        INPUT:


        -  ``delay`` - (default: 20) delay in hundredths of a
           second between frames

        -  ``iterations`` - integer (default: 0); number of
           iterations of animation. If 0, loop forever.


        .. note::

           Currently this is done using an animated gif, though this
           could change in the future. This requires that either
           ffmpeg or the ImageMagick suite (in particular, the
           ``convert`` command) is installed.

        See also the :meth:`ffmpeg` method.

        EXAMPLES::

            sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
            ....:                xmin=0, xmax=2*pi, figsize=[2,1])
            sage: a.show()       # optional -- ImageMagick

        The preceding will loop the animation forever. If you want to show
        only three iterations instead::

            sage: a.show(iterations=3)    # optional -- ImageMagick

        To put a half-second delay between frames::

            sage: a.show(delay=50)        # optional -- ImageMagick

        .. note::

           If you don't have ffmpeg or ImageMagick installed, you will
           get an error message like this::

              Error: Neither ImageMagick nor ffmpeg appears to be installed. Saving an
              animation to a GIF file or displaying an animation requires one of these
              packages, so please install one of them and try again.

              See www.imagemagick.org and www.ffmpeg.org for more information.
        """
        filename = graphics_filename(ext='gif')
        self.gif(savefile=filename, delay=delay, iterations=iterations)
        if not (sage.doctest.DOCTEST_MODE or plot.EMBEDDED_MODE):
            os.system('%s %s 2>/dev/null 1>/dev/null &'%(
                sage.misc.viewer.browser(), filename))
示例#8
0
    def sagenb_embedding(self):
        """
        Embed in SageNB

        This amounts to just placing the file in the cell
        directory. The notebook will then try to guess what we want
        with it.
        """
        from sage.misc.temporary_file import graphics_filename
        ext = Mime.extension(self.mime())
        if sage.doctest.DOCTEST_MODE:
            return
        self.save_as(graphics_filename(ext=ext))
示例#9
0
    def sagenb_embedding(self):
        """
        Embed in SageNB

        This amounts to just placing the file in the cell
        directory. The notebook will then try to guess what we want
        with it.
        """
        from sage.misc.temporary_file import graphics_filename
        ext = "." + Mime.extension(self.mime())
        fn = graphics_filename(ext=ext)
        self.save_as(fn)
        # Client-server sagenb requires this to be world-readable.
        # See Trac #17755.
        os.chmod(fn, 0o644)
示例#10
0
    def sagenb_embedding(self):
        """
        Embed in SageNB

        This amounts to just placing the file in the cell
        directory. The notebook will then try to guess what we want
        with it.
        """
        from sage.misc.temporary_file import graphics_filename
        ext = "." + Mime.extension(self.mime())
        fn = graphics_filename(ext=ext)
        self.save_as(fn)
        # Client-server sagenb requires this to be world-readable.
        # See Trac #17755.
        os.chmod(fn, 0o644)
示例#11
0
    def sagenb_launch_script_filename(self):
        """
        Return the launch script filename used by SageNB

        OUTPUT:

        String.

        EXAMPLES::

            sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol
            sage: j = SageNbOutputSceneJmol.example()
            sage: j.sagenb_launch_script_filename()
            'sage0-size32.jmol'
        """
        import PIL.Image
        width, height = PIL.Image.open(io.BytesIO(self.preview_png.get())).size
        ext = '-size{0}.jmol'.format(width)
        return graphics_filename(ext=ext)
示例#12
0
    def sagenb_launch_script_filename(self):
        """
        Return the launch script filename used by SageNB

        OUTPUT:

        String.

        EXAMPLES::

            sage: from sage.repl.rich_output.backend_sagenb import SageNbOutputSceneJmol
            sage: j = SageNbOutputSceneJmol.example()
            sage: j.sagenb_launch_script_filename()
            'sage0-size32.jmol'
        """
        import PIL.Image
        from StringIO import StringIO
        width, height = PIL.Image.open(StringIO(self.preview_png.get())).size
        ext = '-size{0}.jmol'.format(width)
        return graphics_filename(ext=ext)
示例#13
0
    def display_immediately(self, plain_text, rich_output):
        r"""
        Show output without waiting for the prompt.

        INPUT:

        - ``plain_text`` -- instance of
          :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The
          plain text version of the output.

        - ``rich_output`` -- instance of an output container class
          (subclass of
          :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed
          to be one of the output containers returned from
          :meth:`supported_output`, possibly the same as
          ``plain_text``.

        OUTPUT:

        This method does not return anything.

        EXAMPLES::

            sage: import sage.repl.rich_output.output_catalog as catalog
            sage: plain_text = catalog.OutputPlainText.example()
            sage: from sage.repl.rich_output.backend_sagenb import BackendSageNB
            sage: backend = BackendSageNB()
            sage: backend.display_immediately(plain_text, plain_text)
            Example plain text output
            sage: latex = catalog.OutputLatex.example()
            sage: backend.display_immediately(plain_text, latex)
            <html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\int \sin\left(x\right)\,{d x}</script></html>
        """
        if isinstance(rich_output, (OutputPlainText, OutputAsciiArt)):
            rich_output.print_to_stdout()
        elif isinstance(rich_output, OutputLatex):
            print(rich_output.mathjax())
        elif isinstance(rich_output, OutputHtml):
            print(rich_output.with_html_tag())
        elif isinstance(rich_output, OutputImagePng):
            self.embed_image(rich_output.png, '.png')
        elif isinstance(rich_output, OutputImageGif):
            self.embed_image(rich_output.gif, '.gif')
        elif isinstance(rich_output, OutputImageJpg):
            self.embed_image(rich_output.jpg, '.jpg')
        elif isinstance(rich_output, OutputImagePdf):
            self.embed_image(rich_output.pdf, '.pdf')
        elif isinstance(rich_output, OutputImageSvg):
            self.embed_image(rich_output.svg, '.svg')
        elif isinstance(rich_output, OutputSceneJmol):
            rich_output.embed()
        elif isinstance(rich_output, OutputSceneThreejs):
            filename = graphics_filename(ext='.html')
            rich_output.html.save_as(filename)
            world_readable(filename)
            iframe = IFRAME_TEMPLATE.format(
                html='cell://' + filename,
                width=700,
                height=400,
            )
            from pretty_print import pretty_print
            pretty_print(html(iframe))
        elif isinstance(rich_output, OutputSceneCanvas3d):
            self.embed_image(rich_output.canvas3d, '.canvas3d')
        elif isinstance(rich_output, OutputVideoBase):
            self.embed_video(rich_output)
        else:
            raise TypeError(
                'rich_output type not supported, got {}'.format(rich_output))
示例#14
0
文件: animate.py 项目: Etn40ff/sage
    def ffmpeg(self, savefile=None, show_path=False, output_format=None,
               ffmpeg_options='', delay=None, iterations=0, pix_fmt='rgb24'):
        r"""
        Returns a movie showing an animation composed from rendering
        the frames in self.

        This method will only work if ffmpeg is installed.  See
        http://www.ffmpeg.org for information about ffmpeg.

        INPUT:

        -  ``savefile`` - file that the mpeg gets saved to.

        .. warning:

            This will overwrite ``savefile`` if it already exists.

        - ``show_path`` - boolean (default: False); if True, print the
          path to the saved file

        - ``output_format`` - string (default: None); format and
          suffix to use for the video.  This may be 'mpg', 'mpeg',
          'avi', 'gif', or any other format that ffmpeg can handle.
          If this is None and the user specifies ``savefile`` with a
          suffix, say ``savefile='animation.avi'``, try to determine the
          format ('avi' in this case) from that file name.  If no file
          is specified or if the suffix cannot be determined, 'mpg' is
          used.

        - ``ffmpeg_options`` - string (default: ''); this string is
          passed directly to ffmpeg.

        - ``delay`` - integer (default: None); delay in hundredths of a
          second between frames.  The framerate is 100/delay.
          This is not supported for mpeg files: for mpegs, the frame
          rate is always 25 fps.

        - ``iterations`` - integer (default: 0); number of iterations
          of animation. If 0, loop forever.  This is only supported
          for animated gif output and requires ffmpeg version 0.9 or
          later.  For older versions, set ``iterations=None``.

        - ``pix_fmt`` - string (default: 'rgb24'); used only for gif
          output.  Different values such as 'rgb8' or 'pal8' may be
          necessary depending on how ffmpeg was installed.  Set
          ``pix_fmt=None`` to disable this option.

        If ``savefile`` is not specified: in notebook mode, display
        the animation; otherwise, save it to a default file name.  Use
        :func:`sage.misc.misc.set_verbose` with ``level=1`` to see
        additional output.

        EXAMPLES::

            sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
            ....:                xmin=0, xmax=2*pi, figsize=[2,1])
            sage: dir = tmp_dir()
            sage: a.ffmpeg(savefile=dir + 'new.mpg')       # optional -- ffmpeg
            sage: a.ffmpeg(savefile=dir + 'new.avi')       # optional -- ffmpeg
            sage: a.ffmpeg(savefile=dir + 'new.gif')       # optional -- ffmpeg
            sage: a.ffmpeg(savefile=dir + 'new.mpg', show_path=True) # optional -- ffmpeg
            Animation saved to .../new.mpg.

        .. note::

           If ffmpeg is not installed, you will get an error message
           like this::

              Error: ffmpeg does not appear to be installed. Saving an animation to
              a movie file in any format other than GIF requires this software, so
              please install it and try again.

              See www.ffmpeg.org for more information.


        TESTS::

            sage: a.ffmpeg(output_format='gif',delay=30,iterations=5)     # optional -- ffmpeg
        """
        if not self._have_ffmpeg():
            msg = """Error: ffmpeg does not appear to be installed. Saving an animation to
a movie file in any format other than GIF requires this software, so
please install it and try again."""
            raise OSError(msg)
        else:
            if savefile is None:
                if output_format is None:
                    output_format = '.mpg'
                else:
                    if output_format[0] != '.':
                        output_format = '.'+output_format
                savefile = graphics_filename(ext=output_format[1:])
            else:
                if output_format is None:
                    suffix = os.path.splitext(savefile)[1]
                    if len(suffix) > 0:
                        output_format = suffix
                    else:
                        output_format = '.mpg'
            if not savefile.endswith(output_format):
                savefile += output_format
            early_options = ''
            if output_format == '.gif':
                # We try to set reasonable options for gif output.
                #
                # Older versions of ffmpeg (before 0.9, summer 2011)
                # use the option -loop_output instead of -loop.
                # Setting iterations=None is a way of preventing sage
                # from adding the -loop option.  A separate
                # -loop_output option can be added with the
                # ffmpeg_options argument.
                if iterations is not None:
                    loop_cmd = '-loop {0} '.format(iterations)
                else:
                    loop_cmd = ''
                # A pix_fmt value is required for some but not all
                # ffmpeg installations.  Setting pix_fmt=None will
                # prevent sage from adding this option, and it may be
                # controlled separately through ffmpeg_options.
                if pix_fmt is not None:
                    pix_fmt_cmd = '-pix_fmt {0} '.format(pix_fmt)
                else:
                    pix_fmt_cmd = ''
                ffmpeg_options += ' {0}{1}'.format(pix_fmt_cmd,loop_cmd)
            if delay is not None and output_format != '.mpeg' and output_format != '.mpg':
                early_options += ' -r %s ' % int(100/delay)
            savefile = os.path.abspath(savefile)
            pngdir = self.png()
            pngs = os.path.join(pngdir, "%08d.png")
            # For ffmpeg, it seems that some options, like '-g ... -r
            # ...', need to come before the input file names, while
            # some options, like '-pix_fmt rgb24', need to come
            # afterwards.  Hence 'early_options' and 'ffmpeg_options'
            cmd = 'cd "%s"; sage-native-execute ffmpeg -y -f image2 %s -i %s %s %s' % (pngdir, early_options, pngs, ffmpeg_options, savefile)
            from subprocess import check_call, CalledProcessError, PIPE
            try:
                if sage.misc.misc.get_verbose() > 0:
                    set_stderr = None
                else:
                    set_stderr = PIPE
                sage.misc.misc.verbose("Executing '%s'" % cmd,level=1)
                sage.misc.misc.verbose("\n---- ffmpeg output below ----\n")
                check_call(cmd, shell=True, stderr=set_stderr)
                if show_path:
                    print "Animation saved to file %s." % savefile
            except (CalledProcessError, OSError):
                print "Error running ffmpeg."
                raise
示例#15
0
文件: animate.py 项目: Etn40ff/sage
    def gif(self, delay=20, savefile=None, iterations=0, show_path=False,
            use_ffmpeg=False):
        r"""
        Returns an animated gif composed from rendering the graphics
        objects in self.

        This method will only work if either (a) the ImageMagick
        software suite is installed, i.e., you have the ``convert``
        command or (b) ``ffmpeg`` is installed.  See
        [IM] for more about ImageMagick, and see
        [FF] for more about ``ffmpeg``.  By default, this
        produces the gif using ``convert`` if it is present.  If this
        can't find ``convert`` or if ``use_ffmpeg`` is True, then it
        uses ``ffmpeg`` instead.

        INPUT:

        -  ``delay`` - (default: 20) delay in hundredths of a
           second between frames

        -  ``savefile`` - file that the animated gif gets saved
           to

        -  ``iterations`` - integer (default: 0); number of
           iterations of animation. If 0, loop forever.

        -  ``show_path`` - boolean (default: False); if True,
           print the path to the saved file

        - ``use_ffmpeg`` - boolean (default: False); if True, use
          'ffmpeg' by default instead of 'convert'.

        If ``savefile`` is not specified: in notebook mode, display the
        animation; otherwise, save it to a default file name.

        EXAMPLES::

            sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
            ....:                xmin=0, xmax=2*pi, figsize=[2,1])
            sage: dir = tmp_dir()
            sage: a.gif()              # not tested
            sage: a.gif(savefile=dir + 'my_animation.gif', delay=35, iterations=3)  # optional -- ImageMagick
            sage: a.gif(savefile=dir + 'my_animation.gif', show_path=True) # optional -- ImageMagick
            Animation saved to .../my_animation.gif.
            sage: a.gif(savefile=dir + 'my_animation_2.gif', show_path=True, use_ffmpeg=True) # optional -- ffmpeg
            Animation saved to .../my_animation_2.gif.

        .. note::

           If neither ffmpeg nor ImageMagick is installed, you will
           get an error message like this::

              Error: Neither ImageMagick nor ffmpeg appears to be installed. Saving an
              animation to a GIF file or displaying an animation requires one of these
              packages, so please install one of them and try again.

              See www.imagemagick.org and www.ffmpeg.org for more information.
        """
        from sage.misc.sage_ostools import have_program
        have_convert = have_program('convert')
        have_ffmpeg = self._have_ffmpeg()
        if use_ffmpeg or not have_convert:
            if have_ffmpeg:
                self.ffmpeg(savefile=savefile, show_path=show_path,
                            output_format='.gif', delay=delay,
                            iterations=iterations)
            else:
                if not have_convert:
                    msg = """
Error: Neither ImageMagick nor ffmpeg appears to be installed. Saving an
animation to a GIF file or displaying an animation requires one of these
packages, so please install one of them and try again.

See www.imagemagick.org and www.ffmpeg.org for more information."""
                else:
                    msg = """
Error: ffmpeg does not appear to be installed.  Download it from
www.ffmpeg.org, or use 'convert' to produce gifs instead."""
                raise OSError(msg)
        else:
            if not savefile:
                savefile = graphics_filename(ext='gif')
            if not savefile.endswith('.gif'):
                savefile += '.gif'
            savefile = os.path.abspath(savefile)
            d = self.png()
            cmd = ( 'cd "%s"; sage-native-execute convert -dispose Background '
                    '-delay %s -loop %s *.png "%s"' ) % ( d, int(delay),
                        int(iterations), savefile )
            from subprocess import check_call, CalledProcessError
            try:
                check_call(cmd, shell=True)
                if show_path:
                    print "Animation saved to file %s." % savefile
            except (CalledProcessError, OSError):
                msg = """
Error: Cannot generate GIF animation.  Verify that convert
(ImageMagick) or ffmpeg is installed, and that the objects passed to
the animate command can be saved in PNG image format.

See www.imagemagick.org and www.ffmpeg.org for more information."""
                raise OSError(msg)