Skip to content

Commit 8a4f3a9

Browse files
committed
Add tutorials on common cases with more examples.
1 parent 064e842 commit 8a4f3a9

12 files changed

Lines changed: 261 additions & 0 deletions

docs/.pages

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ nav:
44
- whats-new-version-4.md
55
- installation.md
66
- conformance.md
7+
- Tutorials:
8+
- tutorials
79
- Reference:
810
- reference
911
- Project:

docs/examples/inject_context.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
3+
from pyld import jsonld
4+
5+
doc = {
6+
"name": "Ada Lovelace",
7+
"homepage": "https://example.com/ada",
8+
}
9+
context = {
10+
"name": "http://schema.org/name",
11+
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
12+
}
13+
14+
expanded = jsonld.expand(doc, {"expandContext": context})
15+
16+
print(json.dumps(expanded, indent=2))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import json
2+
3+
from pyld import jsonld
4+
5+
doc = {
6+
"name": "Ada Lovelace",
7+
"homepage": "https://example.com/ada",
8+
}
9+
source_context = {
10+
"name": "http://schema.org/name",
11+
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
12+
}
13+
output_context = {
14+
"name": "http://schema.org/name",
15+
"url": {"@id": "http://schema.org/url", "@type": "@id"},
16+
}
17+
18+
compacted = jsonld.compact(
19+
doc,
20+
output_context,
21+
{"expandContext": source_context},
22+
)
23+
24+
print(json.dumps(compacted, indent=2))

docs/examples/open_jsonld_file.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
from pathlib import Path
3+
4+
from pyld import jsonld
5+
6+
path = Path(__file__).resolve().parent / "data" / "person.jsonld"
7+
doc = json.loads(path.read_text())
8+
9+
expanded = jsonld.expand(doc)
10+
11+
print(json.dumps(expanded, indent=2))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
from pathlib import Path
3+
4+
from pyld import jsonld
5+
6+
path = Path(__file__).resolve().parent / "data" / "person.jsonld"
7+
doc = json.loads(path.read_text())
8+
9+
nquads = jsonld.to_rdf(doc, {"format": "application/n-quads"})
10+
11+
print(nquads)

docs/examples/rdflib_parse.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
3+
from rdflib import Dataset
4+
5+
from pyld import jsonld
6+
7+
nquads = (
8+
'<http://dbpedia.org/resource/Earth> '
9+
'<http://schema.org/name> "Earth" .\n'
10+
)
11+
dataset = Dataset().parse(data=nquads, format="nquads")
12+
13+
doc = jsonld.from_rdf(dataset)
14+
compacted = jsonld.compact(doc, {"name": "http://schema.org/name"})
15+
16+
print(json.dumps(compacted, indent=2))

docs/examples/rdflib_processing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
3+
from rdflib import Namespace, URIRef
4+
5+
from pyld import jsonld
6+
7+
SCHEMA = Namespace("http://schema.org/")
8+
subject = URIRef("http://dbpedia.org/resource/Earth")
9+
10+
doc = {
11+
"@context": {"name": str(SCHEMA.name)},
12+
"@id": str(subject),
13+
"name": "Earth",
14+
}
15+
16+
dataset = jsonld.to_rdf(doc)
17+
dataset.add((subject, SCHEMA.url, URIRef("https://example.com/earth")))
18+
19+
jsonld_doc = jsonld.from_rdf(dataset)
20+
compacted = jsonld.compact(
21+
jsonld_doc,
22+
{
23+
"name": str(SCHEMA.name),
24+
"homepage": {"@id": str(SCHEMA.url), "@type": "@id"},
25+
},
26+
)
27+
28+
print(json.dumps(compacted, indent=2))

docs/examples/rdflib_query.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import json
2+
3+
from rdflib import Namespace, URIRef
4+
5+
from pyld import jsonld
6+
7+
SCHEMA = Namespace("http://schema.org/")
8+
subject = URIRef("http://dbpedia.org/resource/Earth")
9+
10+
doc = {
11+
"@context": {"name": str(SCHEMA.name)},
12+
"@id": str(subject),
13+
"name": "Earth",
14+
}
15+
16+
dataset = jsonld.to_rdf(doc)
17+
names = [str(value) for value in dataset.objects(subject, SCHEMA.name)]
18+
19+
print(json.dumps(names, indent=2))

docs/tutorials/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
hide: [toc]
3+
---
4+
5+
# :material-school-outline: Tutorials
6+
7+
<div class="grid cards" markdown>
8+
9+
- [:material-library:{ .lg .middle } RDFLib and `jsonld`](rdflib-processing.md)
10+
11+
---
12+
13+
Convert JSON-LD to an `rdflib.Dataset`, process it with RDFLib, and convert
14+
it back to JSON-LD.
15+
16+
- [:material-code-json:{ .lg .middle } Inject a JSON-LD `@context`](inject-context.md)
17+
18+
---
19+
20+
Apply a context to plain JSON when the source cannot provide an in-band
21+
`@context` or HTTP `Link` header.
22+
23+
- [:material-file-code-outline:{ .lg .middle } Open a JSON-LD file](open-jsonld-file.md)
24+
25+
---
26+
27+
Load a local `.jsonld` file, process it with PyLD, and choose the right
28+
dependencies for local or remote contexts.
29+
30+
</div>

docs/tutorials/inject-context.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# :material-code-json: Inject a JSON-LD `@context`
2+
3+
JSON-LD property names only become RDF terms when a context maps them to IRIs.
4+
If the input JSON has no `@context`, and the source cannot provide one through
5+
an HTTP `Link` header, pass the context with `expandContext`.
6+
7+
Use this for JSON already loaded from a file, queue, database, or API client:
8+
9+
1. Load or receive the plain JSON document.
10+
2. Define the context your application wants to apply.
11+
3. Pass that context in the operation `options` as `expandContext`.
12+
4. Run `jsonld.expand()` or another API that expands internally.
13+
14+
PyLD processes `expandContext` before expansion. If the document also has its
15+
own `@context`, that document context is processed later and can refine or
16+
override term definitions for the document body.
17+
18+
{{ example('inject_context.py', 'json') }}
19+
20+
The same option works with APIs that expand internally, including
21+
`jsonld.compact()`, `jsonld.flatten()`, and `jsonld.frame()`:
22+
23+
{{ example('inject_context_compact.py', 'json') }}
24+
25+
Use an in-document `@context` when you control the JSON. Use `expandContext`
26+
when the context is application policy or transport metadata.

0 commit comments

Comments
 (0)