I'd like to produce a data file that is downloaded by the user, either automatically or through a hyperlink.
For instance, is there an equivalent of this iPython approach for iJulia?
I'd like to produce a data file that is downloaded by the user, either automatically or through a hyperlink.
For instance, is there an equivalent of this iPython approach for iJulia?
If I understand your question correctly, you're looking to auto-generate a link in Jupyter Notebook, such that people can download? If so, I do this in Vega.jl (so people can download PNG files of graphs) with the following:
window.setTimeout(function() {
var pnglink = document.getElementById(\"$divid\").getElementsByTagName(\"canvas\")[0].toDataURL(\"image/png\")
document.getElementById(\"$divid\").insertAdjacentHTML('beforeend', '<br><a href=\"' + pnglink + '\" download>Save as PNG</a>')
}, 20);
https://github.com/johnmyleswhite/Vega.jl/blob/master/src/render.jl#L65-L69
Basically, this code finds the div that the output is in (which is known at the time of Julia code running, before the output is rendered), then auto-generates an HTML link with the base64 representation of the PNG. Depending on your content, this obviously may differ (since the .toDataURL
method has to have your file type in the method).
I translated from original FileLink
definition to julia:
type FileLink
file_path::ByteString
end
type FileLinks
links::Vector{FileLink}
end
FileLinks(paths::Vector{ByteString}) = FileLinks(map(FileLink,paths))
function Base.writemime(st::IO, ty::MIME"text/html", fl::FileLink)
write(st, "<a href=$(fl.file_path) target='_blank'>$(fl.file_path)</a>")
end
function Base.writemime(st::IO, ty::MIME"text/html", file_links::FileLinks)
for fl in file_links.links
Base.writemime(st, ty, fl)
write(st,"<br>")
end
end
FileLinks(readdir("."))
Works on locally hosted IJulia/IPython/Jupyter server, but you may have problems with remote servers (eg Sage).
Using NBViewer to serve Jupyter notebooks statically works also with IJulia.
As shown in it's home page:
In this example I give NBViewer the link of one of my IJulia notebooks that is hosted in Gist, notice that GitHub/Gist now also renders Jupyter noteboks: