2014-09-26 96 views
13

我有一個ipython筆記本,它具有來自本地驅動器的嵌入式圖像。我期待它與代碼單元的輸出一起嵌入到JSON中,但是當我分發筆記本時,圖像不會顯示給用戶。在Notebook中嵌入圖像的推薦方法是什麼,以便在用戶重新運行代碼單元,清除單元輸出等時不會消失?將圖像嵌入到ipython筆記本中進行發佈

筆記本電腦系統緩存了包含在![label](image.png)中的圖像,但它們只持續到服務筆記本的python「內核」重新啓動爲止。如果我在磁盤上重命名圖像文件,我可以關閉並重新打開筆記本,並且它仍然顯示圖像;但是當我重新啓動內核時它會消失。

編輯:如果我生成圖像作爲代碼細胞輸出然後導出筆記本到html,圖像被嵌入在HTML作爲編碼數據。當然,必須有辦法掛鉤到這個功能,並將輸出加載到markdown(或更好的「原始nbconvert」)單元格中?

from IPython.display import Image 
Image(filename='imagename.png') 

將出口(與ipython nbconvert)爲HTML,它包含以下內容:

<div class="output_png output_subarea output_execute_result"> 
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA... 
</div> 

然而,即使我手動嵌入段成降價細胞,我不能得到要顯示的圖像。我究竟做錯了什麼?

PS現有(舊)答案給出了一些非常有用的指針,但不是解決方案。

+0

可惜沒有人在這段時間內回答過這個問題!現在有沒有解決方案? – alexis 2015-04-22 14:10:28

+0

我遇到了完全相同的問題。顯然,進入' tel 2015-10-01 06:03:38

+0

Yeah, that's reasonable, and I had expected some sort of filter. Actually I'd be surprised if originally there was no filter at all-- it's more likely that was just strengthened in version 2. But the question is still, _is_ there some method that gets past the filters? – alexis 2015-10-01 15:32:10

回答

5

你是否樂意使用額外的代碼單元來顯示圖像?如果是這樣,使用此:

from IPython.display import Image 
Image(filename="example.png") 

輸出單元將具有嵌入.ipynb文件中的原始圖像數據,這樣你就可以分享和圖像將被保留。

注意,Image類也有一個url關鍵字,但這隻會鏈接到圖像,除非你也指定embed=True(見documentation瞭解詳細信息)。因此,除非您指的是遠程服務器上的映像,否則使用filename關鍵字更爲安全。

如果您需要將圖像包含在Markdown單元中,即沒有單獨的代碼單元來生成嵌入的圖像數據,我不確定是否有簡單的解決方案。您可以使用python markdown extension,它允許在降價單元格中動態顯示Python變量的內容。但是,該擴展會動態生成降格單元,因此爲了在共享筆記本時保留輸出,需要使用「安裝」一節中提到的預處理器pymdpreprocessor.py運行ipython nbconvert --to notebook original_notebook.ipynb --output preprocessed_notebook。生成的筆記本將數據作爲格式爲<img src="data:image/png;base64,...">的HTML標籤嵌入到降格單元中,因此您可以從preprocessed_notebook.ipynb中刪除相應的代碼單元格。不幸的是,當我嘗試這個時,<img>標籤的內容實際上並沒有顯示在瀏覽器中,所以不確定這是否是一個可行的解決方案。 : -/

不同的選擇是在代碼單元中使用Image類生成上面的圖像,然後使用nbconvert和自定義模板從筆記本中刪除代碼輸入單元。詳情請參閱this thread。但是,這將剝離所有來自轉換筆記本的代碼單元,因此它可能不是您想要的。

+0

Thanks! I don't _require_ the image to be in a markdown cell, but using code is (a) distracting, since the code cell cannot be hidden; and (b) more crucially, it's unsafe because these are notebooks for programming practice and the user can be expectedto clear cell outputs now and then. – alexis 2015-05-04 17:04:40

+0

PS. Thanks for the nbconvert thread... I've been gradually piling up my own conversion scripts because nbconvert's guts are completely opaque (and not very well documented). Maybe this will lead me to a source of better explanations. – alexis 2015-05-04 17:08:05

+0

Not sure if this is relevant, but maybe [this example](https://github.com/maxalbert/auto-exec-notebook) can also be helpful to better understand 'nbconvert' and automated notebook execution. I haven't looked at it in detail, but in my limited experience the nbconvert machinery seems to be relatively tidy in the latest version of IPython (and potentially simpler than in previous versions due to the simplification of the notebook format itself). – cilix 2015-05-04 19:03:43

2

之所以當你把它放在一個降價細胞中

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA... 

標籤沒有做任何事情,是因爲IPython中使用HTML清潔劑(一種叫做Google Caja)是篩選出這個類型的標籤(和許多其他)在它可以被呈現之前。

在IPython中的HTML消毒劑可以通過添加下面一行到你的custom.js文件(通常位於~/.ipython/profile_default/static/custom/custom.js)被完全禁用:

iPython.security.sanitize_html = function (html) { return html; }; 

這不是一個很好的解決方案,雖然,因爲它帶來安全風險,這對分配並沒有太大的幫助。

後記
呈現base64編碼字符串作爲圖像=明顯的安全問題,所以應該有卡哈人最終允許這類事情的經過(雖然the related feature request ticket第一次打開背部的方式的能力!在2012年,所以不要屏住呼吸)。

+1

That's a good lead! Notebooks have a concept of "trusted" notebook, implemented (I think) as a cryptographic key once a notebook has been inspected by the user. The reasonable thing to do would be to relax html sanitizing for trusted notebooks. Any ideas on how this could be set up? – alexis 2015-10-01 18:43:26

+1

@alexis *le sigh* I tried that too. Clicking though the 'File -> Trust Notebook'菜單對話框的原因似乎不會以某種方式影響HTML清理。不過,你說得對。我想它必須在IPython代碼庫中實現。也許你或我會考慮提交拉取請求? – tel 2015-10-01 20:49:38

+0

不是我,我不知道如何在受信任的屬性(或實際放置的位置)上設置此條件。我不確定完全禁用消毒是否正確 - 應該有更大的功能白名單通過。如果你有足夠的興趣去做,我會非常好奇,看看開發者是否接受了這個想法! – alexis 2015-10-02 12:07:59