File Coverage

blib/lib/Devel/IPerl/Plugin/Chart/Plotly.pm
Criterion Covered Total %
statement 35 41 85.3
branch n/a
condition n/a
subroutine 11 13 84.6
pod 1 3 33.3
total 47 57 82.4


line stmt bran cond sub pod time code
1             package Devel::IPerl::Plugin::Chart::Plotly;
2              
3 2     2   101652 use strict;
  2         12  
  2         57  
4 2     2   10 use warnings;
  2         5  
  2         48  
5 2     2   1283 use utf8;
  2         29  
  2         10  
6              
7 2     2   1013 use Module::Find;
  2         2756  
  2         131  
8 2     2   959 use Chart::Plotly;
  2         169630  
  2         98  
9 2     2   1052 use namespace::autoclean;
  2         22720  
  2         8  
10              
11             our $VERSION = '0.006'; # VERSION
12              
13             # ABSTRACT: Inline display of plotly charts in Jupyter notebooks using L<Devel::IPerl> kernel
14              
15             my $parameter_list = "(" . join( ", ", Chart::Plotly::plotlyjs_plot_function_parameters() ) . ")";
16             my $require_plotly = <<'EOJSFP';
17             <script>
18             //# sourceURL=iperl-devel-plugin-chart-plotly.js
19             $('#Plotly').each(function(i, e) { $(e).attr('id', 'plotly') });
20              
21             if (!window.Plotly) {
22             requirejs.config({
23             paths: {
24             plotly: ['https://cdn.plot.ly/plotly-latest.min']},
25             });
26             window.Plotly = {
27             EOJSFP
28              
29             $require_plotly .= Chart::Plotly::plotlyjs_plot_function() . " : function " . $parameter_list . "{\n";
30             $require_plotly .= <<'EOJSSP';
31             require(['plotly'], function(plotly) {
32             window.Plotly=plotly;
33             EOJSSP
34             $require_plotly .= "Plotly." . Chart::Plotly::plotlyjs_plot_function() . $parameter_list . ";";
35             $require_plotly .= <<'EOJSTP';
36             });
37             }
38             }
39             }
40             </script>
41             EOJSTP
42              
43             sub register {
44              
45             # only works registering the plugin for each notebook
46 1     1 1 621 require Chart::Plotly::Plot;
47 1         744728 require Role::Tiny;
48              
49 1         13 Role::Tiny->apply_roles_to_package( 'Chart::Plotly::Plot',
50             q(Devel::IPerl::Plugin::Chart::Plotly::Plot::IPerlRole) );
51 1         471 for my $module ( findsubmod('Chart::Plotly::Trace') ) {
52 47         18892 Role::Tiny->apply_roles_to_package( $module, q(Devel::IPerl::Plugin::Chart::Plotly::Plot::Trace::IPerlRole) );
53             }
54             }
55              
56             {
57             package Devel::IPerl::Plugin::Chart::Plotly::Plot::IPerlRole;
58              
59 2     2   1635 use Moo::Role;
  2         25273  
  2         11  
60              
61 2     2   1682 use Devel::IPerl::Display::HTML;
  2         60597  
  2         261  
62              
63             sub iperl_data_representations {
64 0     0 0   my ($plot) = @_;
65 0           Devel::IPerl::Display::HTML->new( $require_plotly . $plot->html( load_plotly_using_script_tag => 0 ) )
66             ->iperl_data_representations;
67             }
68              
69             }
70              
71             {
72             package Devel::IPerl::Plugin::Chart::Plotly::Plot::Trace::IPerlRole;
73              
74 2     2   17 use Moo::Role;
  2         4  
  2         9  
75              
76 2     2   728 use Devel::IPerl::Display::HTML;
  2         18  
  2         255  
77              
78             sub iperl_data_representations {
79 0     0 0   require Chart::Plotly::Plot;
80 0           my ($trace) = @_;
81 0           my $plot = Chart::Plotly::Plot->new( traces => [$trace] );
82 0           Devel::IPerl::Display::HTML->new( $require_plotly . $plot->html( load_plotly_using_script_tag => 0 ) )
83             ->iperl_data_representations;
84             }
85              
86             }
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             Devel::IPerl::Plugin::Chart::Plotly - Inline display of plotly charts in Jupyter notebooks using L<Devel::IPerl> kernel
99              
100             =head1 VERSION
101              
102             version 0.006
103              
104             =head1 SYNOPSIS
105              
106             # In notebook
107             IPerl->load_plugin('Chart::Plotly');
108              
109             # Trace objects get displayed automatically
110             use Chart::Plotly::Trace::Scatter;
111             my $scatter_trace = Chart::Plotly::Trace::Scatter->new( x => [ 1 .. 5 ], y => [ 1 .. 5 ] );
112              
113             # Also Plot objects
114             use Chart::Plotly::Trace::Box;
115             use Chart::Plotly::Plot;
116              
117             my $x = [ 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3 ];
118             my $box1 = Chart::Plotly::Trace::Box->new( x => $x, y => [ map { rand() } ( 1 .. ( scalar(@$x) ) ) ], name => "box1" );
119             my $box2 = Chart::Plotly::Trace::Box->new( x => $x, y => [ map { rand() } ( 1 .. ( scalar(@$x) ) ) ], name => "box2" );
120             my $plot = Chart::Plotly::Plot->new( traces => [ $box1, $box2 ], layout => { boxmode => 'group' } );
121              
122             =head1 DESCRIPTION
123              
124             Plugin to display automatically L<Chart::Plotly> plot objects in Jupyter notebooks using kernel L<Devel::IPerl>
125              
126             The example above can be viewed in L<nbviewer|http://nbviewer.jupyter.org/github/pablrod/p5-Devel-IPerl-Plugin-Chart-Plotly/blob/master/examples/PlotlyPlugin.ipynb>
127              
128             =head1 INSTANCE METHODS
129              
130             =head2 register
131              
132             This method is called automatically by L<Devel::IPerl>. You only need to load the plugin:
133              
134             IPerl->load_plugin('Chart::Plotly');
135              
136             =head1 AUTHOR
137              
138             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is Copyright (c) 2019 by Pablo Rodríguez González.
143              
144             This is free software, licensed under:
145              
146             The MIT (X11) License
147              
148             =head1 CONTRIBUTOR
149              
150             =for stopwords Roy Storey
151              
152             Roy Storey <kiwiroy@users.noreply.github.com>
153              
154             =cut