“斑马表”在IPython笔记本中? [英] "Zebra Tables" in IPython Notebook?
问题描述
我正在使用奇妙的笔记本进行交互式分析和Pandas在IPython中构建一些交互式工作流程。
I'm building some interactive workflows in IPython using the fantastic Notebook for interactive analysis and Pandas.
我正在显示的一些表格会更容易阅读一点格式化。我真的很喜欢像斑马桌这样的东西,其中每一行都有阴影。我在此阅读,了解这种格式如何通过css实现。是否有一种非常直接的方法将css应用于 IPython Notebook ,然后使用样式表呈现表格?
Some of the tables I'm displaying would be much easier to read with a little bit of formatting. I'd really like something like "zebra tables" where every other row is shaded. I read here about how this formatting can be implemented via css. Is there a really straight forward way to apply a css to an IPython Notebook and then have tables rendered using the style sheet?
推荐答案
您可以在< script> <中的降价单元格中运行任意javascript(使用jQuery) / code>标签,或通过IPython的
IPython.core.display.Javascript
类。有了这些,您可以根据内容操作(或破坏)文档,包括添加样式表。
You can run arbitrary javascript (with jQuery) either in markdown cells inside <script>
tags, or via IPython's IPython.core.display.Javascript
class. With these, you can manipulate (or ruin) the document to your heart's content, including adding stylesheets.
例如,以下内容将对适当的分类表进行条带化:
For instance, the following will stripe appropriately classed tables:
<script type="text/javascript">
$('head').append(
"<style type='text/css'>tr.odd{background-color: #def}</style>"
);
</script>
如果您只是坚持在其中一个降价单元格中,那么它将适用于页面上的所有内容。
If you just stick that in one of your markdown cells, then it will apply to everything on the page.
或者,您可以在代码中从Python运行相同的代码(减去< script>
标签)单元格:
Or, you might run the same code (minus <script>
tags) from Python in a code cell:
from IPython.core.display import Javascript, display
display(Javascript("""
$('head').append(
"<style type='text/css'>tr.odd{background-color: #def}</style>"
);
"""))
但这样做只会影响适当分类的桌子,这取决于编写HTML的代码(例如在Pandas中)。
But doing this will only affect your tables that are appropriately classed, which is up to the code that is writing the HTML (e.g. in Pandas).
这篇关于“斑马表”在IPython笔记本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!