Comunica for LiaScript: Run SPARQL Queries and Explore Knowledge Graphs

Comunica for LiaScript: Run SPARQL Queries and Explore Knowledge Graphs

The Semantic Web and Linked Open Data are among the richest topics in computer science and information science — but teaching SPARQL interactively has always required running a server.

The Comunica template changes that. Built on Comunica, a flexible open-source knowledge graph querying framework, this LiaScript template lets you write and execute SPARQL queries, define custom RDF datasets in Turtle notation, and query real-world knowledge graphs — all directly in the browser, without any backend or configuration.


What Is SPARQL?

SPARQL is the standard query language for RDF data — think SQL, but for knowledge graphs. Instead of rows and columns, you work with subject–predicate–object triples. SPARQL queries live knowledge graphs like DBpedia and Wikidata, as well as countless domain-specific open datasets.

The Comunica template makes every code block in your LiaScript course a fully executable SPARQL editor — learners can run and modify queries without leaving the page.


Quick Start

Add the import to your course header:

<!--
import: https://raw.githubusercontent.com/LiaTemplates/Comunica/0.0.3/README.md
-->

For a version-stable import (recommended for published courses):

<!--
import: https://raw.githubusercontent.com/LiaTemplates/Comunica/0.0.3/README.md
-->

That’s it. Both macros — @Comunica.SPARQL and @Comunica.RDF_SPARQL — are now available throughout your document.


Macro 1: @Comunica.SPARQL — Query Live Endpoints

The core macro turns any sparql code block into an interactive SPARQL editor. Add a # source: comment to specify the endpoint, then attach @Comunica.SPARQL after the closing fence:

``` sparql
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Belgium>.
  ?s ?p ?o
} LIMIT 10
```
@Comunica.SPARQL

Try it live:

Multiple Sources

You can query across multiple endpoints simultaneously — just add multiple # source: comments:

``` sparql
# source: https://fragments.dbpedia.org/2015/en
# source: https://ruben.verborgh.org/profile/

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Belgium>.
  ?s ?p ?o
} LIMIT 5
```
@Comunica.SPARQL

Output Formats

The default output is a table. Override it with a # format: comment inside the code block:

FormatDescription
tableText-based visual table (default)
simplePlain text result
application/jsonSimplified JSON
application/sparql-results+jsonStandard SPARQL/JSON
text/csvCSV output
text/tab-separated-valuesTSV output

Example:

``` sparql
# format: simple
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 5
```
@Comunica.SPARQL

Macro 2: @Comunica.RDF_SPARQL — Query Custom RDF Data

The second macro combines two adjacent code blocks: the first defines your own RDF dataset in Turtle notation, the second contains your SPARQL query. Both are attached together with @Comunica.RDF_SPARQL:

``` turtle       Turtle
@prefix ex:   <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:Dog    rdfs:subClassOf ex:Animal .
ex:Person rdfs:subClassOf ex:Animal .

ex:Max    a ex:Dog .
ex:Alice  a ex:Person .
ex:Bob    a ex:Person ;
          ex:hasChild ex:Alice .
```
``` sparql       -SPARQL-Query
PREFIX ex:   <http://example.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?subject ?type WHERE {
  ?subject a ?type .
}
```
@Comunica.RDF_SPARQL

The text after the language name (e.g., Turtle, -SPARQL-Query) becomes the code block’s title. A leading - means the editor starts collapsed; a leading + means expanded.

Try a live example with a small family dataset:

It is still possible to add # source: or # format: comments inside the SPARQL block when using @Comunica.RDF_SPARQL — the local Turtle data and any live endpoint sources are queried together.


Full Template Demo

The full Comunica README is itself a self-documenting LiaScript course. Explore all macros, output formats, and examples live:


Use Cases

Semantic Web and Linked Data courses — Teach SPARQL syntax interactively: students write queries, run them against DBpedia or Wikidata, and see results immediately. No endpoint setup, no local triple store required.

Research Data Management — Build exercises around real RDF datasets — library catalogues, research object metadata, institutional knowledge graphs. Use @Comunica.RDF_SPARQL with custom Turtle snippets to illustrate data modelling and schema design.

Ontology and knowledge representation — Introduce class hierarchies, property inheritance, and OWL reasoning with small, self-contained Turtle examples. Students can edit both the data and the query and observe how changes propagate.

Information literacy and open data — Let learners explore real open data sources: DBpedia, GeoNames, the Linked Open Data Cloud. Turn abstract graph theory into hands-on data exploration inside an open course.

Computer science seminars — Use @Comunica.RDF_SPARQL to illustrate RDF modelling patterns, federated queries, and the difference between schema and instance data — all without any local installation.


Technical Facts

Runs in browserYes — fully client-side (JavaScript / WebAssembly)
Server requiredNo (for local RDF data); external endpoints are optional
Interactive editingYes — both query and Turtle data are editable
External APIOnly when querying a live endpoint
Offline capablePartially — local RDF works offline; live endpoints need network
Based onComunica v5
LicenseApache-2.0
MaintainedYes (TypeScript, version 0.0.3)
Version-stable importYes (0.0.3 tag available)

Try It

Try in LiaScript Open in LiveEditor View on GitHub
  • SQLite — interactive SQL with a full SQLite database in the browser
  • DuckDB — analytical SQL queries with DuckDB WASM
  • AlaSQL — lightweight in-browser SQL + NoSQL
  • Wikimedia — embed open knowledge and media from Wikimedia Commons

Related Posts

A-Frame for LiaScript: 3D and VR Scenes in Your Course

Embed interactive 3D scenes and VR environments in LiaScript using the A-Frame template — write HTML A-Frame markup in a code block and render a fully interactive WebGL scene on any page.

Read More

Algebrite for LiaScript: A Computer Algebra System in the Browser

Use the Algebrite template to add symbolic math computation to your LiaScript courses — evaluate CAS expressions, check student answers algebraically, and verify equations directly in the browser.

Read More

Redis for LiaScript: Explore Key-Value Stores and Data Structures in the Browser

Use the Redis template to teach key-value stores, Redis data structures, TTL, transactions, and caching patterns — directly in your LiaScript courses, with no server needed.

Read More