Wikidata-to-Entity-Graph builds a clean, connected entity-relationship graph from any Wikidata entity (QID), saving the result as JSON files for use in data analysis or visualization.
This project was made mostly to provide as a backend for my wikidata entity FDG project, and so filtering is quite intense due to wikidata's data being messy and full of metadata/different languages.
- SPARQL and Wikidata API: Gained hands-on experience with SPARQL queries, batching, and handling the quirks of the Wikidata endpoint.
- Graph Data Cleaning: Learned the importance of cleaning and validating graph data, including handling missing labels, disconnected nodes, and invalid references.
- Efficient Caching: Implemented caching to avoid redundant API calls and speed up repeated queries.
- RESTful API Design: Built a robust Flask API with clear endpoints and flexible parameters for graph exploration.
- Deployment: Automated deployment to Railway using Gunicorn, and made the server auto-detect its environment for seamless local and cloud use.
- Testing and Debugging: Developed backend tests to check for data consistency, connectivity, and filtering correctness.
- Wikidata Rate Limiting: Had to tune worker counts and batch sizes to avoid being throttled or blocked by the Wikidata SPARQL endpoint.
- Data Inconsistencies: Encountered missing or malformed labels, orphaned relations, and other real-world data issues that required robust cleaning logic.
- Graph Expansion Control: It was difficult to precisely control the number of entities/relations due to the unpredictable nature of graph crawling and filtering.
- Deployment Path Issues: Ensured that config and data files were always found regardless of local or cloud deployment paths.
- Maintaining Consistent Data Formats: Needed to keep entity and property data formats consistent between cache, API, and frontend expectations.
- Debugging in Production: Diagnosed and fixed issues that only appeared after deployment, such as environment variable handling and file path mismatches.
- Crawls Wikidata: Starting from any QID, recursively fetches related entities and properties up to a specified depth and relation limit.
- Cleans Data: Removes entities/properties with missing labels, unconnected nodes, and invalid relations.
- Caches Results: Provides functions to store entities, properties, and relations in JSON files for fast reuse.
- REST API: Flask server provides endpoints to fetch graph data for any entity.
- Configurable: Batch sizes, thumbnail size, and more are easily configurable.
- Crawl: Given a Wikidata QID, the backend fetches all related entities and properties up to a user-specified depth and relation limit.
- Clean: Data is filtered to remove missing labels, disconnected nodes, and invalid references.
- Cache: Results are saved in
data/entities.json,data/properties.json, anddata/relations.json. - Serve: The Flask API serves the graph data for any QID on request.
-
GET /api/graph/<entity_id>?depth=1&relation_limit=5
Returns a JSON object with:entities: All entities in the subgraphproperties: All properties used in the subgraphrelations: All relations (edges) in the subgraph
-
GET /data/entities.json
Returns the cached entities as JSON. -
GET /data/properties.json
Returns the cached properties as JSON. -
GET /data/relations.json
Returns the cached relations as JSON.
Python:
import requests
response = requests.get("http://localhost:5000/api/graph/Q42?depth=2&relation_limit=5")
data = response.json()
print(data["data"]["entities"])Frontend:
fetch("http://localhost:5000/api/graph/Q42?depth=2&relation_limit=5")
.then((res) => res.json())
.then((data) => console.log(data.data.entities));-
Install dependencies:
pip install -r requirements.txt -
Run the server:
python WikiGraphServer.pyThe API will be available at
http://localhost:5000.
- The app is ready for deployment with Gunicorn:
web: gunicorn WikiGraphServer:app - The server will auto-detect the environment and use the correct port.
WikiGraphServer.py— Flask API serverbackend_tester.py- Script to test backend is return clean and valid datapy/WikiGraph_Manager.py— Orchestrates crawling, cleaning, and savingpy/Entity_Crawler.py— Handles graph crawling from Wikidatapy/Data_Handler.py— Handles caching and file I/Opy/Cleaner/— Cleans and validates graph datapy/Wikidata_Client/— Handles all Wikidata API/SPARQL callsdata/— Stores cached JSON data
config.json— Set global options (e.g., thumbnail size)requirements.txt— Python dependencies
MIT License (see LICENSE file)
Created by Kieran B, 2025.