Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output to HTML added via writemime #433

Merged
merged 1 commit into from
Dec 6, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,42 @@ function writetable(filename::String,
return
end

##############################################################################
#
# HTML output
#
##############################################################################
function html_escape(cell::String)
cell = replace(cell, "&", "&")
cell = replace(cell, "<", "&lt;")
cell = replace(cell, ">", "&gt;")
return cell
end

function Base.writemime(io::IO,
::MIME"text/html",
df::DataFrame)
n = size(df, 1)
column_names = colnames(df)
write(io, "<table>")
write(io, "<tr>")
write(io, "<th></th>")
for column_name in column_names
write(io, "<th>$column_name</th>")
end
write(io, "</tr>")
for row in 1:n
write(io, "<tr>")
write(io, "<th>$row</th>")
for column_name in column_names
cell = string(df[row, column_name])
write(io, "<td>$(html_escape(cell))</td>")
end
write(io, "</tr>")
end
write(io, "</table>")
end

##############################################################################
#
# Binary serialization
Expand Down