Skip to main content

Flask decorator to output RDF using content negotiation

Project description

A Flask decorator to output RDF using content negotiation.

Apply the @flask_rdf decorator to a view function and return an rdflib Graph object. Flask_rdf will automatically format it into an RDF output format, depending on what the request’s Accept header says. If the view function returns something besides an rdflib graph, it will be passed through without modification.

Custom formats can be registered easily. After registering the new serializer with rdflib’s plugin support, use the decide_format method to register a new mimetype request to use the new formatter.

API

  • add_format

    Registers a new format to be recognized for content negotiation. It accepts arguments mimetype, serialize_format, and is used to add any custom rdflib serializer plugins to be used for the content negotiation. A third argument, requires_context, will restrict this serializer to only be used by graphs that are context_aware

  • decide_format

    Given an Accept header, return a (mimetype, format) tuple that would best satisfy the client’s request. A second argument, context_aware, may be used to allow formats that require a context_aware graph

  • flask_rdf

    Decorator for a Flask view function to use the Flask request’s Accept header. It handles converting an rdflib Graph object to the proper Flask response, depending on the content negotiation. Other content is returned without modification.

Example

#!/usr/bin/env python
from rdflib import Graph, BNode, Literal, URIRef
from rdflib.namespace import FOAF
from flask import Flask
from flask_rdf import flask_rdf
import random

app = Flask(__name__)

@app.route('/')
@app.route('/<path:path>')
@flask_rdf
def random_age(path=''):
    graph = Graph('IOMemory', BNode())
    graph.add((URIRef(path), FOAF.age, Literal(random.randint(20, 50))))
    return graph

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
Build Status

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_rdf-0.1.8.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distributions

flask_rdf-0.1.8-py3-none-any.whl (6.3 kB view hashes)

Uploaded Python 3

flask_rdf-0.1.8-py2-none-any.whl (6.3 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page