Skip to content

Instantly share code, notes, and snippets.

@danlamanna
Created August 15, 2016 19:08
Show Gist options
  • Save danlamanna/04475402dbd3827e28ab6adce11bcb63 to your computer and use it in GitHub Desktop.
Save danlamanna/04475402dbd3827e28ab6adce11bcb63 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"API_URL = 'http://localhost:8103/api/v1'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Authentication\n",
"Authenticate with Girder and setup headers to be used in future calls to Girder to identify yourself"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = requests.get(API_URL + '/user/authentication', auth=('girder', 'girder')).json()\n",
"token = r['authToken']['token']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"headers = {'Girder-Token': token}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Listing collections\n",
"Passing the headers allows you to see collections you have permission to view, omitting them will show you only public collections."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"size\": 21282, \n",
" \"updated\": \"2016-08-05T17:32:07.609000+00:00\", \n",
" \"description\": \"\", \n",
" \"created\": \"2016-08-05T16:27:31.857000+00:00\", \n",
" \"public\": false, \n",
" \"_id\": \"57a4be73d2a7331829d87979\", \n",
" \"_modelType\": \"collection\", \n",
" \"_accessLevel\": 2, \n",
" \"name\": \"Main\"\n",
"}\n"
]
}
],
"source": [
"for collection in requests.get(API_URL + '/collection', headers=headers).json():\n",
" print json.dumps(collection, indent=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Listing folders in a collection\n",
"Using the collection `_id` from the above list"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"size\": 0, \n",
" \"updated\": \"2016-08-05T16:27:31.900000+00:00\", \n",
" \"description\": \"\", \n",
" \"created\": \"2016-08-05T16:27:31.900000+00:00\", \n",
" \"baseParentType\": \"collection\", \n",
" \"public\": false, \n",
" \"baseParentId\": \"57a4be73d2a7331829d87979\", \n",
" \"creatorId\": \"57a4aa23d2a7335b89a6882d\", \n",
" \"parentId\": \"57a4be73d2a7331829d87979\", \n",
" \"parentCollection\": \"collection\", \n",
" \"_id\": \"57a4be73d2a7331829d8797c\", \n",
" \"_modelType\": \"folder\", \n",
" \"_accessLevel\": 2, \n",
" \"name\": \"Analyses\"\n",
"}\n",
"{\n",
" \"size\": 21282, \n",
" \"updated\": \"2016-08-05T16:27:31.899000+00:00\", \n",
" \"description\": \"\", \n",
" \"created\": \"2016-08-05T16:27:31.899000+00:00\", \n",
" \"baseParentType\": \"collection\", \n",
" \"public\": false, \n",
" \"baseParentId\": \"57a4be73d2a7331829d87979\", \n",
" \"creatorId\": \"57a4aa23d2a7335b89a6882d\", \n",
" \"parentId\": \"57a4be73d2a7331829d87979\", \n",
" \"parentCollection\": \"collection\", \n",
" \"_id\": \"57a4be73d2a7331829d8797a\", \n",
" \"_modelType\": \"folder\", \n",
" \"_accessLevel\": 2, \n",
" \"name\": \"Data\"\n",
"}\n",
"{\n",
" \"size\": 0, \n",
" \"updated\": \"2016-08-05T16:27:31.898000+00:00\", \n",
" \"description\": \"\", \n",
" \"created\": \"2016-08-05T16:27:31.898000+00:00\", \n",
" \"baseParentType\": \"collection\", \n",
" \"public\": false, \n",
" \"baseParentId\": \"57a4be73d2a7331829d87979\", \n",
" \"creatorId\": \"57a4aa23d2a7335b89a6882d\", \n",
" \"parentId\": \"57a4be73d2a7331829d87979\", \n",
" \"parentCollection\": \"collection\", \n",
" \"_id\": \"57a4be73d2a7331829d8797b\", \n",
" \"_modelType\": \"folder\", \n",
" \"_accessLevel\": 2, \n",
" \"name\": \"Visualizations\"\n",
"}\n"
]
}
],
"source": [
"collectionFolders = requests.get(API_URL + '/folder', params={\n",
" 'parentType': 'collection',\n",
" 'parentId': '57a4be73d2a7331829d87979'}, headers=headers).json()\n",
"\n",
"for collectionFolder in collectionFolders:\n",
" print json.dumps(collectionFolder, indent=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Listing analyses\n",
"Using the `_id` of the Analyses folder from above"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"size\": 0, \n",
" \"updated\": \"2016-08-15T18:35:25.820000+00:00\", \n",
" \"folderId\": \"57a4be73d2a7331829d8797c\", \n",
" \"description\": \"\", \n",
" \"created\": \"2016-08-15T18:35:25.797000+00:00\", \n",
" \"baseParentType\": \"collection\", \n",
" \"meta\": {\n",
" \"analysis\": {\n",
" \"inputs\": [\n",
" {\n",
" \"type\": \"table\", \n",
" \"id\": \"table\", \n",
" \"name\": \"table\", \n",
" \"format\": \"rows\"\n",
" }\n",
" ], \n",
" \"description\": \"\", \n",
" \"script\": \"out_table = table\\n\", \n",
" \"outputs\": [\n",
" {\n",
" \"type\": \"table\", \n",
" \"id\": \"out_table\", \n",
" \"name\": \"out_table\", \n",
" \"format\": \"rows\"\n",
" }\n",
" ], \n",
" \"mode\": \"python\", \n",
" \"name\": \"tablePassThrough\"\n",
" }\n",
" }, \n",
" \"baseParentId\": \"57a4be73d2a7331829d87979\", \n",
" \"creatorId\": \"57a4aa23d2a7335b89a6882d\", \n",
" \"_id\": \"57b20b6dd2a733485e91de8a\", \n",
" \"_modelType\": \"item\", \n",
" \"name\": \"tablePassThrough\"\n",
"}\n"
]
}
],
"source": [
"for analysis in requests.get(API_URL + '/item', params={'folderId': '57a4be73d2a7331829d8797c'}, headers=headers).json():\n",
" print json.dumps(analysis, indent=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Running an analysis\n",
"Using the `_id` of the analysis in the above output"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Prepare the input with data, and the output information\n",
"task = {\n",
" \"inputs\": {\n",
" \"table\": {\n",
" \"type\": \"table\",\n",
" \"format\": \"rows\",\n",
" \"data\": {\n",
" \"fields\": [\"foo\"],\n",
" \"rows\": [\"bar\", \"baz\"]\n",
" }\n",
" }\n",
" },\n",
" \"outputs\": {\n",
" \"out_table\": {\n",
" \"type\": \"table\",\n",
" \"format\": \"rows\"\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# The analysis starts a job and returns its id\n",
"job = requests.post(API_URL + '/item/' + '57b20b6dd2a733485e91de8a' + '/flow', \n",
" data=json.dumps(task), \n",
" headers=headers).json()\n",
"jobId = job['_id']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"result\": {\n",
" \"out_table\": {\n",
" \"type\": \"table\", \n",
" \"data\": {\n",
" \"fields\": [\n",
" \"foo\"\n",
" ], \n",
" \"rows\": [\n",
" \"bar\", \n",
" \"baz\"\n",
" ]\n",
" }, \n",
" \"format\": \"rows\"\n",
" }\n",
" }\n",
"}\n"
]
}
],
"source": [
"# Job results can be viewed with a combination of the analysis itemId and the jobId\n",
"result = requests.get(API_URL + '/item/' + '57b20b6dd2a733485e91de8a' + '/flow/' + jobId + '/result', headers=headers)\n",
"print json.dumps(result.json(), indent=2)"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment