The home page for ipy_table is at epmoyer.github.com/ipy_table/
ipy_table is maintained at github.com/epmoyer/ipy_table
import add_parent_to_path
To create a table call make_table on an array (a list of equal sized lists) or a numpy.ndarray
.
make_table()
creates a table in interactive mode. Subsequent calls to modify styles (e.g. apply_theme()
, set_cell_style()
, etc.) will re-render the table with the new style modifications.
from ipy_table import *
example_table = [[i for i in range(j,j+4)] for j in range(0,30,10)]
make_table(example_table)
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
ipy_table implements three pre-defined table styles (basic, basic_left, and basic_both) which provide bold gray headers and alternating colored rows for three different header configurations.
make_table(example_table)
apply_theme('basic')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
apply_theme('basic_left')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
import copy
example_table2 = copy.deepcopy(example_table) # Copy the example table
example_table2[0][0] = '' # Clear the contents of the upper left corner cell
make_table(example_table2)
apply_theme('basic_both')
1 | 2 | 3 | |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the style of a single cell. For a list of the available style options, see Syle Options below.
make_table(example_table)
set_cell_style(1, 2, color='red')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the style for a row of cells. For a list of the available style options, see Syle Options below.
make_table(example_table)
set_row_style(0, color='lightGreen')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the style for a column of cells. For a list of the available style options, see Syle Options below.
make_table(example_table)
set_column_style(1, color='lightBlue')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the style for all cells. For a list of the available style options, see Syle Options below.
make_table(example_table)
set_global_style(color='Pink')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
set_row_style(1, bold=True)
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
set_row_style(1, italic=True)
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets background cell color by name. The color name can be any any standard web/X11 color name. For a list see http://en.wikipedia.org/wiki/Web_colors
make_table(example_table)
set_row_style(1, color='Orange')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Accepts a comma delimited list of cell edges, which may be any of: left, top, right, bottom. You can also speify 'all' to include all edges.
make_table(example_table)
set_cell_style(0,0, thick_border='left,top')
set_cell_style(2,3, thick_border='right,bottom')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
set_row_style(1, thick_border='all')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Accepts a comma delimited list of cell edges, which may be any of: left, top, right, bottom. You can also speify 'all' to include all edges.
make_table(example_table)
set_cell_style(0,0, no_border='left,top')
set_cell_style(2,3, no_border='right,bottom')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
set_row_style(1, no_border='all')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table)
set_cell_style(0, 0, row_span=3)
0 | 1 | 2 | 3 |
11 | 12 | 13 | |
21 | 22 | 23 |
make_table(example_table)
set_cell_style(1,1, column_span=3)
0 | 1 | 2 | 3 |
10 | 11 | ||
20 | 21 | 22 | 23 |
Sets the cell width in pixels.
make_table(example_table)
set_cell_style(0,0, width=100)
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the cell alignment. Accpets any of: left, right, center.
make_table(example_table)
set_cell_style(0, 0, width='100')
set_cell_style(0, 0, align='right')
set_cell_style(1, 0, align='center')
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Turns text wrapping on or off. By default wraping is off.
example_table2 = copy.deepcopy(example_table)
example_table2[0][0] = 'This cell has wrap set'
example_table2[0][1] = 'This cell does not have wrap set'
make_table(example_table2)
set_cell_style(0, 0, width=50,wrap=True)
set_cell_style(0, 1, width=50)
This cell has wrap set | This cell does not have wrap set | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
Sets the display format for floating point values.
The float format string is a standard Python "%" format string (and should contain one and only one %f reference). See http://docs.python.org/2/library/stdtypes.html#string-formatting-operations
The float format only affects cells that contain float
or numpy.float64
data types, so you can use set_global_style to set a global floating point format and only those cells containing floating point data will be affected.
The default float format is '%0.4f'.
from ipy_table import *
example_table2 = [[i + float(i)/100.0 + i/10000.0 for i in range(j,j+4)] for j in range(0,30,10)]
make_table(example_table2)
set_cell_style(0, 0, float_format='%0.1f')
set_cell_style(1, 0, float_format='%0.6f')
set_cell_style(2, 0, float_format='$%0.2f')
0.0 | 1.0101 | 2.0202 | 3.0303 |
10.101000 | 11.1111 | 12.1212 | 13.1313 |
$20.20 | 21.2121 | 22.2222 | 23.2323 |
t = IpyTable(example_table)
t.set_cell_style(1, 1, color='DarkCyan')
t
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
make_table(example_table, interactive=False)
set_row_style(1,color='yellow')
render()
0 | 1 | 2 | 3 |
10 | 11 | 12 | 13 |
20 | 21 | 22 | 23 |
The HTML text representation of the current table can be obtained by calling render()._repr_html_()
render()._repr_html_()
'<table border="1" cellpadding="3" cellspacing="0" style="border:black; border-collapse:collapse;"><tr><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">0</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">1</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">2</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">3</td></tr><tr><td style="background-color:yellow;border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">10</td><td style="background-color:yellow;border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">11</td><td style="background-color:yellow;border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">12</td><td style="background-color:yellow;border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">13</td></tr><tr><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">20</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">21</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">22</td><td style="border-top: 1px solid;border-right: 1px solid;border-bottom: 1px solid;border-left: 1px solid;">23</td></tr></table>'
Use tabulate(list, n)
to display a list (not an array) of data in a table with n columns.
tabulate(range(20), 6)
0 | 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 |
tabulate()
creates a table object just like make_table()
, so the same style operations can be applied.
set_cell_style(1, 2, color='yellow')
0 | 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 |
import ipy_table as ipt
ipt.__version__
'1.15.0'