Plugin Manager¶
Plugin Manager provides an interface to help you easily manage all your plugins.
Config¶
Plugin manager is configured with Flask.
plugins_blueprint: The plugin manager will register a Blueprint on the Flask App to manage the plugin. This configuration value will be used to name the Blueprint and is also theurl_prefixvalue for the Blueprint.plugins_direcotry: Your plug-in set directory, relative to the project startup path.plugins_excludes_directory: Within the plug-in set directories, there are some directories that you may want to exclude, which do not contain valid plug-in information, or that have other uses; just add their names within this list of configuration items.
You don’t have to provide all the configuration, when Flask-Plugin cannot find the above configuration inside the bound App, it will load a default configuration, which is defined here: config.DefaultConfig。
About how to configure Flask, see: https://flask.palletsprojects.com/en/2.0.x/config/
Create and bind to a Flask app¶
Once your application is configured, bind it to a PluginManager instance to:
from flask import Flask
from flask_plugin import PluginManager
app = Flask(__name__)
manager = PluginManager(app)
Of course you can also create a manager first and then bind your application later:
manager = PluginManager()
app = Flask(__name__)
manager.init_app(app)
Once the application is bound, the instance of the manager bound to the application can be accessed through app.plugin_manager.
The current_app proxy also contains its references.
Auto-discovery of plugins¶
Once the application binding is complete, you can access all plugins using PluginManager.plugins. This attribute will automatically discover all instances of unloaded plugins, followed by instances of loaded plugins at
for plugin in manager.plugins:
...
If you want to manually scan all plug-in directories, you can call the PluginManager.scan() method, which will iterate over all unloaded plug-in instances (but usually you don’t need to do this).
When the specified plugin directory is inaccessible, the method raises a FileNotFoundError。
Plugin Control¶
After you get the plugin instance, you can use methods PluginManager.load(), PluginManager.start(), PluginManager.stop(), PluginManager.unload() to control plugin.
Methods above accept a Plugin instance. It will check if the current state of the plugin allows the transfer operation first, and when the operation is prohibited it will raise a RuntimeError.
Get Plugins Info¶
If you want to get the status information of all plugins at once, the PluginManager.status can help you to call all plugins (including unloaded) of the Plugin.export_status_to_dict() and return as a list:
>>> manager.status
[{'domain': 'flaskex',
'id': '334c997a6c4946aaa920f2ffccc27077',
'info': {'author': '', 'description': '', 'version': (0, 0, 0)},
'name': 'flaskex',
'status': 'Unloaded'},
{'domain': 'hello',
'id': '347336b4fcdd447985aec57f2bc5793c',
'info': {'author': '', 'description': '', 'version': (0, 0, 0)},
'name': 'Greeting',
'status': 'Unloaded'}]