Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin-ability for views and CLI (#91)
* no need to speak of Jeju island anymore in the base template * clean up CLI Readme * introduce FLEXMEASURES_LISTED_VIEWS, retire FLEXMEASURES_SHOW_CONTROL_UI * Analytics page: fix metrics table header for weather sensor * import plugins as Blueprints * enable listing configured plugin view names in menu * add general documentation for creating a plugin Co-authored-by: Nicolas Höning <nicolas@seita.nl>
- Loading branch information
1 parent
ad6fa3f
commit 134fb69
Showing
12 changed files
with
200 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
.. _plugins: | ||
|
||
Writing Plugins | ||
==================== | ||
|
||
You can extend FlexMeasures with functionality like UI pages or CLI functions. | ||
|
||
A FlexMeasures plugin works as a `Flask Blueprint <https://flask.palletsprojects.com/en/1.1.x/tutorial/views/>`_. | ||
|
||
.. todo:: We'll use this to allow for custom forecasting and scheduling algorithms, as well. | ||
|
||
|
||
How it works | ||
^^^^^^^^^^^^^^ | ||
|
||
Use the config setting :ref:`plugin-config` to point to your plugin(s). | ||
|
||
Here are the assumptions FlexMeasures makes to be able to import your Blueprint: | ||
|
||
- The plugin folder contains an __init__.py file. | ||
- In this init, you define a Blueprint object called ``<plugin folder>_bp``. | ||
|
||
We'll refer to the plugin with the name of your plugin folder. | ||
|
||
|
||
Showcase | ||
^^^^^^^^^ | ||
|
||
Here is a showcase file which constitutes a FlexMeasures plugin. We imagine that we made a plugin to implement some custom logic for a client. | ||
|
||
We created the file ``<some_folder>/our_client/__init__.py``. So, ``our_client`` is the plugin folder and becomes the plugin name. | ||
All else that is needed for this showcase (not shown here) is ``<some_folder>/our_client/templates/metrics.html``, which works just as other FlexMeasures templates (they are Jinja2 templates and you can start them with ``{% extends "base.html" %}`` for integration into the FlexMeasures structure). | ||
|
||
|
||
* We demonstrate adding a view which can be rendered via the FlexMeasures base templates. | ||
* We also showcase a CLI function which has access to the FlexMeasures `app` object. It can be called via ``flexmeasures our_client test``. | ||
|
||
.. code-block:: python | ||
from flask import Blueprint, render_template, abort | ||
from flask_security import login_required | ||
from flexmeasures.ui.utils.view_utils import render_flexmeasures_template | ||
our_client_bp = Blueprint('our_client', 'our_client', | ||
template_folder='templates') | ||
# Showcase: Adding a view | ||
@our_client_bp.route('/metrics') | ||
@login_required | ||
def metrics(): | ||
msg = "I am part of FM !" | ||
# Note that we render via the in-built FlexMeasures way | ||
return render_flexmeasures_template( | ||
"metrics.html", | ||
message=msg, | ||
) | ||
# Showcase: Adding a CLI command | ||
import click | ||
from flask import current_app | ||
from flask.cli import with_appcontext | ||
our_client_bp.cli.help = "Our client commands" | ||
@our_client_bp.cli.command("test") | ||
@with_appcontext | ||
def oc_test(): | ||
print(f"I am a CLI command, part of FlexMeasures: {current_app}") | ||
.. note:: Plugin views can also be added to the FlexMeasures UI menu ― just name them in the config setting :ref:`menu-config`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.