markdown 使用python在jupyter上并排显示表的4种不同方法。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 使用python在jupyter上并排显示表的4种不同方法。相关的知识,希望对你有一定的参考价值。

source : https://stackoverflow.com/questions/38783027/jupyter-notebook-display-two-pandas-tables-side-by-side

# METHOD 1

```python
from IPython.display import display, HTML
CSS = """
.output {
flex-direction: row;
}
"""

HTML('<style>{}</style>'.format(CSS))

display(df)
```

# METHOD 2

```python
from IPython.display import display_html
def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str += df.to_html()
        html_str += '\t'
    display_html(html_str.replace('table','table style="display:inline"'), raw=True)
```

# METHOD 2

```python
from IPython.display import display_html
def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str += df.to_html()
        html_str += '\t'
    display_html(html_str.replace('table','table style="display:inline"'), raw=True)
```

# METHOD 3

```python
from IPython.display import display, HTML

def multi_column_df_display(list_dfs, cols=3):
    html_table = "<table style='width:100%; border:0px'>{content}</table>"
    html_row = "<tr style='border:0px'>{content}</tr>"
    html_cell = "<td style='width:{width}%;vertical-align:top;border:0px'>"\
                "{{content}}</td>"
    html_cell = html_cell.format(width=100/cols)
    cells = [ html_cell.format(content=df.to_html()) for df in list_dfs ]
    cells += (cols - (len(list_dfs)%cols)) * [html_cell.format(content="")] # pad
    rows = [html_row.format(content="".join(cells[i:i+cols])) for i in range(0, len(cells), cols)]
    display(HTML(html_table.format(content="".join(rows))))
```

# METHOD 4

```python
class display(object):
    """Display HTML representation of multiple objects"""
    template = """<div style="float: left; padding: 10px;">
    <p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
    </div>"""

    def __init__(self, *args):
        self.args = args

    def _repr_html_(self):
        return '\n'.join(self.template.format(a, eval(a)._repr_html_())
                     for a in self.args)

    def __repr__(self):
       return '\n\n'.join(a + '\n' + repr(eval(a))
                       for a in self.args)
```

以上是关于markdown 使用python在jupyter上并排显示表的4种不同方法。的主要内容,如果未能解决你的问题,请参考以下文章

markdown jupyter,ipython,python

3Python的IDE之Jupyter的使用

在jupyter笔记本中设置markdown内联的字体大小

Python的IDE之Jupyter的使用

pycharm / intellij - jupyter markdown 不渲染

jupyter通过notedown使用markdown