A package that helps to build components for the Qanary Framework
Project description
Qanary Helpers library
Qanary Helpers implements registration and querying functionality for the Qanary framework.
This library is used within a Python Qanary Component.
Install
Via PIP
pip install qanary_helpers
Latest version from GitHub
git clone https://github.com/Perevalov/qanary_helpers.git
cd qanary_helpers
pip install .
Usage
For the "Hello world example" create a file named component.py
in your working directory. Then, fill the file with the
following code (pay attention to the TODO
comments):
import os
from datetime import datetime
from flask import Flask, request, jsonify
from qanary_helpers.registration import Registration
from qanary_helpers.registrator import Registrator
from qanary_helpers.qanary_queries import insert_into_triplestore, get_text_question_in_graph
# TODO: may be changed to other config type (e.g. parsing a json file)
SPRING_BOOT_ADMIN_URL = os.environ['SPRING_BOOT_ADMIN_URL']
SPRING_BOOT_ADMIN_USERNAME = os.environ['SPRING_BOOT_ADMIN_USERNAME']
SPRING_BOOT_ADMIN_PASSWORD = os.environ['SPRING_BOOT_ADMIN_PASSWORD']
SERVICE_HOST = os.environ['SERVICE_HOST']
SERVICE_PORT = os.environ['SERVICE_PORT']
SERVICE_NAME_COMPONENT = os.environ['SERVICE_NAME_COMPONENT']
SERVICE_DESCRIPTION_COMPONENT = os.environ['SERVICE_DESCRIPTION_COMPONENT']
URL_COMPONENT = f"http://{SERVICE_HOST}:{SERVICE_PORT}"
app = Flask(__name__)
@app.route("/annotatequestion", methods=["POST"])
def qanary_service():
triplestore_endpoint_url = request.json["values"]["urn:qanary#endpoint"]
triplestore_ingraph_uuid = request.json["values"]["urn:qanary#inGraph"]
question_text = get_text_question_in_graph(triplestore_endpoint_url, triplestore_ingraph_uuid)
# Start TODO: configure your business logic here and adjust the sparql query
result = "Hello World"
SPARQLquery = """
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX dbo: <http://dbpedia.org/ontology/>
INSERT {{
GRAPH <{uuid}> {{
?a oa:annotationText "{annotation_text}" .
?a oa:annotatedBy <urn:qanary:{component}> .
?a oa:annotatedAt ?time .
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph_uuid,
component=SERVICE_NAME_COMPONENT.replace(" ", "-"),
annotation_text=result)
insert_into_triplestore(triplestore_endpoint_url,
SPARQLquery) # inserting new data to the triplestore
# End TODO
return jsonify(request.get_json())
@app.route("/health", methods=["GET"])
def health():
return "alive"
if __name__ == "__main__":
metadata = {
"start": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"description": SERVICE_DESCRIPTION_COMPONENT,
"written in": "Python"
}
registration = Registration(
name=SERVICE_NAME_COMPONENT,
serviceUrl=f"{URL_COMPONENT}",
healthUrl=f"{URL_COMPONENT}/health",
metadata=metadata
)
reg_thread = Registrator(SPRING_BOOT_ADMIN_URL, SPRING_BOOT_ADMIN_USERNAME,
SPRING_BOOT_ADMIN_PASSWORD, registration)
reg_thread.setDaemon(True)
reg_thread.start()
app.run(host='0.0.0.0', port=SERVICE_PORT, debug=True)
As you may see, several environment variables has to be set before the script execution:
SPRING_BOOT_ADMIN_URL
-- URL of the Qanary pipeline (see Step 1 and Step 2 of the tutorial)SPRING_BOOT_ADMIN_USERNAME
-- the admin username of the Qanary pipelineSPRING_BOOT_ADMIN_PASSWORD
-- the admin password of the Qanary pipelineSERVICE_HOST
-- the host of your component without protocol prefix (e.g.http://
). It has to be visible to the Qanary pipelineSERVICE_PORT
-- the port of your component (has to be visible to the Qanary pipeline)SERVICE_NAME_COMPONENT
-- the name of your componentSERVICE_DESCRIPTION_COMPONENT
-- the description of your component
You may also change the configuration via environment variables to any configuration that you want (e.g. via a json
file).
To run the component, simply execute python component.py
in your terminal.
If the component registration was successful, a corresponding message will appear in the output.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file qanary-helpers-0.1.0.tar.gz
.
File metadata
- Download URL: qanary-helpers-0.1.0.tar.gz
- Upload date:
- Size: 18.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/56.2.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2301172935d4a049f5493cd834adcf5f196b65ce826ce2671bd67900fbb5add5 |
|
MD5 | 3b25df263a06a26e401195dbb8c5daee |
|
BLAKE2b-256 | 60e632dc34cb963865e3af02b98b13465e5625989cd2921d2ce79a5131b920fb |
Provenance
File details
Details for the file qanary_helpers-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: qanary_helpers-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/56.2.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aca56a64e066f5fec7cba2fbba2297a81ec4402f6a00b94b61ed68688b5f920b |
|
MD5 | a76512bb6eaf758f7188a99fdb207fec |
|
BLAKE2b-256 | 6cbe2d037218ce1770b96e39e1451e0febf95c261cd0b496c7ac2a215158ae16 |