Skip to content

Instantly share code, notes, and snippets.

@shabbychef
Created January 3, 2013 17:50
Show Gist options
  • Save shabbychef/4445333 to your computer and use it in GitHub Desktop.
Save shabbychef/4445333 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "ipy_table-Introduction"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ipy_table is a supporting module for IP[y]:Notebook which makes it easy to create richly formatted data tables. "
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a table in interactive mode, import ipy_table and call ``make_table()`` on an array.\n",
"\n",
"Notes:\n",
"\n",
"* ipy_table can accept either a \"native\" array (a list of equal-length lists) or a ``numpy.ndarray``. \n",
"* Arrays passed to ipy_table typically contain integers, floats or strings, but in general they can contain other object types and ipy_table will render the result of calling ``str()`` on those objects."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from ipy_table import *\n",
"planets = [\n",
" ['Planet', 'Mass (kg)', 'Diameter (km)'],\n",
" ['Mercury', 3.3022E23, 4879], \n",
" ['Venus', 4.896E24, 12104],\n",
" ['Earth', 5.972E24, 12735],\n",
" ['Mars', 6.4191E23, 6772]];\n",
"make_table(planets)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\"><tr><td>Planet</td><td>Mass&nbsp(kg)</td><td>Diameter&nbsp(km)</td></tr><tr><td>Mercury</td><td>330219999999999996854272.0000</td><td>4879</td></tr><tr><td>Venus</td><td>4896000000000000201326592.0000</td><td>12104</td></tr><tr><td>Earth</td><td>5972000000000000327155712.0000</td><td>12735</td></tr><tr><td>Mars</td><td>641910000000000065536000.0000</td><td>6772</td></tr>"
],
"output_type": "pyout",
"prompt_number": 2,
"text": [
"<IPython.core.display.HTML at 0xa6b9aac>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``make_table()`` interface is interactive, so after calling ``make_table()`` we can call style formatting commands to modify the current table format. Here we'll apply the \"basic\" table theme.\n",
"\n",
"Note: Use \"basic_left\" for tables with row headers. Use \"basic_both\" for tables with row and coulmn headers."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"apply_theme('basic')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\"><tr><td style=\"background-color:LightGray;\"><b>Planet</b></td><td style=\"background-color:LightGray;\"><b>Mass&nbsp(kg)</b></td><td style=\"background-color:LightGray;\"><b>Diameter&nbsp(km)</b></td></tr><tr><td style=\"background-color:Ivory;\">Mercury</td><td style=\"background-color:Ivory;\">330219999999999996854272.0000</td><td style=\"background-color:Ivory;\">4879</td></tr><tr><td style=\"background-color:AliceBlue;\">Venus</td><td style=\"background-color:AliceBlue;\">4896000000000000201326592.0000</td><td style=\"background-color:AliceBlue;\">12104</td></tr><tr><td style=\"background-color:Ivory;\">Earth</td><td style=\"background-color:Ivory;\">5972000000000000327155712.0000</td><td style=\"background-color:Ivory;\">12735</td></tr><tr><td style=\"background-color:AliceBlue;\">Mars</td><td style=\"background-color:AliceBlue;\">641910000000000065536000.0000</td><td style=\"background-color:AliceBlue;\">6772</td></tr>"
],
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<IPython.core.display.HTML at 0xa68edec>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Mass values are being fully expanded. By default ipy_table formats floating point numbers using the Python formatting string ``\"%0.4f\"``. We can override that by setting the ``float_format`` parameter.\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_global_style(float_format='%0.3E')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\"><tr><td style=\"background-color:LightGray;\"><b>Planet</b></td><td style=\"background-color:LightGray;\"><b>Mass&nbsp(kg)</b></td><td style=\"background-color:LightGray;\"><b>Diameter&nbsp(km)</b></td></tr><tr><td style=\"background-color:Ivory;\">Mercury</td><td style=\"background-color:Ivory;\">3.302E+23</td><td style=\"background-color:Ivory;\">4879</td></tr><tr><td style=\"background-color:AliceBlue;\">Venus</td><td style=\"background-color:AliceBlue;\">4.896E+24</td><td style=\"background-color:AliceBlue;\">12104</td></tr><tr><td style=\"background-color:Ivory;\">Earth</td><td style=\"background-color:Ivory;\">5.972E+24</td><td style=\"background-color:Ivory;\">12735</td></tr><tr><td style=\"background-color:AliceBlue;\">Mars</td><td style=\"background-color:AliceBlue;\">6.419E+23</td><td style=\"background-color:AliceBlue;\">6772</td></tr>"
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<IPython.core.display.HTML at 0xa6b902c>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All cell formatting is dynamic. Custom formatting can be applied by calling ``set_<global, row, column, cell>_style()``."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_row_style(3,color='yellow')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\"><tr><td style=\"background-color:LightGray;\"><b>Planet</b></td><td style=\"background-color:LightGray;\"><b>Mass&nbsp(kg)</b></td><td style=\"background-color:LightGray;\"><b>Diameter&nbsp(km)</b></td></tr><tr><td style=\"background-color:Ivory;\">Mercury</td><td style=\"background-color:Ivory;\">3.302E+23</td><td style=\"background-color:Ivory;\">4879</td></tr><tr><td style=\"background-color:AliceBlue;\">Venus</td><td style=\"background-color:AliceBlue;\">4.896E+24</td><td style=\"background-color:AliceBlue;\">12104</td></tr><tr><td style=\"background-color:yellow;\">Earth</td><td style=\"background-color:yellow;\">5.972E+24</td><td style=\"background-color:yellow;\">12735</td></tr><tr><td style=\"background-color:AliceBlue;\">Mars</td><td style=\"background-color:AliceBlue;\">6.419E+23</td><td style=\"background-color:AliceBlue;\">6772</td></tr>"
],
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<IPython.core.display.HTML at 0xa6b904c>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For documentation on all ipy_table commands, see the ipy_table reference notebook (ipy_table-Reference.ipynb)"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment