-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path02_project_data_files.py
81 lines (50 loc) · 2.27 KB
/
02_project_data_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""02 Project data files.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1NVA-XIxc18_0IDBJquXEIUMkTfse-iZn
# 2 - Accessing Related Items through Relationships
---
"""
import requests
import json
import string
# Importing the libraries we need to format the data in a more readable way.
import pandas as pd
from pandas.io.json import json_normalize
base_url = 'https://www.fairdomhub.org'
def json_for_resource(type, id):
headers = {"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"}
r = requests.get(base_url + "/" + type + "/" + str(id), headers=headers)
r.raise_for_status()
return r.json()
"""Fetches the JSON for Project 60
Shows just the relationships block, rather than the full JSON, as this is what we are interested in here.
Relationships are grouped by type. Each relationship has a '*data*' block, which provides an array of relationship resource.
Each resource is described with an '*id*' and '*type*'.
SEEK supports Restful Routes, each resource can be identified by:
https://<host>/<resource-type>/<id>
so, *{'id': '18', 'type': 'institutions'}* would be https://fairdomhub.org/institutions/18
**Note** that only visible resources are listed, any related items that are not visible due to sharing permissions, are omitted. (more in accessing as an authorized user later).
"""
project_id = 60
result = json_for_resource('projects',project_id)
print("Project: " + result['data']['attributes']['title'] + "\n")
result['data']['relationships']
"""Here we iterate over each related data_file, and GET the resource JSON, and display the resulting id and title in a table"""
files = []
type = 'data_files'
for item in result['data']['relationships'][type]['data']:
j = json_for_resource(item['type'],item['id'])
files.append({
'id':j['data']['id'],
'title':j['data']['attributes']['title'],
})
print(str(len(files)) + " found")
json_normalize(files)
"""# Exercise 2
* Run the notebook and familiarise yourself with each step.
* Update to list the assays related to the project https://fairdomhub.org/projects/19
* Update to list the models related to Natalie: https://fairdomhub.org/people/433
"""