- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将 jupyter 在他们的笔记本中用于 pandas 数据帧的输出复制到 html/css/js,以便 Flask jsonify
可以将其返回为我稍后在 AJAX 中使用的 html称呼。
我找到了 this , 和 this ,建议使用 pandas 内置样式功能而不是 CSS hack,但我正在努力获得所需的功能:
def hover(hover_color="#add8e6"):
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
hover(),
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("padding", "5px 5px")]),
dict(selector="tr", props=[("text-align", "center")]),
dict(selector="caption", props=[("caption-side", "bottom")])
]
# creating some dummy data
index = pd.MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],
labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],
names=['first', 'second'])
df = pd.DataFrame(data=np.random.randn(8), index=index)
# you'll see the html rendered bellow
df.style.set_table_styles(styles).set_caption("test").render()
与 jupyter notebooks 相比,这缺少一些默认的背景颜色剥离,并且标题不应该应用悬停类。我认为在选择元素上应用某些东西的唯一方法是添加 class=
或 id=
但样式功能隐藏了所有这些。
<style type="text/css" > #T_479b61ba_292a_11e8_86bf_0ee09a5428a2 tr:hover { background-color: #add8e6; } #T_479b61ba_292a_11e8_86bf_0ee09a5428a2 th { font-size: 150%; text-align: center; } #T_479b61ba_292a_11e8_86bf_0ee09a5428a2 caption { caption-side: bottom; }</style> <table id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2" ><caption>Hover to highlight.</caption> <thead> <tr> <th class="blank" ></th> <th class="blank level0" ></th> <th class="col_heading level0 col0" >0</th> </tr> <tr> <th class="index_name level0" >first</th> <th class="index_name level1" >second</th> <th class="blank" ></th> </tr></thead> <tbody> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level0_row0" class="row_heading level0 row0" rowspan=2>bar</th> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row0" class="row_heading level1 row0" >one</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row0_col0" class="data row0 col0" >-0.0690895</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row1" class="row_heading level1 row1" >two</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row1_col0" class="data row1 col0" >-0.518092</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level0_row2" class="row_heading level0 row2" rowspan=2>baz</th> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row2" class="row_heading level1 row2" >one</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row2_col0" class="data row2 col0" >-0.163842</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row3" class="row_heading level1 row3" >two</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row3_col0" class="data row3 col0" >-0.144757</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level0_row4" class="row_heading level0 row4" rowspan=2>foo</th> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row4" class="row_heading level1 row4" >one</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row4_col0" class="data row4 col0" >1.22865</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row5" class="row_heading level1 row5" >two</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row5_col0" class="data row5 col0" >1.83947</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level0_row6" class="row_heading level0 row6" rowspan=2>qux</th> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row6" class="row_heading level1 row6" >one</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row6_col0" class="data row6 col0" >0.793328</td> </tr> <tr> <th id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2level1_row7" class="row_heading level1 row7" >two</th> <td id="T_479b61ba_292a_11e8_86bf_0ee09a5428a2row7_col0" class="data row7 col0" >-0.723836</td> </tr></tbody> </table>
最佳答案
我对您的代码做了一些更改以获得您想要的功能:
thead
和用于正文的 tbody
。我们可以使用它来将突出显示行为指定为仅主体在 Jupyter Notebook 中:
import pandas as pd
import numpy as np
from IPython.display import HTML
def hover(hover_color="#add8e6"):
return dict(selector="tbody tr:hover",
props=[("background-color", "%s" % hover_color)])
styles = [
#table properties
dict(selector=" ",
props=[("margin","0"),
("font-family",'"Helvetica", "Arial", sans-serif'),
("border-collapse", "collapse"),
("border","none"),
("border", "2px solid #ccf")
]),
#header color - optional
dict(selector="thead",
props=[("background-color","#cc8484")
]),
#background shading
dict(selector="tbody tr:nth-child(even)",
props=[("background-color", "#fff")]),
dict(selector="tbody tr:nth-child(odd)",
props=[("background-color", "#eee")]),
#cell spacing
dict(selector="td",
props=[("padding", ".5em")]),
#header cell properties
dict(selector="th",
props=[("font-size", "125%"),
("text-align", "center")]),
#caption placement
dict(selector="caption",
props=[("caption-side", "bottom")]),
#render hover last to override background-color
hover()
]
html = (df.style.set_table_styles(styles)
.set_caption("Hover to highlight."))
html
...但是当我们输出 HTML 文件时它还漂亮吗??是的。你可以做更多的 CSS 样式来让它恰到好处(字体大小、字体系列、文本装饰、边距/填充等),但这给了你一个开始。见下文:
print(html.render())
<style type="text/css" >
#T_3e73cfd2_396c_11e8_9d70_240a645b34fc {
margin: 0;
font-family: "Helvetica", "Arial", sans-serif;
border-collapse: collapse;
border: none;
border: 2px solid #ccf;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc thead {
background-color: #cc8484;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc tbody tr:nth-child(even) {
background-color: #fff;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc tbody tr:nth-child(odd) {
background-color: #eee;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc td {
padding: .5em;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc th {
font-size: 125%;
text-align: center;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc caption {
caption-side: bottom;
} #T_3e73cfd2_396c_11e8_9d70_240a645b34fc tbody tr:hover {
background-color: #add8e6;
}</style>
<table id="T_3e73cfd2_396c_11e8_9d70_240a645b34fc" ><caption>Hover to highlight.</caption>
<thead> <tr>
<th class="blank" ></th>
<th class="blank level0" ></th>
<th class="col_heading level0 col0" >0</th>
</tr> <tr>
<th class="index_name level0" >first</th>
<th class="index_name level1" >second</th>
<th class="blank" ></th>
</tr></thead>
<tbody> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel0_row0" class="row_heading level0 row0" rowspan=2>bar</th>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row0" class="row_heading level1 row0" >one</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow0_col0" class="data row0 col0" >-0.130634</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row1" class="row_heading level1 row1" >two</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow1_col0" class="data row1 col0" >1.17685</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel0_row2" class="row_heading level0 row2" rowspan=2>baz</th>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row2" class="row_heading level1 row2" >one</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow2_col0" class="data row2 col0" >0.500367</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row3" class="row_heading level1 row3" >two</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow3_col0" class="data row3 col0" >0.555932</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel0_row4" class="row_heading level0 row4" rowspan=2>foo</th>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row4" class="row_heading level1 row4" >one</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow4_col0" class="data row4 col0" >-0.744553</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row5" class="row_heading level1 row5" >two</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow5_col0" class="data row5 col0" >-1.41269</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel0_row6" class="row_heading level0 row6" rowspan=2>qux</th>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row6" class="row_heading level1 row6" >one</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow6_col0" class="data row6 col0" >0.726728</td>
</tr> <tr>
<th id="T_3e73cfd2_396c_11e8_9d70_240a645b34fclevel1_row7" class="row_heading level1 row7" >two</th>
<td id="T_3e73cfd2_396c_11e8_9d70_240a645b34fcrow7_col0" class="data row7 col0" >-0.683555</td>
</tr></tbody>
</table>
关于python - 复制 Jupyter Notebook Pandas 数据框 HTML 打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49324569/
在尝试安装 jupyter 的许多不同方法之后,它似乎没有正确安装。 根据我最近遇到的 MacOS 系统 python 问题,可能与 MacOS 相关 pip install jupyter --us
命名 Jupyter Notebook 时,如果使用空格,即 This is my notebook.ipynb 然后,当使用网络浏览器打开时,它的渲染效果非常好。然而,空格在命令行环境中是有害的。但
运行 jupyter notebook和 jupyter server给我非常相似的结果,描述也非常相似。 ❯ jupyter notebook -h The Jupyter HTML Noteboo
是否可以在 Jupyter Lab 中使用笔记本扩展 (nbextensions)? 我认为我的扩展已启用...我调用 jupyter nbextension enable事先在他们身上。但我没有在
我将 Jupyter 与 Anaconda3 结合使用。 我的 Anaconda3\ 和 Anaconda3\Scripts\ 文件夹已添加到 %PATH% 变量中。 尽管 jupyter.exe 位
我将 Jupyter 用于我公司的分析。我想制作显示一些漂亮图表的实时页面。我将在大厅的大显示器上显示此页面,我希望它自动刷新。 有什么方法可以通过刷新浏览器页面来触发“运行所有单元格”吗?或者,是否
%store 魔术功能可以保存大型 python 对象,供您在不同 session 之间使用,但我想找出文件的实际位置,以便我也可以在不同 session 之间传输它们不同的电脑。我正在使用 Wind
我在 Windows 10 的本地 Ubunto 机器上运行 Jupyter notebook。问题是所有文本都与屏幕右侧对齐,包括菜单 - 例如"file"选项卡位于最右侧。以这种方式阅读文本非常困
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-t
我尝试使用以下内容创建指向 jupyter 笔记本中标题的内部链接。 SO上的各种答案,例如here在我的笔记本中似乎没有按预期工作。下面的代码创建了一个链接,但在尝试访问该链接时没有任何 react
这个问题在这里已经有了答案: How can I share Jupyter notebooks with non-programmers? [closed] (6 个回答) 5年前关闭。 我安装了一
我试图在 jupyter notebook 中自动完成路径。按“tab”后,它显示的不仅仅是文件夹或文件。我认为这些是python的内置功能。有没有办法在自动完成路径时只显示路径和文件? 谢谢! 最佳
我在 Jupyter notebook 中组合了一些数据分析步骤。 随着数据的变化,我希望能够 重新运行所有单元格(以考虑新数据) 转换为html以供查看 我知道我可以通过 jupyter nbcon
在 jupyter 中 笔记本 , 我可以用 nbextensions 配置自动单元计时,结果是这样的: 我怎样才能在 jupyter 中做到这一点 实验室 ?我没有找到任何做类似事情的扩展。 观察:
我正在寻找一个带有 Jupter 可选组件的数据处理集群。 gcloud beta dataproc clusters create cluster-1ea3 --enable-component-g
Jupyter Notebook 的自动完成功能似乎有效,但不知何故它会显示该方法的重复选项。例如下面: 对于每个可能的选项,下拉菜单将显示 2 个相同的选项。为什么会发生这种情况以及如何解决? 最佳
每当我将 jupyter 实验室窗口推到一边时,“简单模式”就会被激活。这导致只显示一个选项卡,而其他选项卡被隐藏。这非常烦人,我不敢相信这是一个标准功能(只是再次安装了 jupyter 实验室)。
当我使用jupyter notebook时, 我希望新单元格的类型为“markdown”。 默认情况下,新单元格的类型为“代码”。我应该修改哪个配置文件以及应该更改哪个变量? 最佳答案 转到您的pyt
奇怪的是,互联网上没有关于如何在 Jupyter 环境下运行 Haskell 的说明。任何引导您实现此目的的文档都需要您从源代码编译,但是当尝试这样做时,会发生各种构建错误。 奇怪的是,一个肯定会给
在带有 ipython 内核的 Jupyter 中,是否有以非阻塞方式执行单元格的规范方法? 理想情况下,我希望能够运行一个单元格 %%background time.sleep(10) print(
我是一名优秀的程序员,十分优秀!