print "Hello! This IPython notebook file is generated with an early version of the IPerl language kernel.\n";
Hello! This IPython notebook file is generated with an early version of the IPerl language kernel.
1
This document is meant to serve as a demonstration of how to run the IPerl kernel, what it can currently do, and what needs to be done to improve it.
You can get the source for the IPerl language kernel from GitHub by running
git clone https://github.com/zmughal/p5-Devel-IPerl.git
IPerl currently has some non-Perl dependencies that are needed for various features. These include the IPython and IPython notebook itself and some native code needed for communication (ZeroMQ) and content sniffing (libmagic). To install these dependencies on a Debian-based system run
sudo apt-get install libzmq3-dev ipython ipython-notebook libmagic-dev
You can then install the Perl dependencies using the cpanfile
with cpanm
in the top level of the
source code directory
cpanm --installdeps .
Finally, to start up IPython notebook with the IPerl kernel, run
./bin/iperl notebook
Now let's see what the IPython notebook can do.
First, let's get the mandrill test image using the Data::TestImage module.
use Data::TestImage;
$mandrill_file = Data::TestImage->get_image('mandrill');
/home/zaki/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/auto/share/dist/Data-TestImage/USC/SIPI/misc/4.2.03.tiff
OK, that's just the path to a TIFF file. Now we can read it into a piddle. As you can see from the output, the number of dimensions in the PDL is 3. Note that the size of the last dimension is 3 which indicates that the image is an RGB image.
use PDL;
use DDP;
$mandrill = rim(~~ $mandrill_file); # need to stringify
p $mandrill;
PDL { Data : too long to print Type : unsigned char Shape : [512 512 3] Nelem : 786432 Min : 0 Max : 255 Badflag : No Has Bads : No }
To display the image in IPython notebook, we need to have a PNG. We can write the piddle to a PNG file on disk and read it back in.
use Path::Class;
$temp_png_file = Path::Class::tempdir()->file( $mandrill_file->basename . '.png' );
$mandrill->wim( $temp_png_file );
$mandrill_png = $temp_png_file->slurp;
Let's try something different.
Here we use the Festival TTS engine to convert some text to a wave file
of the generated speech. In order to play the sound in the browser, we
need to convert the wave file into an MP3 file by using the lame
encoder.
$mp3 = qx{echo "Hello from IPerl" | text2wave | lame - -}; # pipe text to Festival TTS and convert to MP3
LAME 3.99.5 64bits (http://lame.sf.net) Using polyphase lowpass filter, transition band: 5742 Hz - 5935 Hz Encoding <stdin> to <stdout> Encoding as 16 kHz single-ch MPEG-2 Layer III (10.7x) 24 kbps qval=3
We can also use PDL::Graphics::Gnuplot to
plot 2 periods of a sine wave. This also needs to be written to a PNG
file by passing the hardcopy
option to the plot
function.
use PDL::Graphics::Gnuplot;
use Math::Trig qw(pi);
$plot_png_file = Path::Class::tempdir()->file( 'plot.png' );
$x = zeroes(100)->xlinvals( -2 * pi, 2 * pi );
$w = gpwin();
$w->plot( $x, sin($x) , { xlabel => 'x', ylabel => 'sin(x)', xrange => [$x->minmax], hardcopy => $plot_png_file } );
$plot_png = $plot_png_file->slurp();
The IPython notebook can also display LaTeX equations using the MathJax library:
\begin{equation} \int e^{x}\,d\!x = e^{x} + C \,. \end{equation}for (0..2) {
say "test";
sleep 2;
}
Bad file descriptor (epoll.cpp:67)
I have a feeling this is because a part of the kernel is trying to read from a socket after
the frontend has closed it.bin/iperl
) needs to be converted to Perl for portability.
I think that it should just be a wrapper around the ipython
command except that it will also
help create and load Perl profiles.