(One of my summaries of a talk at the 2017 PyCon.de conference).
Raphael Michel asked for a show of hands. “Who is a python web developer”. “Who used wordpress previously”. Quite a number of us have worked with wordpress. About a quarter of the internet runs on wordpress or so. One of the reasons is its plugin mechanism that makes it possible to do lots of things.
He works on pretix, a python ticket (concert tickets, not bug tracker tickets) system. They wanted to make it extensible, as there are many special cases (for instance payment processors).
Their idea:
Establish a way that plugins can hook into your applications.
Provide many of these hooks themselves.
Document it well
They started with a simple signal system. A class that you can register functions on and a send method that simply calls all the registered functions. They set it up so that you could use decorators.
If you want to use something like that and you’re using django: use django’s
django.dispatch.signal
. If you use django, do also package it up as a
‘django applications’.
But… if you want to use such an application, you have to:
Install the package.
Add the app to the INSTALLED_APPS
setting.
Adjust your urls.py
Yes, this list is already getting too long.
How can we make this easier?
One way you can do is to add an AppConfig in your app’s __init__.py
. You can
add add custom data into your AppConfig object, for instance some metadata
that your plugin mechanism can pick up.
Within django, you can loop over all the registered apps and query them for that metadata. If so, you can automatically add urls to your URL config.
A second way is a setuptools entrypoint (I have an introduction about them on my blog). This is a standard setuptools mechanism to register functions or classes under some name. A name which we can then query for.
Nice: they have a mechanism for enabling/disabling plugins. Core to their approach is that a plugin’s functionality is called from the main application. If it isn’t called, it doesn’t do anything. Simple.
Essential for adoption of your plugin mechanism:
Document it very, very well.
Provide a cookiecutter template to make it easy to start in the right way.
Photo explanation: simply a picture from my train trip (with a nice planned detour through the Eifel) from Utrecht (NL) to Karlsruhe (DE). Station building of ‘Urft’ along the wonderful Köln-Gerolstein-Trier line.
My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):