#!/usr/bin/env python # coding: utf-8 # In[1]: try: import IPython except: get_ipython().run_line_magic('pip', 'install IPython') import IPython from IPython.display import display, IFrame, HTML, Javascript from IPython.core.display import display, HTML HTML("""""") # # Transforming Collections Data to Linked Art # # Harvard Art Museum # ## Input Data # The notebook uses data from the Harvard Art Museum, using an API # In[2]: import requests try: import json except: get_ipython().system('pip install json') import json apidocs = "https://github.com/harvardartmuseums/api-docs" keyfile = "data/ruskin/input/key.txt" key = open(keyfile, mode='r', encoding='utf-8-sig').read() uri = "https://api.harvardartmuseums.org/object?person=28419&apikey=" + key for page in range(1, 11): response = requests.get(uri + "&page=" + str(page)) json_data = response.json() text_file = open( "./data/harvard/input/" + str(page) + ".json", "wt") n = text_file.write(json.dumps(json_data, indent=2)) text_file.close() # In[3]: try: import pandas as pd except: get_ipython().system('pip install pandas') import pandas as pd baseURI = "https://www.harvardartmuseums.org/collections/object/" outputdir = "data/harvard/output" mapp = { "id":"id", "accession_number":"objectnumber", "accession_date": "accessionyear", "classification" : "", "title": "title", "alt_title": "", "notes": "commentary", "date_created":"dated", "date_created_earliest": "", "date_created_latest": "", "created_period":"period", "created_dynasty":"century", "created_inscriptions":"", "created_notes": "", "creator":"people", "physical_medium": "medium", "physical_style": "", "physical_technique": "technique", "physical_description": "description", "physical_dimensions": "dimensions", "created_provenance": "provenance" , "credit_line": "creditline", "collection" : "division", "classification": "classification", "current_status" : "", "current_owner": "Harvard Museum of Art", "image_url":"primaryimageurl", "homepage" : "url" } # display transposed dataframe of data mapping display(pd.DataFrame(mapp, index=[0]).T) # In[4]: try: import cromulent except: get_ipython().system('pip install cromulent') import cromulent from cromulent.model import factory from lib import linkedart as la def createObjPropHarvard(obj,mapp): objProp = {} csv_keys = list(obj.keys()) for key in csv_keys: for prop in mapp: if key == mapp[prop]: if prop == "creator": objProp["creator"] = "" for person in obj[key]: if "personid" in person and person["personid"] == 28419 and person["role"] == "Artist": objProp[prop] = [{ "id": str(person["personid"]), "name": person["displayname"], }] else: objProp[prop] = obj[key] objProp["current_owner"] = {"name":"Harvard Art Museum", "location":"Cambridge, MA", "type": "http://vocab.getty.edu/aat/300312281" , "type_label": ""} return objProp # list to hold file names for use with jsonld visualisation dropdown selectOptions = [] selectOptions = [('Please select an artwork', '')] for x in range(1,11): with open("./data/harvard/input/" + str(x) + ".json") as json_file: data = json.load(json_file) for obj in data["records"]: objProp=createObjPropHarvard(obj,mapp) if objProp["creator"] != "": id = str(objProp["id"]) filename = id + ".json" object_uri = baseURI + id # create obj description objLA = la.createObjDesc(objProp,la.objTypes,object_uri) selectOptions.append( ( objProp["title"] + " (" + filename + ")" , filename)) # write to file text_file = open(outputdir + "/" + filename, "wt") n = text_file.write(factory.toString(objLA, compact=False)) text_file.close() # In[5]: try: import ipywidgets except: get_ipython().run_line_magic('pip', 'install ipywidgets') import ipywidgets from ipywidgets import Layout, FileUpload from IPython.display import display, IFrame, HTML, Image from IPython.core.display import Javascript import os try: import json except: get_ipython().run_line_magic('pip', 'install json') import json def dropdown_eventhandler(change): with open('./src/js/visld.js', 'r') as _jscript: code = _jscript.read() + "var file = '" + outputdir + "/" + change.new + "';var selector = '#vis';visjsonld(file, selector); " display(Javascript(code)) with open( outputdir + "/" + change.new) as json_file: artwork = json.load(json_file) if ("representation" in artwork): image = artwork["representation"][0]["id"] display(Javascript("document.getElementById('artwork').src = '" + image + "';")) else: display(Javascript("document.getElementById('artwork').src = '';")) selectObject = ipywidgets.Dropdown(options=selectOptions) selectObject.observe(dropdown_eventhandler, names='value') display(selectObject) #
# #
# In[ ]: