Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hcferguson/6c0540df702aefb929ef84a55a621f42 to your computer and use it in GitHub Desktop.
Save hcferguson/6c0540df702aefb929ef84a55a621f42 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test of Python wrapper around Airtable API\n",
"\n",
" - API docs -- https://airtable.com/api (a bit weird; you need to set up a base to see these)\n",
" - python wrapper http://airtable-python-wrapper.readthedocs.io/en/master/\n",
"\n",
"The table for this test is at https://airtable.com/shr4TChPF38v1J2cA. \n",
"To run the test, you need to create an Airtable API key following the instructions at \n",
"Then put that in an environment variable named AIRTABLE_API_KEY before starting up the Jupyter notebook."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from airtable import Airtable\n",
"import os\n",
"from astropy.table import Table\n",
"import numpy as np\n",
"from random_groups import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read in the table of players from the Airtable site https://airtable.com/shr4TChPF38v1J2cA. The base_key here is provided when you ask for the API documentation on their site and select that table and then look down at the code uner Authentication."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"base_key = 'appeD7su0rJ1vWvib'\n",
"table_name = 'Players'\n",
"airtable = Airtable(base_key, table_name)\n",
"t = airtable.get_all()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next cell shows the structure of each record in the table."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'createdTime': '2018-05-25T19:51:33.000Z',\n",
" 'fields': {'Name': 'Dan', 'subset': 'monday'},\n",
" 'id': 'rec5C9QuibOu6Bm6y'}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Loop through the records and stuff them into lists."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"names = []\n",
"subset = []\n",
"for record in t:\n",
" names += [record['fields']['Name']]\n",
" subset += [record['fields']['subset']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an Astropy Table from the lists and inspect it"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<Table length=14>\n",
"<table id=\"table4475760880\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>name</th><th>subset</th></tr></thead>\n",
"<thead><tr><th>str5</th><th>str8</th></tr></thead>\n",
"<tr><td>Dan</td><td>monday</td></tr>\n",
"<tr><td>Jason</td><td>monday</td></tr>\n",
"<tr><td>Harry</td><td>monday</td></tr>\n",
"<tr><td>Ken</td><td>tuesday</td></tr>\n",
"<tr><td>Iva</td><td>tuesday</td></tr>\n",
"<tr><td>Vicky</td><td>monday</td></tr>\n",
"<tr><td>Susan</td><td>monday</td></tr>\n",
"<tr><td>Ilana</td><td>monday</td></tr>\n",
"<tr><td>Steve</td><td>thursday</td></tr>\n",
"<tr><td>Katie</td><td>tuesday</td></tr>\n",
"<tr><td>Arfon</td><td>tuesday</td></tr>\n",
"<tr><td>Josh</td><td>thursday</td></tr>\n",
"<tr><td>Bill</td><td>tuesday</td></tr>\n",
"<tr><td>Carl</td><td>monday</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=14>\n",
" name subset \n",
" str5 str8 \n",
"----- --------\n",
" Dan monday\n",
"Jason monday\n",
"Harry monday\n",
" Ken tuesday\n",
" Iva tuesday\n",
"Vicky monday\n",
"Susan monday\n",
"Ilana monday\n",
"Steve thursday\n",
"Katie tuesday\n",
"Arfon tuesday\n",
" Josh thursday\n",
" Bill tuesday\n",
" Carl monday"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = Table([names,np.array(subset)],names=['name','subset'])\n",
"t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now just play around with making random groupings."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"&lt;Table length=12&gt;\n",
"<table id=\"table4475763512\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>name</th><th>groupid</th></tr></thead>\n",
"<thead><tr><th>str5</th><th>int32</th></tr></thead>\n",
"<tr><td>Jason</td><td>1</td></tr>\n",
"<tr><td>Dan</td><td>1</td></tr>\n",
"<tr><td>Iva</td><td>1</td></tr>\n",
"<tr><td>Arfon</td><td>1</td></tr>\n",
"<tr><td>Bill</td><td>1</td></tr>\n",
"<tr><td>Carl</td><td>1</td></tr>\n",
"<tr><td>Ken</td><td>2</td></tr>\n",
"<tr><td>Harry</td><td>2</td></tr>\n",
"<tr><td>Ilana</td><td>2</td></tr>\n",
"<tr><td>Vicky</td><td>2</td></tr>\n",
"<tr><td>Steve</td><td>2</td></tr>\n",
"<tr><td>Josh</td><td>2</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=12>\n",
" name groupid\n",
" str5 int32 \n",
"----- -------\n",
"Jason 1\n",
" Dan 1\n",
" Iva 1\n",
"Arfon 1\n",
" Bill 1\n",
" Carl 1\n",
" Ken 2\n",
"Harry 2\n",
"Ilana 2\n",
"Vicky 2\n",
"Steve 2\n",
" Josh 2"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = random_group(t)\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"&lt;Table length=6&gt;\n",
"<table id=\"table4475763456\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>name</th><th>groupid</th></tr></thead>\n",
"<thead><tr><th>str5</th><th>int32</th></tr></thead>\n",
"<tr><td>Vicky</td><td>1</td></tr>\n",
"<tr><td>Harry</td><td>1</td></tr>\n",
"<tr><td>Dan</td><td>1</td></tr>\n",
"<tr><td>Jason</td><td>1</td></tr>\n",
"<tr><td>Carl</td><td>1</td></tr>\n",
"<tr><td>Susan</td><td>1</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=6>\n",
" name groupid\n",
" str5 int32 \n",
"----- -------\n",
"Vicky 1\n",
"Harry 1\n",
" Dan 1\n",
"Jason 1\n",
" Carl 1\n",
"Susan 1"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = random_group(t,subset=t['subset'] == 'monday')\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"&lt;Table length=6&gt;\n",
"<table id=\"table4475764632\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>name</th><th>groupid</th></tr></thead>\n",
"<thead><tr><th>str5</th><th>int32</th></tr></thead>\n",
"<tr><td>Susan</td><td>1</td></tr>\n",
"<tr><td>Dan</td><td>1</td></tr>\n",
"<tr><td>Ilana</td><td>1</td></tr>\n",
"<tr><td>Jason</td><td>1</td></tr>\n",
"<tr><td>Harry</td><td>1</td></tr>\n",
"<tr><td>Carl</td><td>1</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=6>\n",
" name groupid\n",
" str5 int32 \n",
"----- -------\n",
"Susan 1\n",
" Dan 1\n",
"Ilana 1\n",
"Jason 1\n",
"Harry 1\n",
" Carl 1"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = random_group(t,subset=t['subset'] == 'monday')\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"&lt;Table length=5&gt;\n",
"<table id=\"table4475761272\" class=\"table-striped table-bordered table-condensed\">\n",
"<thead><tr><th>name</th><th>groupid</th></tr></thead>\n",
"<thead><tr><th>str5</th><th>int32</th></tr></thead>\n",
"<tr><td>Ken</td><td>1</td></tr>\n",
"<tr><td>Arfon</td><td>1</td></tr>\n",
"<tr><td>Iva</td><td>1</td></tr>\n",
"<tr><td>Bill</td><td>1</td></tr>\n",
"<tr><td>Katie</td><td>1</td></tr>\n",
"</table>"
],
"text/plain": [
"<Table length=5>\n",
" name groupid\n",
" str5 int32 \n",
"----- -------\n",
" Ken 1\n",
"Arfon 1\n",
" Iva 1\n",
" Bill 1\n",
"Katie 1"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = random_group(t,subset=t['subset'] == 'tuesday')\n",
"g"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:astroconda35]",
"language": "python",
"name": "conda-env-astroconda35-py"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment