Plugins¶
Plugin Example¶
A basic plugin tree¶
example_plugin
│- README.md
│- __init__.py
│- example_plugin.py
│
└───templates
│- settings.inc.html
│- my_template.html
│
└───js
│- my_lib.js
The python module example_plugin.py have reserved class
html # HTML on /example_plugin/ endpoint
rest # JSON on /rest/example_plugin/ endpoint
The following example import pyrmin module to render the Jinja html template on /
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pyrmin
# Plugin Header
__name__ = "Example Plugin"
__description__ = "\
My long Description"
__version__ = "0.1.0"
__pyrmin_version__ = "0.4.0"
__namespace__ = "example_plugin"
__menu__ = {
"href": "/" + __namespace__,
"label": "Packages",
"icon": "fa-database"
}
class html():
def __init__(self, config):
self.config = config
self.tools = pyrmin.tools(config)
def index(self, core, **kwargs):
""" Root View For Plugin /example_plugin/
Will render a Winja2 template named example.html
"""
tmpl = self.tools.init_html('example.html')
return tmpl.render(core=core, title='My Example Plugin Home')
def register(self):
return ['path1']
def path1(self):
core = self.tools.get_core_html(self.name)
tmpl = self.tools.init_html('example.html')
return tmpl.render(core=core, title='My Path 1')
def submenu(self):
""" Register submenu
"""
links = self.tools.add_submenu(['my sub-plugins'])
links.append({
"href": "/" + __namespace__ + "/path1",
"icon": "fa-thumbs-up",
"label": "Path 1",
"childs": {}
})
return links
The template example.html extend the base.html template to provides to user with the default menu
{% extends "base.html" %}
{% block css %}
<link href="/plugins/example_plugin/css/example.css" rel="stylesheet" type="text/css">
{% endblock %}
{% block header %}
<h1>My Title</h1>
{% endblock %}
{% block content %}
<div class="row">
<p>My content</p>
</div>
{% endblock %}
{% block js %}
<script src="/plugins/example_plugin/js/my_lib.js" type="text/javascript"></script>
{% endblock %}