2016-11-11 62 views
6

我有一個jupyter筆記本電池,看起來像這樣:彈出式視窗/擴展jupyter細胞到新的瀏覽器窗口

enter image description here

有什麼辦法彈出/擴展了這一個新的瀏覽器窗口(沒有看到輸出內聯)?

基本上,我想複製R/RStudio中的View()函數...這可能嗎?

+1

你可以嘗試使用時間HTML文件一樣http://stackoverflow.com/questions/39977117/ipython-display-full-dataframe-in-new-tab和http://stackoverflow.com/新問題/ 37439014 /可能爲熊貓數據框將在新窗口中呈現 或在其他窗口中顯示數據:Excel,PyQt或tkinter http://stackoverflow.com/questions/10636024/python-pandas-gui-for-viewing-a-dataframe-or-matrix –

回答

12

您可以使用Javascript打開一個新窗口,執行HTMLIPython.display

import pandas as pd 
import numpy as np 
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD')) 
# Show in Jupyter 
df 

from IPython.display import HTML 
s = '<script type="text/Javascript">' 
s += 'var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));' 
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';' 
s += '</script>' 

# Show in new Window 
HTML(s) 

這裏,df.to_HTML()創建從含有大量換行符的數據幀中的HTML字符串。這些對於Javascript來說是有問題的。 JavaScript中的多行字符串需要在EOL處有一個反斜槓,這就是爲什麼python必須使用.replace()方法修改HTML字符串的原因。

什麼是真的很酷使用JavaScript的(而不是document.write().innerHTML是,你可以更新表中的任何時間,而不需要創建一個新的窗口:

df /= 2 
s = '<script type="text/Javascript">' 
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';' 
s += '</script>' 
HTML(s) 

這對在你的桌子即時效果打開窗戶。

enter image description here

這裏是R一個View()器因python一個簡單的建議:

def View(df): 
    css = """<style> 
    table { border-collapse: collapse; border: 3px solid #eee; } 
    table tr th:first-child { background-color: #eeeeee; color: #333; font-weight: bold } 
    table thead th { background-color: #eee; color: #000; } 
    tr, th, td { border: 1px solid #ccc; border-width: 1px 0 0 1px; border-collapse: collapse; 
    padding: 3px; font-family: monospace; font-size: 10px }</style> 
    """ 
    s = '<script type="text/Javascript">' 
    s += 'var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));' 
    s += 'win.document.body.innerHTML = \'' + (df.to_html() + css).replace("\n",'\\') + '\';' 
    s += '</script>' 

    return(HTML(s+css)) 

這個作品在jupyter只需鍵入:

View(df) 

由於看中摘心,它還使用一些CSS來設置打開的表格的樣式,使其看起來更加美觀和c與你從RStudio知道的無關。
enter image description here

+0

。對於任何想要實現這個目標的人來說,這只是一個提示:確保你不阻止本地主機上的彈出窗口! – emehex

相關問題