lambdamusic / Ontospy

Python library and command-line interface for inspecting and visualizing RDF models aka ontologies.
http://lambdamusic.github.io/Ontospy/
MIT License
218 stars 52 forks source link

How to Use in A Django App #88

Closed ImanoWilliams closed 2 years ago

ImanoWilliams commented 4 years ago

Hi, I have a Django application that user can query an ontology. I would like to know how I could visualize my ontology using Ontospy using Django. I see where there is Ontowebspy that is now archived.

lambdamusic commented 2 years ago

Ontospy does not include any out of the box functionality for Django, but you can use the library like any other Python data processor.

For example

#
# in views.py
#

def ontology_page(request, uri=""):

    context = {}

    templatee = "my-ontology-template.html"

    # .. 
    # .. set ontospy opts
    # 

    g = Ontospy(
        URI, 
        verbose=verbose, 
        pref_title=preflabel, 
        pref_lang=preflang,
        hide_base_schemas=hide_base_schemas,
        hide_implicit_types=hide_implicit_types,
        hide_implicit_preds=hide_implicit_preds,
        hide_individuals=hide_individuals)

        context_data = {
            "topclasses": g.topclasses,
            "classes": g.all_classes,
            "properties": g.all_properties,
        # etc...
        }

    return render(request, templatee, context)

Then in the template you simply render the ontology properties using HTML. For example, see https://github.com/lambdamusic/Ontospy/blob/v1.9.9.1/ontospy/ontodocs/media/templates/html-single/html-single.html - this template is used by the ontospy gendocs command but that's basically a Django template..