BackendAction.py
from src.backend.PluginManager.ActionBase import ActionBase
import os
import Pyro5.api
@Pyro5.api.expose
class BackendAction(ActionBase):
ACTION_NAME = "Backend Action"
def __init__(self, deck_controller, page, coords):
super().__init__(deck_controller=deck_controller, page=page, coords=coords)
# Launch backend (optional)
backend_path = os.path.join(os.path.dirname(__file__), "backend.py")
self.launch_backend(backend_path=backend_path)
def on_key_down(self):
self.set_bottom_label(self.backend.get_number())
As the name implies, this is an action with a backend, a dedicated backend to be exact. Such a backend is not always necessary, but required if you want to use third party python modules.
If you want to have a look at how to add an action without backend, head over here.
Pyro5¶
The backend of all actions is running in a completely separate python process. This means no normal communication between the backend and the actual action is possible. Here comes Pyro5 into play. It creates a communication channel between the action and the backend over the local network. If you want to know more about Pyro5, you can read it's documentation.
Variables
Due to safety reasons Pyro5 disallows the direct access of variables of the backend from the frontend and vice versa. For this reason you have to use getters and setters.
Let's go over the code:¶
StreamController imports¶
imports the ActionBase which is the base class for all actions.General imports¶
importsos
to generate the path of the backend and Pyro5
to create the communication channel.
Action name¶
This is the name of your action and will be shown in the ui.Init the action¶
def __init__(self, deck_controller, page, coords):
super().__init__(deck_controller=deck_controller, page=page, coords=coords)
The arguments will be provided by the PluginManager of StreamController on initialization of the action.
Backend launch¶
This gets the path of the backend file. This launches the backend and establishes a connection with the backend.The backend will be loaded in a new python process.
Modules
If you want to use third party modules you'll need to run the backend in a custom environment. You can do so by providing venv_activate_path
to the method. Usually this is .venv/bin/activate
.
Head over to __install__.py to learn how to create a custom environment on plugin installation.
on_key_down¶
This function is executed when the action key is pressed. You can add any kind of python code here. To ensure a lag-free experience for the user, all actions on the pressed keys are executed in a dedicated thread. This means you can add time consuming code here without affecting the application. However, any actions on the button after that will be delayed to ensure that the actions are always called in the same order.If you want to learn more about the available events see ActionBase.
self.set_bottom_label()
will change the bottom label on the key. To learn more about the available methods see ActionBase.
self.backend.get_number()
calls get_number()
on the launched backend.