Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
Created September 6, 2013 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egonSchiele/6458158 to your computer and use it in GitHub Desktop.
Save egonSchiele/6458158 to your computer and use it in GitHub Desktop.
Display a bunch of data from a hive table in a pretty HTML table
#!/usr/bin/env ruby
require 'tempfile'
def make_row cols, style=""
"<tr style='#{style}'><td>" + cols.join("</td><td>") + "</td></tr>"
end
if ARGV.empty?
puts %{
Usage: #{$0} [file] where file has col list for a hive table, then a blank line, then some sample rows
Example:
year string
month string
day string
2013 04 05
2013 04 06
}
exit
end
file = ARGV[0]
lines = File.readlines(file)
cols = []
data = []
reading_data = false
lines.each do |line|
# we've read all the cols, now it's time to read data
if line.chomp.empty?
reading_data = true
next
end
if reading_data
data << line.split("\t")
else
cols << line.split("\t")[0]
end
end
# make table
contents = %{
<html>
<head>
<style>
table {
border: 1px solid black;
}
td {
border: 1px solid black;
}
tr:first-child {
font-weight: bold;
font-size: 24px;
}
tr:nth-child(odd) {
background: #ddd;
}
td {
padding: 3px;
max-width: 400px;
word-wrap: break-word;
}
</style>
</head>
<body>
<table>
#{make_row(cols)}
#{data.map{ |d| make_row(d) }.join("\n")}
</table>
</body>
</html>
}
tmp = Tempfile.new(['pretty_hive', ".html"]) # seed string, can be anything
tmp.write(contents)
tmp.flush
`open #{tmp.path}`
sleep 2
tmp.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment