2016-03-02 85 views

回答

2

如果我正確理解你的問題,你正在尋找在Jupyter Notebook中自動生成鏈接,以便人們可以下載?如果是這樣,我這樣做是Vega.jl以下的(這樣的人可以下載圖表的PNG文件):

   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

基本上,這個代碼發現DIV輸出是在(這在運行Julia代碼時,在渲染輸出前已知),然後使用PNG的base64表示自動生成HTML鏈接。根據您的內容,這顯然可能會有所不同(因爲.toDataURL方法必須在方法中具有您的文件類型)。

2

我從原來的FileLink定義翻譯成朱莉婭:

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(".")) 

作品本地託管IJulia/IPython的/ Jupyter服務器上,但你可能有與遠程服務器(例如鼠尾草)的問題。