1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "source" : [
6+ " ## This example show how to query sparql endpoint using Python.\n " ,
7+ " ### To run this, press \" Run all\" button, or Ctrl+Shift+Alt+Enter"
8+ ],
9+ "metadata" : {
10+ "collapsed" : false ,
11+ "pycharm" : {
12+ "name" : " #%% md\n "
13+ }
14+ }
15+ },
16+ {
17+ "cell_type" : " code" ,
18+ "execution_count" : 35 ,
19+ "outputs" : [],
20+ "source" : [
21+ " # Uncomment last line of this cell (by deleting #) to install necessary dependencies.\n " ,
22+ " \n " ,
23+ " #!pip install sparqlwrapper pandas"
24+ ],
25+ "metadata" : {
26+ "collapsed" : false ,
27+ "pycharm" : {
28+ "name" : " #%%\n "
29+ }
30+ }
31+ },
32+ {
33+ "cell_type" : " code" ,
34+ "execution_count" : 36 ,
35+ "outputs" : [],
36+ "source" : [
37+ " # Import dependencies\n " ,
38+ " from io import StringIO\n " ,
39+ " from SPARQLWrapper import SPARQLWrapper, CSV\n " ,
40+ " from pandas import read_csv\n " ,
41+ " \n " ,
42+ " # Prepare variable with sparql endpoint\n " ,
43+ " QUERY_ENDPOINT: str = 'https://publications.europa.eu/webapi/rdf/sparql'\n " ,
44+ " \n " ,
45+ " # Example of sparql query\n " ,
46+ " SPARQL_QUERY: str = \"\"\"\n " ,
47+ " prefix cdm: <http://publications.europa.eu/ontology/cdm#>\n " ,
48+ " select distinct ?Countries\n " ,
49+ " where {\n " ,
50+ " ?Countries a cdm:country\n " ,
51+ " }\n " ,
52+ " \"\"\" "
53+ ],
54+ "metadata" : {
55+ "collapsed" : false ,
56+ "pycharm" : {
57+ "name" : " #%%\n "
58+ }
59+ }
60+ },
61+ {
62+ "cell_type" : " code" ,
63+ "execution_count" : 37 ,
64+ "outputs" : [],
65+ "source" : [
66+ " # Prepare connection to sparql endpoint\n " ,
67+ " sparql_endpoint = SPARQLWrapper(QUERY_ENDPOINT)\n " ,
68+ " sparql_endpoint.setQuery(SPARQL_QUERY)\n " ,
69+ " sparql_endpoint.setReturnFormat(CSV)"
70+ ],
71+ "metadata" : {
72+ "collapsed" : false ,
73+ "pycharm" : {
74+ "name" : " #%%\n "
75+ }
76+ }
77+ },
78+ {
79+ "cell_type" : " code" ,
80+ "execution_count" : 38 ,
81+ "outputs" : [],
82+ "source" : [
83+ " # Request for query\n " ,
84+ " endpoint_result = sparql_endpoint.query().convert()\n " ,
85+ " # Convert endpoint result to table (dataframe)\n " ,
86+ " result_data = StringIO(str(endpoint_result, 'utf-8'))\n " ,
87+ " result_dataframe = read_csv(result_data)"
88+ ],
89+ "metadata" : {
90+ "collapsed" : false ,
91+ "pycharm" : {
92+ "name" : " #%%\n "
93+ }
94+ }
95+ },
96+ {
97+ "cell_type" : " code" ,
98+ "execution_count" : 42 ,
99+ "outputs" : [
100+ {
101+ "data" : {
102+ "text/plain" : " Countries\n 0 http://publications.europa.eu/resource/authori...\n 1 http://publications.europa.eu/resource/authori...\n 2 http://publications.europa.eu/resource/authori...\n 3 http://publications.europa.eu/resource/authori...\n 4 http://publications.europa.eu/resource/authori...\n .. ...\n 215 http://publications.europa.eu/resource/authori...\n 216 http://publications.europa.eu/resource/authori...\n 217 http://publications.europa.eu/resource/authori...\n 218 http://publications.europa.eu/resource/authori...\n 219 http://publications.europa.eu/resource/authori...\n\n [220 rows x 1 columns]" ,
103+ "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Countries</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>1</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>2</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>3</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>4</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n </tr>\n <tr>\n <th>215</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>216</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>217</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>218</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n <tr>\n <th>219</th>\n <td>http://publications.europa.eu/resource/authori...</td>\n </tr>\n </tbody>\n</table>\n<p>220 rows × 1 columns</p>\n</div>"
104+ },
105+ "execution_count" : 42 ,
106+ "metadata" : {},
107+ "output_type" : " execute_result"
108+ }
109+ ],
110+ "source" : [
111+ " # Print result\n " ,
112+ " result_dataframe"
113+ ],
114+ "metadata" : {
115+ "collapsed" : false ,
116+ "pycharm" : {
117+ "name" : " #%%\n "
118+ }
119+ }
120+ }
121+ ],
122+ "metadata" : {
123+ "kernelspec" : {
124+ "display_name" : " Python 3" ,
125+ "language" : " python" ,
126+ "name" : " python3"
127+ },
128+ "language_info" : {
129+ "codemirror_mode" : {
130+ "name" : " ipython" ,
131+ "version" : 2
132+ },
133+ "file_extension" : " .py" ,
134+ "mimetype" : " text/x-python" ,
135+ "name" : " python" ,
136+ "nbconvert_exporter" : " python" ,
137+ "pygments_lexer" : " ipython2" ,
138+ "version" : " 2.7.6"
139+ }
140+ },
141+ "nbformat" : 4 ,
142+ "nbformat_minor" : 0
143+ }
0 commit comments