lambdamusic / Ontospy

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

Make jinja2 env configurable #138

Open jgsogo opened 4 months ago

jgsogo commented 4 months ago

Feature: VizFactory can take the jinja2 environment from outside (fallbacks to the same default it was before)

With this change, users can override the templates used for the different visualizers. Example:

import ontospy
from ontospy.gendocs.viz.viz_html_single import *
from ontospy.gendocs.jinja_env import add_default_filters
from jinja2 import Environment, PackageLoader, select_autoescape, ChoiceLoader

g = ontospy.Ontospy("http://cohere.open.ac.uk/ontology/cohere.owl#")

# Provide my jinja2 environment so I can override some templates
my_environment = Environment(
    loader=ChoiceLoader(
        [
            FileSystemLoader(["/override/templates"]),
            PackageLoader("ontospy.gendocs.media", "templates"),
        ]
    ),
    autoescape=select_autoescape(["html", "xml"]),
    extensions=["jinja2_time.TimeExtension"],
)
add_default_filters(my_environment)

v = HTMLVisualizer(g, jinja2env=my_environment) # => instantiate the visualization object
v.build() # => render visualization. You can pass an 'output_path' parameter too
v.preview() # => open in browser

Personal note.- I plan to use the gendocs capability (html_multi) for one of my projects (I really like the output!). I would like to contribute to the project. Besides the contributions I would love to do around the template framework, I can help updating the codebase (https://github.com/lambdamusic/Ontospy/issues/129) or improving the testing.