''' Helper functions and ipython magic for the SPARQL exercises. The tests in the notebooks rely on the `LAST_QUERY` variable, which is updated by the `%%sparql` magic after every query. This variable contains the full query used (`LAST_QUERY["query"]`), the endpoint it was sent to (`LAST_QUERY["endpoint"]`), and a dictionary with the response of the endpoint (`LAST_QUERY["results"]`). For convenience, the results are also given as tuples (`LAST_QUERY["tuples"]`), and as a dictionary of of `{column:[values]}` (`LAST_QUERY["columns"]`). ''' from IPython.core.magic import (register_line_magic, register_cell_magic, register_line_cell_magic) from IPython.display import HTML, display, Image, display_javascript from urllib.request import Request, urlopen from urllib.parse import quote_plus, urlencode from urllib.error import HTTPError import ssl import json import sys js = "IPython.CodeCell.options_default.highlight_modes['magic_sparql'] = {'reg':[/^%%sparql/]};" display_javascript(js, raw=True) def send_query(query, endpoint): FORMATS = ",".join(["application/sparql-results+json", "text/javascript", "application/json"]) data = {'query': query} # b = quote_plus(query) r = Request(endpoint, data=urlencode(data).encode('utf-8'), headers={'content-type': 'application/x-www-form-urlencoded', 'accept': FORMATS}, method='POST') context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE res = urlopen(r, context=context) data = res.read().decode('utf-8') if res.getcode() == 200: try: return json.loads(data) except Exception: print('Got: ', data, file=sys.stderr) raise raise Exception('Error getting results: {}'.format(data)) def tabulate(tuples, header=None): if not header: header, tuples = tuples[0], tuples[1:] header = '