-
-
Save postfalk/02ae0542b9dbe7467e25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:7084d0e0a376eaab7de21c3c9d81cb914ca86d57d916024c194b50ba46f39104" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Basic Example how to interact with the Berkeley Ecoinformatics Engine using Python" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In this example I will demonstrate how to request json data from the Ecoengine." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import requests" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The [requests](http://docs.python-requests.org/en/latest/) library makes interacting with an API quite easy. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"headers = {'Accept': 'application/json'}\n", | |
"r = requests.get('https://ecoengine.berkeley.edu/api/observations/', headers=headers)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"r.status_code" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"200" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Status code 200 means that the API returned valid data." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"if r.status_code == 200:\n", | |
" print r.json()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{u'count': 4674293, u'previous': None, u'results': [{u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1362/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1362', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1360/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1360', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1361/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1361', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1359/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1359', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1358/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1358', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1357/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1357', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1356/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1356', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1355/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1355', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1354/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1354', u'observation_type': u'checklist'}, {u'state_province': u'California', u'begin_date': u'2007-01-01T00:00:00', u'end_date': u'2007-12-31T00:00:00', u'source': u'https://ecoengine.berkeley.edu/api/sources/18/', u'url': u'https://ecoengine.berkeley.edu/api/observations/bigcb%3Aspecieslist%3A6%3A1353/', u'country': u'USA', u'geojson': None, u'scientific_name': u'Milnesium tardigradum', u'remote_resource': u'http://bigcb.berkeley.edu/cgi/bigcb_speclist_show?ItemID=1353', u'observation_type': u'checklist'}], u'next': u'https://ecoengine.berkeley.edu/api/observations/?page=2'}\n" | |
] | |
} | |
], | |
"prompt_number": 34 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment