How to: create a plugin
- Keywords:
- plugin
- Last updated by:
- Jorgealda
Alg3py has several options to manage matrices and scalars (and there will be a lot more in near future). But, if you need a feauture not available in Alg3py, you can add it writing your very-own plugin. You only need a little knowing of Python, and follow these guidelines:
1 - Plugin outline
First, create a python document (extension .py). It must contain a class called plugin_container, with three string atributes to store your name, plugin name and a brief description of what your plugin does. It must also have a member function, called code, with two attributes, self and widget. So a basic plugin would be:
class plugin_container():
author = 'J. Doe'
name = 'Simple plugin'
descr = 'Demonstration of a plugin for Alg3py'
def code(self, widget):
#do stuff...
_______
2 - Getting the object displayed in Alg3py
You can access the object currently displayed using self.parent.current This object is a dictionary, with the following keys:
['Object']
The matrix/
['id']
An unique identfier for the object
['Name']
Name of the object
['Type']
It takes the values 'Matrix', 'Vector', 'Scalar' and 'None'
['descr']
Description of the object
['Path']
Path where the object was last saved. If it hasn't never been saved, its value is '-'
['using']
Always set to True
_______
3 - Adding an object to the Alg3py right panel
First create a dictionary with all the keys described above. (The next identifier number is stored at self.parent.idc). Then use it as the argument for the self.parent.
M = Matrix(3, 3)
Obj = {'Object':M, 'id':self.
self.parent.
_______
4 - Getting an object not displayed: using dialogs
To use the dialog, import its python module with the clause from Dialog.
To open a selection dialog, create an instance of object SelectobjectDialog, pass it the type of objects and the list of open objects with the method type_of_
select = SelectobjectDia
select.
response = select.run()
if response == Gtk.ResponseTyp
read_matrix = select.Object
select.destroy()
_______
5 - Summing up: Entrywise multiplication example
Let's see an example of a complete plugin at work. It will perform a special kind of matricial product, namely Hadamard product. The product of two matrices with the same dimensions, C = A·B, is a matrix with the same size such that C[(i, j)] = A[(i, j)] * B[(i, j)]
from Dialog.
from gi.repository import Gtk
class plugin_container():
author = 'J. Doe'
name = 'Hadamard product'
descr = 'Entrywise multiplication of two matrices'
def code(self, widget):
#If the user is not displaying a matrix, finish the plugin
if self.parent.
return
#Reading current object
A = self.parent.
#Creating a selection dialog
select = SelectobjectDia
response = select.run()
if response == Gtk.ResponseTyp
objB = select.Object
B = objB['Object']
else:
#if user cancels the selection, finish the plugin
return
#Check that both matrices have the same dimension
if A.rows != B.rows or A.columns != B.columns:
msg = Gtk.MessageDial
return
#Create an empty matrix
C = A.copy()
#Perform the Hadamard multiplication: loop over the elements of both matrices
for row in range(0, A.rows):
for column in range (0, A.columns):
#Create a dictionary with necessary data:
descr = 'Hadamard product of matrices ' + self.parent.
Obj = {'Object':C, 'id':self.
#Add the result to Alg3py's main window