弹出/扩展jupyter单元到新的浏览器窗口 [英] Pop-out / expand jupyter cell to new browser window
问题描述
我有一个看起来像这样的jupyter笔记本电脑电池:
I have a jupyter notebook cell that looks like this:
是否可以将其弹出/扩展到新的浏览器窗口(看不到内联输出)?
Is there any way to pop / expand out this to a new browser window (not see the output inline)?
基本上,我想从R/RStudio复制View()
函数...这可能吗?
Basically, I want to replicate the View()
function from R/RStudio... is this possible?
推荐答案
您可以使用Javascript打开由IPython.display
中的HTML
执行的新窗口.
You could use Javascript to open a new window, executed by HTML
from IPython.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字符串的原因.
Here, df.to_HTML()
creates a HTML string from the data frame which contains lots of newlines. Those are problematic for Javascript. Multi-line strings in Javascript require a backslash at the EOL, that's why python has to modify the HTML string with the .replace()
method.
JavaScript的.innerHTML
(而不是document.write()
)真正的酷之处在于,您可以随时更新表,而无需创建新窗口:
What's really cool with JavaScript's .innerHTML
(instead of document.write()
) is that you can update your table any time without the need to create a new window:
df /= 2
s = '<script type="text/Javascript">'
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';'
s += '</script>'
HTML(s)
这将在打开的窗口中对您的表立即生效.
This will have instant effect on your table in the opened window.
这是R
中python
的View()
模拟器的简单建议:
Here is a simple suggestion of an View()
emulator from R
for 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中工作:
This works in jupyter simply by typing:
View(df)
作为一个漂亮的开头,它还使用一些CSS设置了打开的表格的样式,以使其看起来比RStudio
更好和可比.
As a fancy topping, it also styles your opened table using some CSS, such that it looks much nicer and comparable to what you know from the RStudio
.
这篇关于弹出/扩展jupyter单元到新的浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!