diff --git a/ipy_table-Introduction.ipynb b/ipy_table-Introduction.ipynb index 880824e..d66bdc2 100644 --- a/ipy_table-Introduction.ipynb +++ b/ipy_table-Introduction.ipynb @@ -8,11 +8,10 @@ { "cells": [ { - "cell_type": "heading", - "level": 1, + "cell_type": "markdown", "metadata": {}, "source": [ - "Introduction" + "# Introduction" ] }, { @@ -27,11 +26,17 @@ ] }, { - "cell_type": "heading", - "level": 1, + "cell_type": "markdown", "metadata": {}, "source": [ - "Example" + "# Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Array as input" ] }, { @@ -69,7 +74,7 @@ "output_type": "pyout", "prompt_number": 1, "text": [ - "" + "" ] } ], @@ -100,7 +105,7 @@ "output_type": "pyout", "prompt_number": 2, "text": [ - "" + "" ] } ], @@ -129,7 +134,7 @@ "output_type": "pyout", "prompt_number": 3, "text": [ - "" + "" ] } ], @@ -158,12 +163,94 @@ "output_type": "pyout", "prompt_number": 4, "text": [ - "" + "" ] } ], "prompt_number": 4 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dict as input" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Also we can use a dictrionary to create a teble. We need a list to know the order to print the columns." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "\n", + "data = {\n", + " 'Planet': ['Mercury', 'Venus', 'Earth', 'Mars'],\n", + " 'Mass (kg)': np.array([3.3022E23, 4.896E24, 5.972E24, 6.4191E23]),\n", + " 'Diameter (km)': np.array([4879, 12104, 12735, 6772])\n", + " }\n", + "headers = ['Planet', 'Mass (kg)', 'Diameter (km)']\n", + "\n", + "make_table(data, headers)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "
PlanetMass (kg)Diameter (km)
Mercury330219999999999996854272.00004879
Venus4896000000000000201326592.000012104
Earth5972000000000000327155712.000012735
Mars641910000000000065536000.00006772
" + ], + "output_type": "pyout", + "prompt_number": 5, + "text": [ + "" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Also we can change the order of the columns:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "headers2 = headers[::-1]\n", + "make_table(data, headers2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "
Diameter (km)Mass (kg)Planet
4879330219999999999996854272.0000Mercury
121044896000000000000201326592.0000Venus
127355972000000000000327155712.0000Earth
6772641910000000000065536000.0000Mars
" + ], + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This can be useful when we have data stored in a dict." + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/ipy_table.py b/ipy_table.py index 259e6bb..8b40c27 100755 --- a/ipy_table.py +++ b/ipy_table.py @@ -56,6 +56,9 @@ """ import copy +import sys + + __version__ = 1.12 @@ -77,12 +80,34 @@ class IpyTable(object): # External methods #--------------------------------- - def __init__(self, array): + def __init__(self, input_data, order): + """Receive Python dictionary as input. Sort the dictionary + as the 'order' list. The dictionary can have numpy arrays + Also can receive a list o lists + """ + + + if type(input_data) is dict and order: + # Convert dict to a list o lists + values = [] + for key in order: + values.append(_convert_to_list(input_data[key])) + values = map(list, zip(*values)) + array = [order] + values + elif type(input_data) is list: + array = input_data + else: + sys.exit('Input data must be: \n -List of lists \n -Dictionary with a list of the order of keys to use.') + + + + self.array = array - self._num_rows = len(array) self._num_columns = len(array[0]) - + + + # Check that array is well formed for row in array: if len(row) != self._num_columns: @@ -402,11 +427,11 @@ def tabulate(data_list, columns, interactive=True): return get_interactive_return_value() -def make_table(array, interactive=True): +def make_table(array,order= None , interactive=True): """Create a table in interactive mode.""" global _TABLE global _INTERACTIVE - _TABLE = IpyTable(array) + _TABLE = IpyTable(array, order) _INTERACTIVE = interactive return get_interactive_return_value()