<jupyter_start><jupyter_text>Birdsong REST APIThe Birdsong database is accessible via a REST API. Accessing it with any language should be fairly simple; for these examples, we'll use Python.All we need to get started is to import the _requests_ package and set up our base URL.<jupyter_code>import requests
URL = "http://127.0.0.1:5000/"<jupyter_output><empty_output><jupyter_text>The simplest requests just view the rows in the database tables. Let's look at _bird_ - the datbase's driving table:<jupyter_code># Find all birds
req = requests.get(URL + "view/bird")

# Results are returned as JSON. There are two blocks - "data" contains the returned data
req.json()["data"]
# "rest" contains statistics on the API call itself
req.json()["rest"]<jupyter_output><empty_output><jupyter_text>We can also filter query results to look at specific birds:<jupyter_code># Find all birds sired by a specific bird
req = requests.get(URL + "view/bird?sire=20171220_purple42white35")
req.json()<jupyter_output><empty_output><jupyter_text>That's a lot of data. What if we just wanted the nest and name for the birds?We can specify just the columns we want:<jupyter_code># Find specific columns for all birds sired by a specific bird
req = requests.get(URL + "view/bird?sire=20171220_purple42white35&_columns=nest,name")
req.json()<jupyter_output><empty_output><jupyter_text>If you ever want to know which columns you can use for filtering, use the _/columns/_ endpoint:<jupyter_code># Find columns in the bird_event table
req = requests.get(URL + "columns/bird")
req.json()
Not sure of the tables you can query?
# Find views/tables
req = requests.get(URL + "tables")
req.json()<jupyter_output><empty_output><jupyter_text>Let's try a different table:<jupyter_code># Find all events for a specific bird
req = requests.get(URL + "view/bird_event?name=20220405_purple12white23")
req.json()<jupyter_output><empty_output><jupyter_text>So far, everything has worked. What if there's an error, like a filter that specifies a non-existent bird?<jupyter_code># If there's an error, check rest->error
req = requests.get(URL + "view/bird_event?name=no_such_bird")
req.json()<jupyter_output><empty_output><jupyter_text>What about nests?<jupyter_code># Find nests with a specific bird as a sire
req = requests.get(URL + "view/nest?sire=20171220_purple42white35")
req.json()<jupyter_output><empty_output><jupyter_text>Wildcards are also supported. Let's go back to the bird table:<jupyter_code># Find all birds with a green leg band:
req = requests.get(URL + "view/bird?name=*green*")
req.json()<jupyter_output><empty_output><jupyter_text>"Not equals" (or "not like" in the case of wildscards) is also supported:<jupyter_code># Find the names of all birds without a green leg band:
req = requests.get(URL + "view/bird?name!=*green*&_columns=name")
req.json()<jupyter_output><empty_output><jupyter_text>= are also supported. Let's find every bird hatched before January 1st of 2020:<jupyter_code># Find the names of all birds without a green leg band hatched before 2020-01-01:
req = requests.get(URL + "view/bird?hatch_late<=2020-01-01&_columns=name,hatch_early,hatch_late")
req.json()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10602258469259725, 'fraction_numerical': 0.02478042659974906, 'mean_word_length': 6.610978520286396, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '496398', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1049, 'n_words': 331, 'path': 'api/jupyter/birdsong_rest_api.ipynb', 'repo_name': 'JaneliaSciComp/birdsong', 'size': '3188'}