Using IPython Notebook (with rdflib?) to log SPARQL queries and results -
Using IPython Notebook (with rdflib?) to log SPARQL queries and results -
i want utilize ipython notebook record sparql queries results of queries.
since command-line tool can called ipython notebook "bang", can of course of study run:
!arq --data dcterms.ttl --query test1.rq
or roqet, can embed short query in command itself:
!roqet -i sparql -e ’select * { ?s ?p ?o }’ -d dcterms.rdf
neither arq or roqet take multi-line sparql queries arguments. query longer one-liner must stored in file (e.g., "test1.rq" above).
far improve define sparql queries straight in ipython notebook cells, cloned , tweaked. next works:
in [4]: myquery = """ prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> build {?s rdf:type ?o} """ in [5]: def turtleme(myquery): import rdflib g = rdflib.graph() g.parse('dcam.rdf') results = g.query(myquery) print results.serialize(format="turtle") in [6]: turtleme(myquery) out [6]: @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/xml/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/xmlschema#> . <http://purl.org/dc/dcam/vocabularyencodingscheme> rdfs:class . <http://purl.org/dc/dcam/memberof> rdf:property .
however, not see way pass sparql query specifies info sources queried, such as:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> build <dcterms.ttl> <dcam.ttl> {?s rdf:type ?o}
or, @ minimum, improve function take @ to the lowest degree 1 filename argument, in
turtleme('dcam.ttl', myquery)
i have scoured google hits examples of using ipython notebook sparql find none. seems obvious utilize environment designed info exploration. method have found works run arq, 1 needs do
!cat test3.rq
to paste query ipython notebook, fulfills function of documenting process of exploring data, queries must edited, in parallel notebook, separate files. objective create easy origin students explore rdf info using sparql , record explorations in notebook. there must improve way!
update:
@joshua taylor, @andys point out commands take multiline queries arguments. works fine @ bash prompt unfortunately not in ipython notebook, throws syntaxerror:
in [5]: !arq --data dcam.ttl ' prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix dcam: <http://purl.org/dc/dcam/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?s ?p ?o { ?s ?p ?o . }' out [5]: file "<ipython-input-5-c9328c1c0c64>", line 2 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ^ syntaxerror: invalid syntax
if escape end of line in line 1, in
in [5]: !arq --data dcam.ttl '\ ... out [5]: file "<ipython-input-18-313c556abc1d>", line 2 prefix dcam: <http://purl.org/dc/dcam/> ^ syntaxerror: invalid syntax
however, cannot entire command execute escaping of ends of line.
so perhaps problem lies not how arq , roqet handle queries in-line how arq , roqet command lines passed ipython notebook?
in ipython notebook, preceding shell command bang ("!") work commands, (e.g., "!date") but, noted above, multiline commands not passed correctly. according the cell magics in ipython
ipython has %%script
cell magic, lets run cell in subprocess of interpreter on system, such as: bash, ruby, perl, zsh, r, etc.
it can script of own, expects input on stdin.
to utilize it, pass path or shell command programme want run on %%script
line, , rest of cell run script, , stdout/err subprocess captured , displayed.
so pass query correctly, ipython notebook cell must begin %%script bash
(or %%bash
), in:
in [5]: %%script bash arq --data dcam.ttl ' prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix dcam: <http://purl.org/dc/dcam/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> build { ?s rdf:type ?o . }'
sparql ipython-notebook rdflib
Comments
Post a Comment