Build your first widget plugin
Write a plugin that draws rows as a table, put it through the review that every plugin passes, and use it on a dashboard. At the end, a widget of a type that you invented shows the data of your backend.
Before you start
You need:
- The FINOS Workspace that Run the stack from source starts, with its administrator signed in.
- The backend of Connect a backend and build a dashboard, connected as Sales demo.
- A text editor, and a terminal in the repository folder, next to
docker-compose.yml.
A plugin never arrives through the app. The person who runs a deployment places its files on the server, a superuser approves them, and an organization admin enables the plugin and says who may use it. Because you run your own FINOS Workspace, you do all three, in that order.
1. Write the module
Make a folder for the lesson, outside the repository, and a folder for the package in it:
mkdir -p ~/workspace-plugin-tutorial/packages/row-table/1.0.0
A package lives in <plugin id>/<version>. In that folder, create entry.js. It is the code that draws the widget:
export default {
render({ model, el }) {
const draw = () => {
const rows = model.get("data");
el.replaceChildren();
if (!Array.isArray(rows) || rows.length === 0) {
el.textContent = "No rows to show.";
return;
}
const columns = Object.keys(rows[0]);
const table = document.createElement("table");
const head = table.createTHead().insertRow();
for (const column of columns) {
const cell = document.createElement("th");
cell.textContent = column;
head.appendChild(cell);
}
const body = table.createTBody();
for (const row of rows) {
const line = body.insertRow();
for (const column of columns) {
line.insertCell().textContent = String(row[column] ?? "");
}
}
el.appendChild(table);
};
model.on("change:data", draw);
draw();
return () => model.off("change:data", draw);
},
};
FINOS Workspace calls render with two things:
elis the element that the widget fills.modelholds the values that FINOS Workspace hands to the widget.datais what the widget's backend endpoint returned, so a list of objects with the same keys arrives as an array.
The function draws the table, draws it again whenever data changes, and returns a function that stops listening when FINOS Workspace removes the widget.
2. Write the manifest
Next to entry.js, create plugin.json:
{
"manifestVersion": 1,
"id": "row-table",
"version": "1.0.0",
"name": "Row table",
"description": "Draws a JSON list of objects as a plain table.",
"publisher": { "name": "Example Publisher" },
"host": { "api": 1, "afm": "0.11" },
"entry": { "module": "entry.js" },
"contributes": {
"widgets": [
{
"id": "table",
"name": "Row table",
"description": "A table of the rows that the backend returns",
"category": "Tables",
"handles": [
{
"type": "rowtable",
"response": {
"accepts": "json",
"description": "A JSON list of objects that all have the same keys."
}
}
],
"permissions": ["core:data:read"],
"grid": { "w": 20, "h": 12 }
}
]
}
}
The manifest says who the plugin is and which widget it offers:
handlesnames the widget type that the plugin draws,rowtable. A backend uses that type in itswidgets.json. It also says what the endpoint has to return.core:data:readlets the widget readdata. A widget gets no permission that it does not ask for.
Plugin package and manifest describes every key.
3. Give the server the package
The API reads plugins from one folder in its container, and it ignores plugins until WIDGET_PLUGINS_DIR names that folder.
-
Create
~/workspace-plugin-tutorial/compose.plugins.yml:services:api:environment:WIDGET_PLUGINS_DIR: /opt/widget-pluginsvolumes:- ${HOME}/workspace-plugin-tutorial/packages:/opt/widget-plugins:ro -
Start the stack again with this file added. Compose recreates the API's container and leaves the rest as it is:
docker compose -f docker-compose.yml -f "$HOME/workspace-plugin-tutorial/compose.plugins.yml" up -d
The mount is read-only, because the API only reads the folder.
4. Check the package
The server has its own validator. It runs the checks that the server applies to a plugin and reports what it would refuse. Run it in the API's container, on your package as the container sees it:
docker compose -f docker-compose.yml -f "$HOME/workspace-plugin-tutorial/compose.plugins.yml" \
exec api python -m scripts.plugin_check /opt/widget-plugins/row-table/1.0.0
The validator refuses the package, and it tells you why:
row-table@1.0.0
files 2
checksum sha256-<a value that starts with sha256->
digest sha256-<another value>
error checksum: Field required
blocked: the server would not offer this version for approval
A manifest has to carry the checksum of its files, so that nobody can change them after the review. To fix it:
-
Copy the value printed after
checksum. -
Add it to
plugin.json, as a new line belowhost:"checksum": "sha256-<the value that the validator printed>", -
Run the validator again. It now ends with
ok: the server would offer this version for approval.
The checksum covers every file except plugin.json. If you change entry.js later, print the checksum again.
5. Approve the plugin
- Open the main menu and choose Admin Portal.
- Select Widgets, then the Deployment tab. The page says that a new plugin does nothing until you approve it.
- Select Rescan. A message says "Rescanned: 1 discovered". The table lists Row table with the status disabled and the version 1.0.0 as discovered.
- Select the plugin. The page shows what the review found:
- who published the plugin, with the line "as the package states it; not verified",
- the version's digest, and that the checksum matches the manifest,
- the files that browsers receive,
- that the frame has no network access and that the plugin runs no code on the server.
- Under What it asks for, tick each request that the version makes: its widget, its permission
core:data:readand its code. - Tick the box that says you take responsibility for trusting the version. Select Approve 1.0.0.
A message says "Row table 1.0.0 approved", and the plugin is enabled at the deployment.
6. Enable the plugin and give it to a role
An approved plugin is available to nobody until an organization enables it and a role grants it.
- On the same page, under Enabled for, turn on the switch of your organization.
- In the admin sidebar, select Roles and Access.
- Open the Widget plugins action of a role that you hold, such as Agent User. The dialog lists Row table.
- Tick All widgets of this plugin and select Save.
A message says "Widget plugin access saved".
Nobody has access to a plugin except through a role, so you cannot skip step 6.
7. Use the widget
A backend that declares a widget of the type rowtable can now use your plugin.
-
In the
widgetsof the backend from the earlier lesson, above the line};that ends the object, add:orders: {name: "Orders",description: "The latest orders, one row for each",category: "Sales",type: "rowtable",endpoint: "/orders",}, -
Add
"/orders"to the list inconst routes. -
Add the route that answers it, above the route for
/notes:if (url.pathname === "/orders") {const orders = [{ id: 1, region: "East", total: 10.5 },{ id: 2, region: "West", total: 4 },{ id: 3, region: "North", total: 7.25 },];return send("application/json", JSON.stringify(orders));} -
Stop the backend with Ctrl+C and start it again with
node backend.mjs. -
Select Connections in the sidebar, then the refresh button beside Connect Backend. The row of Sales demo now shows 4 widgets.
-
Open a dashboard. Select the round button at its bottom right that opens the Widget Menu, and type
Ordersin the search box. Select the widget and add it.
The card first shows nothing, because the plugin loads in a frame of its own. A moment later it shows your table with the three orders. FINOS Workspace asked the backend for the rows, passed them to your module as data, and your code drew them.
Next steps
- Style a plugin widget for both themes: your table is plain because the module draws no styles.
- Add an export to a plugin widget: let people export the rows.
- Plugin module contract: everything that a module can do.
- Plugin: what a plugin is.
- The folder
docs/examplesof the repository holds three complete plugins, two of which draw charts and tables with libraries.