Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 97 additions & 10 deletions ipy_table-Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"cell_type": "markdown",
"metadata": {},
"source": [
"Introduction"
"# Introduction"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change html headings to markdown syntax.

]
},
{
Expand All @@ -27,11 +26,17 @@
]
},
{
"cell_type": "heading",
"level": 1,
"cell_type": "markdown",
"metadata": {},
"source": [
"Example"
"# Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array as input"
]
},
{
Expand Down Expand Up @@ -69,7 +74,7 @@
"output_type": "pyout",
"prompt_number": 1,
"text": [
"<ipy_table.IpyTable at 0xa11e1ac>"
"<ipy_table.IpyTable at 0x10ae76e90>"
]
}
],
Expand Down Expand Up @@ -100,7 +105,7 @@
"output_type": "pyout",
"prompt_number": 2,
"text": [
"<ipy_table.IpyTable at 0xa11e1ac>"
"<ipy_table.IpyTable at 0x10ae76e90>"
]
}
],
Expand Down Expand Up @@ -129,7 +134,7 @@
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<ipy_table.IpyTable at 0xa11e1ac>"
"<ipy_table.IpyTable at 0x10ae76e90>"
]
}
],
Expand Down Expand Up @@ -158,12 +163,94 @@
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<ipy_table.IpyTable at 0xa11e1ac>"
"<ipy_table.IpyTable at 0x10ae76e90>"
]
}
],
"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": [
"<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></table>"
],
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<ipy_table.IpyTable at 0x10ae7e410>"
]
}
],
"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": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\"><tr><td>Diameter&nbsp(km)</td><td>Mass&nbsp(kg)</td><td>Planet</td></tr><tr><td>4879</td><td>330219999999999996854272.0000</td><td>Mercury</td></tr><tr><td>12104</td><td>4896000000000000201326592.0000</td><td>Venus</td></tr><tr><td>12735</td><td>5972000000000000327155712.0000</td><td>Earth</td></tr><tr><td>6772</td><td>641910000000000065536000.0000</td><td>Mars</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 7,
"text": [
"<ipy_table.IpyTable at 0x10ae765d0>"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This can be useful when we have data stored in a dict."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
35 changes: 30 additions & 5 deletions ipy_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"""

import copy
import sys




__version__ = 1.12
Expand All @@ -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:
Expand Down Expand Up @@ -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()

Expand Down