eme*_*hex 11 python jupyter jupyter-notebook
我有一个jupyter笔记本电池,看起来像这样:
有没有办法将其弹出/扩展到新的浏览器窗口(不看内联输出)?
基本上,我想View()
从R/RStudio 复制这个功能......这可能吗?
Mar*_*tin 18
您可以使用Javascript打开一个新窗口,由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)
Run Code Online (Sandbox Code Playgroud)
在这里,df.to_HTML()
从包含许多换行符的数据框创建一个HTML字符串.这些都是Javascript的问题.Javascript中的多行字符串需要在EOL处使用反斜杠,这就是python必须使用该.replace()
方法修改HTML字符串的原因.
使用JavaScript .innerHTML
(而不是document.write()
)的真正酷处是你可以随时更新你的表而无需创建新窗口:
df /= 2
s = '<script type="text/Javascript">'
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';'
s += '</script>'
HTML(s)
Run Code Online (Sandbox Code Playgroud)
这将在打开的窗口中对您的表格产生即时效果.
下面是一个简单的建议View()
从模拟器R
为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))
Run Code Online (Sandbox Code Playgroud)
只需键入以下内容即可在jupyter中运行:
View(df)
Run Code Online (Sandbox Code Playgroud)
作为一个花哨的顶部,它还使用一些CSS来打开你打开的表的样式,这样它看起来更好,可以与你所知道的相比RStudio
.
归档时间: |
|
查看次数: |
5453 次 |
最近记录: |