Project

General

Profile

How to create a Prewikka plugin - Part 1 HelloWorld

This tutorial is based on Prewikka 1.2.6 and Python 2.X (where X >= 6).
It will teach you how to create a simple plugin that adds a HelloWorld page to the Prewikka interface.
For a more complete example of a Prewikka plugin, you can visit this page.

Introduction

You will need only two Python files for this plugin:
  • the main file containing the plugin interesting content;
  • the setup script for distributing and installing the plugin.

File hierarchy

|-- helloworld
|   `-- __init__.py
`-- setup.py

Creating a new plugin

Let's begin by creating a directory named helloworld and editing our main file inside it named __init__.py:

"""A HelloWorld plugin""" 
from prewikka import view

class HelloWorld(view.View):
    """The view that displays Hello World""" 
    plugin_name = "Hello World" 
    plugin_description = "A plugin that says hello world!" 
    plugin_version = "1.0.0" 
    view_name = "Hello" 
    view_section = "Hello" 
Here we defined our plugin HelloWorld as a sub-class of view.View with the following attributes:
  • plugin_name, plugin_description and plugin_version: this information will be displayed on the Plugins page.
  • view_name: this is the text of the tab that will contain our HelloWorld view.
  • view_section : this is the menu section in which the tab will appear. Since there is no Hello section defined yet, this section will be automatically added to the menu.

Adding content

Now that the plugin is created, some content must be added in order to greet the user with our message.
This is done through the render() function, inside our HelloWorld class:

    def render(self):
        return "<div>Hello World!</div>" 

And that's it.

Writing the setup script

The last thing to do is writing the setup.py script at the root of the project. The file should look like this:

from setuptools import setup

setup(name="prewikka-helloworld",
      version="1.0.0",
      author="John Doe",
      author_email="john.doe@acme.com",
      url="https://prelude-siem.org",
      packages=["helloworld"],
      install_requires=["prewikka"],
      entry_points={
          "prewikka.views": [
              "HelloWorld = helloworld:HelloWorld",
          ],
      })

Not much to say here, but please note the important part: defining the entry point for our plugin.
As we want Prewikka to take our plugin into account, we should register it in the prewikka.views entry point.

Testing the plugin

Installation

Let's install the plugin:

python setup.py install

Plugins page

After a Prewikka restart, you can check that it has correctly been loaded by visiting the Plugins page:

HelloWorld page

And here is our HelloWorld page:

For a more complete example of a Prewikka plugin, please continue this tutorial here.

plugins.png View (10.9 KB) Thomas ANDREJAK, 09/08/2015 05:08 PM

helloworld.png View (5.22 KB) Thomas ANDREJAK, 09/08/2015 05:08 PM