Skip to content

Commit

Permalink
html table code of DataFrame and Vector are modified (SciRuby#366)
Browse files Browse the repository at this point in the history
* table id is added in table html code

* separating thead and tbody

* methods for accessing thead and tbody

* working tbody and thead script in datatables gem

* modified some testcases

* rubocop errors fixed

* vector html table code modified and used thead and tbody tag
  • Loading branch information
Shekharrajak committed Sep 5, 2017
1 parent e43ee55 commit b6839c7
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 214 deletions.
24 changes: 23 additions & 1 deletion lib/daru/dataframe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,9 @@ def to_h
end

# Convert to html for IRuby.
def to_html threshold=30
def to_html(threshold=30)
table_thead = to_html_thead
table_tbody = to_html_tbody(threshold)
path = if index.is_a?(MultiIndex)
File.expand_path('../iruby/templates/dataframe_mi.html.erb', __FILE__)
else
Expand All @@ -1879,6 +1881,26 @@ def to_html threshold=30
ERB.new(File.read(path).strip).result(binding)
end

def to_html_thead
table_thead_path =
if index.is_a?(MultiIndex)
File.expand_path('../iruby/templates/dataframe_mi_thead.html.erb', __FILE__)
else
File.expand_path('../iruby/templates/dataframe_thead.html.erb', __FILE__)
end
ERB.new(File.read(table_thead_path).strip).result(binding)
end

def to_html_tbody(threshold=30)
table_tbody_path =
if index.is_a?(MultiIndex)
File.expand_path('../iruby/templates/dataframe_mi_tbody.html.erb', __FILE__)
else
File.expand_path('../iruby/templates/dataframe_tbody.html.erb', __FILE__)
end
ERB.new(File.read(table_tbody_path).strip).result(binding)
end

def to_s
"#<#{self.class}#{': ' + @name if @name}(#{nrows}x#{ncols})>"
end
Expand Down
53 changes: 3 additions & 50 deletions lib/daru/iruby/templates/dataframe.html.erb
Original file line number Diff line number Diff line change
@@ -1,52 +1,5 @@
<b> Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>) </b>
<table>
<tr>
<th colspan='<%= @vectors.size+1 %>'>Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>)</th>
</tr>

<% if @vectors.is_a? MultiIndex %>
<% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
<tr>
<% if idx.zero? %>
<th rowspan="<%= @vectors.width %>"></th>
<% end %>
<% tuple.each do |idx, span| %>
<th colspan="<%= span %>"><%= idx %></th>
<% end %>
</tr>
<% end %>
<% else %>
<tr>
<th></th>
<% @vectors.each do |vector| %>
<th><%=vector%></th>
<% end %>
</tr>
<% end %>
<% @index.first(threshold).each_with_index do |index, pos| %>
<tr>
<td><%= index %></td>
<% row.at(pos).each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<% if nrows > threshold %>
<tr>
<% (@vectors.size + 1).times do %>
<td>...</td>
<% end %>
</tr>

<% last_index = @index.to_a.last
last_row = row_at @index.size-1 %>

<tr>
<td><%= last_index %></td>
<% last_row.each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<%= table_thead %>
<%= table_tbody %>
</table>
59 changes: 3 additions & 56 deletions lib/daru/iruby/templates/dataframe_mi.html.erb
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
<b> Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>) </b>
<table>
<tr>
<th colspan='<%= @vectors.size+index.width %>'>Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>)</th>
</tr>
<% if @vectors.is_a? MultiIndex %>
<% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
<tr>
<% if idx.zero? %>
<th colspan="<%= index.width %>" rowspan="<%= @vectors.width %>"></th>
<% end %>
<% tuple.each do |idx, span| %>
<th colspan="<%= span %>"><%= idx %></th>
<% end %>
</tr>
<% end %>
<% else %>
<tr>
<th colspan="<%= index.width %>"></th>
<% @vectors.each do |vector| %>
<th><%=vector%></th>
<% end %>
</tr>
<% end %>
<% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(@index.to_a).each do |tuple, index| %>
<tr>
<% tuple.each do |idx, span| %>
<th rowspan="<%= span %>"><%= idx %></th>
<% end %>
<% row[index].each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<% if nrows > threshold %>
<tr>
<% index.width.times do %>
<th>...</th>
<% end %>
<% @vectors.size.times do %>
<td>...</td>
<% end %>
</tr>

<% last_index = @index.to_a.last
last_row = row[last_index] %>

<tr>
<% last_index.each do |idx| %>
<th><%= idx %></td>
<% end %>
<% last_row.each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<%= table_thead %>
<%= table_tbody %>
</table>
35 changes: 35 additions & 0 deletions lib/daru/iruby/templates/dataframe_mi_tbody.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<tbody>
<% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(@index.to_a).each do |tuple, index| %>
<tr>
<% tuple.each do |idx, span| %>
<th rowspan="<%= span %>"><%= idx %></th>
<% end %>
<% row[index].each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<% if nrows > threshold %>
<tr>
<% index.width.times do %>
<th>...</th>
<% end %>
<% @vectors.size.times do %>
<td>...</td>
<% end %>
</tr>

<% last_index = @index.to_a.last
last_row = row[last_index] %>

<tr>
<% last_index.each do |idx| %>
<th><%= idx %></td>
<% end %>
<% last_row.each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
</tbody>
21 changes: 21 additions & 0 deletions lib/daru/iruby/templates/dataframe_mi_thead.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<thead>
<% if @vectors.is_a? MultiIndex %>
<% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
<tr>
<% if idx.zero? %>
<th colspan="<%= index.width %>" rowspan="<%= @vectors.width %>"></th>
<% end %>
<% tuple.each do |idx, span| %>
<th colspan="<%= span %>"><%= idx %></th>
<% end %>
</tr>
<% end %>
<% else %>
<tr>
<th colspan="<%= index.width %>"></th>
<% @vectors.each do |vector| %>
<th><%=vector%></th>
<% end %>
</tr>
<% end %>
</thead>
28 changes: 28 additions & 0 deletions lib/daru/iruby/templates/dataframe_tbody.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<tbody>
<% @index.first(threshold).each_with_index do |index, pos| %>
<tr>
<td><%= index %></td>
<% row.at(pos).each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
<% if nrows > threshold %>
<tr>
<% (@vectors.size + 1).times do %>
<td>...</td>
<% end %>
</tr>

<% last_index = @index.to_a.last
last_row = row_at @index.size-1 %>

<tr>
<td><%= last_index %></td>
<% last_row.each do |element| %>
<td><%= element.to_s %></td>
<% end %>
</tr>
<% end %>
</tbody>
21 changes: 21 additions & 0 deletions lib/daru/iruby/templates/dataframe_thead.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<thead>
<% if @vectors.is_a? MultiIndex %>
<% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
<tr>
<% if idx.zero? %>
<th rowspan="<%= @vectors.width %>"></th>
<% end %>
<% tuple.each do |idx, span| %>
<th colspan="<%= span %>"><%= idx %></th>
<% end %>
</tr>
<% end %>
<% else %>
<tr>
<th></th>
<% @vectors.each do |vector| %>
<th><%=vector%></th>
<% end %>
</tr>
<% end %>
</thead>
28 changes: 3 additions & 25 deletions lib/daru/iruby/templates/vector.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<b> Daru::Vector(<%= size %>)<%= ':category' if category? %> </b>
<table>
<tr>
<th colspan="2">Daru::Vector(<%= size %>)<%= ':category' if category? %></th>
</tr>
<% if name %>
<tr>
<th> </th>
<th><%= name %></th>
</tr>
<% end %>
<% @index.each_with_index.first(threshold).each do |index, pos| %>
<tr>
<td><%= index %></td>
<td><%= self.at(pos) %></td>
</tr>
<% end %>
<% if size > threshold %>
<% last_index = @index.size-1 %>
<tr><td>...</td><td>...</td></tr>
<tr>
<td><%= last_index %></td>
<td><%= self.at last_index %></td>
</tr>
<% end %>
<%= table_thead %>
<%= table_tbody %>
</table>
37 changes: 3 additions & 34 deletions lib/daru/iruby/templates/vector_mi.html.erb
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
<b> Daru::Vector(<%= size %>)<%= ':category' if category? %> </b>
<table>
<tr>
<th colspan="<%= index.width+1 %>">Daru::Vector(<%= size %>)<%= ':category' if category? %></th>
</tr>
<% if name %>
<tr>
<th colspan="<%= index.width %>"> </th>
<th><%= name %></th>
</tr>
<% end %>
<% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(to_a).each do |tuple, value| %>
<tr>
<% tuple.each do |idx, span| %>
<th rowspan="<%= span %>"><%= idx %></th>
<% end %>
<td><%= value %></td>
</tr>
<% end %>
<% if size > threshold %>
<% last_index = @index.to_a.last %>
<tr>
<% last_index.size.times do %>
<th>...</th>
<% end %>
<td>...</td>
</tr>
<tr>
<% last_index.each do |idx| %>
<th><%= idx %></td>
<% end %>
<td><%= self[last_index] %></td>
</tr>
<% end %>
<%= table_thead %>
<%= table_tbody %>
</table>
26 changes: 26 additions & 0 deletions lib/daru/iruby/templates/vector_mi_tbody.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<tbody>
<% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(to_a).each do |tuple, value| %>
<tr>
<% tuple.each do |idx, span| %>
<th rowspan="<%= span %>"><%= idx %></th>
<% end %>
<td><%= value %></td>
</tr>
<% end %>
<% if size > threshold %>
<% last_index = @index.to_a.last %>
<tr>
<% last_index.size.times do %>
<th>...</th>
<% end %>
<td>...</td>
</tr>
<tr>
<% last_index.each do |idx| %>
<th><%= idx %></td>
<% end %>
<td><%= self[last_index] %></td>
</tr>
<% end %>
</tbody>
8 changes: 8 additions & 0 deletions lib/daru/iruby/templates/vector_mi_thead.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<thead>
<% if name %>
<tr>
<th colspan="<%= index.width %>"> </th>
<th><%= name %></th>
</tr>
<% end %>
</thead>
17 changes: 17 additions & 0 deletions lib/daru/iruby/templates/vector_tbody.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<tbody>
<% @index.each_with_index.first(threshold).each do |index, pos| %>
<tr>
<td><%= index %></td>
<td><%= self.at(pos) %></td>
</tr>
<% end %>
<% if size > threshold %>
<% last_index = @index.size-1 %>
<tr><td>...</td><td>...</td></tr>
<tr>
<td><%= last_index %></td>
<td><%= self.at last_index %></td>
</tr>
<% end %>
</tbody>
Loading

0 comments on commit b6839c7

Please sign in to comment.