Skip to content

Instantly share code, notes, and snippets.

@erichannell
Created October 13, 2015 18:05
Show Gist options
  • Save erichannell/f4c35e7f2e76b26646b4 to your computer and use it in GitHub Desktop.
Save erichannell/f4c35e7f2e76b26646b4 to your computer and use it in GitHub Desktop.
Getting started with the Tableau Data Extract API
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Importing the **Extract API**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import dataextract as tde"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##I'll generate some data so let's import **random** and **string** too"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from random import randrange, random, choice\n",
"import string"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##What is the simplest way to create an extract programatically?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"created Extract\n"
]
}
],
"source": [
"with tde.Extract('TC15_Extract.tde') as extract:\n",
" \n",
" # create a data model\n",
" tableDef = tde.TableDefinition()\n",
" tableDef.addColumn('rowID', tde.Type.INTEGER)\n",
" tableDef.addColumn('tag', tde.Type.CHAR_STRING)\n",
" tableDef.addColumn('value', tde.Type.DOUBLE)\n",
" \n",
" # give it a name, as long as you call it 'Extract'\n",
" table = extract.addTable('Extract', tableDef)\n",
"\n",
" # define a row to insert\n",
" new_row = tde.Row(tableDef)\n",
"\n",
" # populate each row with data\n",
" for i in xrange(10):\n",
" new_row.setInteger(0, i)\n",
" new_row.setCharString(1, choice(string.ascii_lowercase))\n",
" new_row.setDouble(2, random())\n",
" \n",
" # write each row to the .tde\n",
" table.insert(new_row)\n",
"\n",
"print \"created Extract\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Now, let's see what this thing looks like"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# open the working directory\n",
"os.startfile(os.getcwd())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Nice, let's publish this to Tableau Server."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# cd to the bin directory\n",
"# use tabcmd to log in\n",
"# publish to server\n",
"\n",
"extract_folder = \"C:\\Users\\ehannell\\Desktop\\TC15\\Extract_API\"\n",
"\n",
"# cd to the bin folder...\n",
"os.chdir(\"C:\\\\Program Files (x86)\\Tableau\\Tableau Server\\9.1\\\\bin\") #\\\\ escapes spaces in file path\n",
"\n",
"os.system(\"tabcmd login -s http://localhost:8000 -u admin -p admin\")\n",
"\n",
"os.system('tabcmd publish \"' + extract_folder + '\\TC15_Extract.tde' + '\" -n \"Test Extract\"')\n",
"\n",
"os.chdir(extract_folder)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###[let's take a look](http://localhost:8000/#/datasources)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"##But, what if we want to append?"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Appending to an existing extract\n"
]
}
],
"source": [
"current_file = \"TC15_Extract.tde\"\n",
"\n",
"with tde.Extract(current_file) as extract:\n",
" \n",
" # working with the same data model\n",
" tableDef = tde.TableDefinition()\n",
" tableDef.addColumn('rowID', tde.Type.INTEGER)\n",
" tableDef.addColumn('tag', tde.Type.CHAR_STRING)\n",
" tableDef.addColumn('value', tde.Type.DOUBLE)\n",
" \n",
" # declare an empty table\n",
" table = None\n",
"\n",
" if not extract.hasTable('Extract'):\n",
" # Table does not exist, so create it.\n",
" print \"Creating a new extract\"\n",
" table = extract.addTable('Extract', tableDef)\n",
" else:\n",
" # Table exists, so append the new data.\n",
" print \"Appending to an existing extract\"\n",
" table = extract.openTable('Extract')\n",
"\n",
" new_row = tde.Row(tableDef)\n",
"\n",
" for i in range(10):\n",
" new_row.setInteger(0, i)\n",
" new_row.setCharString(1, choice(string.ascii_lowercase))\n",
" new_row.setDouble(2, random())\n",
" table.insert(new_row)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Nice, let's send that to server too."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"extract_folder = \"C:\\Users\\ehannell\\Desktop\\TC15\\Extract_API\"\n",
"\n",
"os.chdir(\"C:\\\\Program Files (x86)\\Tableau\\Tableau Server\\9.1\\\\bin\")\n",
"os.system(\"tabcmd login -s http://localhost:8000 -u admin -p admin\")\n",
"\n",
"# overwrite old extract w/ -replace\n",
"os.system('tabcmd publish \"' + extract_folder + '\\TC15_Extract.tde' + '\" -name \"Test Extract\" -replace') \n",
"\n",
"os.chdir(extract_folder)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###[let's take a look](http://localhost:8000/#/datasources)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Now that we can create, append and publish, what's next?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment