ZK Zhao
ZK Zhao

Reputation: 21573

IPython Notebook & Pandas: How does pandas produce html table?

The output of pandas dataframe is an HTML table, I want to know how it produce the html into Ipython Notebook?

enter image description here

<div class="output_subarea output_html rendered_html output_result">
  <div>
   <table class="dataframe" border="1">
     <thead>
       <tr style="text-align: right">

Because I also want to wrap my output with some html, I want to know how. (Ipython Notebook & Matplotlib: How to wrap a plot within a html div?)

I'm now digging the source code at: https://github.com/pydata/pandas/blob/2d51b33066e5991913dcba2cfb93d2112a2e8a8f/pandas/core/format.py

Upvotes: 2

Views: 495

Answers (1)

AChampion
AChampion

Reputation: 30288

In IPython you can display arbitrary HTML by importing ipython's display module. Pandas can convert a DataFrame to html which you can then apply your updates to, e.g.:

import pandas as pd
from IPython import display
df = pd.DataFrame(...)
display.HTML(df.to_html())

Should display the same html table as above.

DataFrame.to_html() returns a http string, so you can do simple string manipulation to wrap your text:

display.HTML('<div style="visibility:hidden">'+df.to_html()+'</div>')

Would hide the table.

Upvotes: 3

Related Questions