{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from tabipy import Table, TableRow, TableCell, TableHeaderRow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a package to simply create tables to be displayed in IPython. The tables are rendered as both HTML and LaTeX, so they work both in the browser and if you convert the notebook to LaTeX.\n", "\n", "The simplest case is a plain grid:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
418
973
526
" ], "text/latex": [ "\\begin{tabular}{*{3}{l}}\n", "4 & 1 & 8\\\\\n", "\n", "9 & 7 & 3\\\\\n", "\n", "5 & 2 & 6\\\\\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Table((4, 1, 8),\n", " (9, 7, 3),\n", " (5, 2, 6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add a header row like this:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
abc
123
246
" ], "text/latex": [ "\\begin{tabular}{*{3}{l}}\n", "\\hline\n", "\\bf a & \\bf b & \\bf c\\\\\n", "\\hline\n", "\n", "1 & 2 & 3\\\\\n", "\n", "2 & 4 & 6\\\\\n", "\\hline\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Table(TableHeaderRow('a','b','c'),\n", " (1, 2, 3),\n", " (2, 4, 6),\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Table` also accepts `dict`s (or any mapping) with keys as column headers and values as column contents. The order of columns is undefined unless the mapping is an `OrderedDict`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
abc
123
246
" ], "text/latex": [ "\\begin{tabular}{*{3}{l}}\n", "\\hline\n", "\\bf a & \\bf b & \\bf c\\\\\n", "\\hline\n", "\n", "1 & 2 & 3\\\\\n", "\n", "2 & 4 & 6\\\\\n", "\\hline\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Table({'a': (1, 2),\n", " 'b': (2, 4),\n", " 'c': (3, 6)})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number of column values (rows) need not be equal:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
abc
123
26
" ], "text/latex": [ "\\begin{tabular}{*{3}{l}}\n", "\\hline\n", "\\bf a & \\bf b & \\bf c\\\\\n", "\\hline\n", "\n", "1 & 2 & 3\\\\\n", "\n", "2 & & 6\\\\\n", "\\hline\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Table({'a': (1, 2),\n", " 'b': (2,),\n", " 'c': (3, 6)})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can build a table incrementally using `Table.append_row()`. If you need it, rows also have an `append_cell()` method." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
numbersquarecube
111
248
3927
41664
525125
636216
749343
864512
981729
101001000
" ], "text/latex": [ "\\begin{tabular}{*{3}{l}}\n", "\\hline\n", "\\bf number & \\bf square & \\bf cube\\\\\n", "\\hline\n", "\n", "1 & 1 & 1\\\\\n", "\n", "2 & 4 & 8\\\\\n", "\n", "3 & 9 & 27\\\\\n", "\n", "4 & 16 & 64\\\\\n", "\n", "5 & 25 & 125\\\\\n", "\n", "6 & 36 & 216\\\\\n", "\n", "7 & 49 & 343\\\\\n", "\n", "8 & 64 & 512\\\\\n", "\n", "9 & 81 & 729\\\\\n", "\n", "10 & 100 & 1000\\\\\n", "\\hline\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Computing values\n", "t = Table(TableHeaderRow('number', 'square', 'cube'))\n", "for x in range(1, 11):\n", " t.append_row((x, x**2, x**3))\n", "t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can style cells with the `bg_colour` and `text_colour` parameters. This only works in HTML for the moment; if you convert the notebook to LaTeX, the colours will be ignored." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
divisionsresult
055
118.333333333333332
26.111111111111111
32.0370370370370368
40.6790123456790123
50.22633744855967075
60.07544581618655692
" ], "text/latex": [ "\\begin{tabular}{*{2}{l}}\n", "\\hline\n", "\\bf divisions & \\bf result\\\\\n", "\\hline\n", "\n", "0 & 55\\\\\n", "\n", "1 & 18.333333333333332\\\\\n", "\n", "2 & 6.111111111111111\\\\\n", "\n", "3 & 2.0370370370370368\\\\\n", "\n", "4 & 0.6790123456790123\\\\\n", "\n", "5 & 0.22633744855967075\\\\\n", "\n", "6 & 0.07544581618655692\\\\\n", "\\hline\n", "\\end{tabular}" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Styling determined by code\n", "t = Table(TableHeaderRow('divisions', 'result'))\n", "num = 55\n", "for x in range(7):\n", " if num < 1:\n", " resultcell = TableCell(num, bg_colour='DarkBlue', text_colour='white')\n", " else:\n", " resultcell = TableCell(num)\n", " t.append_row((x, resultcell))\n", " num /= 3\n", "t" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }