Devel-IPerl

 view release on metacpan or  search on metacpan

example/20150209_IPerl_display_demo.pod  view on Meta::CPAN


=head1 IPerl display demo

author: Zaki Mughal

date: 2015-02-09

B<View this notebook on L<nbviewer|http://nbviewer.ipython.org/github/zmughal/zmughal-iperl-notebooks/blob/master/IPerl-demos/20150209_IPerl_display_demo.ipynb>>.

This notebook demonstrates how to use the I<rich display system> in IPerl and how it can be extended on the fly.

All data that is displayed implements a C<<< Displayable >>> role. This role requires a method called C<<< iperl_data_representations >>> that returns a HashRef of different representations of the data where the keys are the MIME type of the data (e....


  use v5.16;
  use DDP; # Data::Printer
  
  my $png_display = Devel::IPerl::Display::PNG->new( "http://www.libpng.org/pub/png/PngSuite/ccwn3p08.png" );
  
  say &p( [ keys $png_display->iperl_data_representations ] );
  
  $png_display;

=begin html

<tt><font face='fixedsys, lucida console, terminal, vga, monospace' style='line-height: 1; letter-spacing: 0; font-size: 12pt'><span style='color: gray; background: black; '>[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb...

=end html

=begin html

<p><img						src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAAYagMeiWXwAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAC4lBMVEX2sN6J/3Xk/8UN/2vq/4w1/63/McCMSZeA/9b+JjT/sTTgMv4ciP//DnRR/yPFA9PIAWz/zi3/Xq...

=end html



Notice that I just displayed that PNG by just putting the C<<< $png_display >>> variable at the end? That's because any displayable is automatically displayed if it is at the end of a cell.

But there's a problem: I don't want to type or remember C<<< Devel::IPerl::Display::PNG >>> every time I want to load up a PNG.

Instead, you can just call the helper method C<<< IPerl->png() >>> and you'll get the same result.


  IPerl->png( "http://www.libpng.org/pub/png/PngSuite/ccwn3p08.png" );

=begin html

<p><img						src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAAYagMeiWXwAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAC4lBMVEX2sN6J/3Xk/8UN/2vq/4w1/63/McCMSZeA/9b+JjT/sTTgMv4ciP//DnRR/yPFA9PIAWz/zi3/Xq...

=end html



There are other C<<< Displayable >>>s too. For example, let's load up an C<<< <iframe> >>>.


  my $iframe_display = IPerl->iframe( "http://metacpan.org/recent", width => "75%" );

=begin html

<p>	<iframe	    width="75%"	    	    src="http://metacpan.org/recent"	    frameborder="0"	    allowfullscreen	></iframe></p>

=end html



What if we want to display multiple things in one cell? For example, we want to loop over a number of images and display each of them?

You can do that by calling the C<<< IPerl->display() >>> on the C<<< Displayable >>> object.


  my @svg = split ' ', q[
          https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg
          https://upload.wikimedia.org/wikipedia/commons/f/f9/LED,_5mm,_green_(en).svg
      ];
  IPerl->display( IPerl->svg( $_ , width => "200" ) ) for @svg;
  
  $png_display;

=begin html

<p><img		width="200"				src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pgo8c3ZnIGlkPSJzdmcyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA5MDAgOTAwIiB2ZXJzaW9uPSIxLjEiPgogPGcgaWQ9Imc0IiBmaWxsPSJub25...

=end html

=begin html

<p><img		width="200"				src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pgo8IS0tIENyZWF0ZWQgd2l0aCBJbmtzY2FwZSAoaHR0cDovL3d3dy5pbmtzY2FwZS5vcmcvKSAtLT4KCjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjE...

=end html

=begin html

<p><img						src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAAYagMeiWXwAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAC4lBMVEX2sN6J/3Xk/8UN/2vq/4w1/63/McCMSZeA/9b+JjT/sTTgMv4ciP//DnRR/yPFA9PIAWz/zi3/Xq...

=end html



There is also a way to display arbitrary HTML. Here, we create a simple HTML table that loops over a 2D nested ArrayRef.

We could send the string directly to the C<<< IPerl->html() >>> method, but that isn't very L<DRY|https://en.wikipedia.org/wiki/Don't_repeat_yourself>. Instead, we'll create our own helper!




( run in 2.828 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )