Defining custom configuration¶
Any code you write using Reahl is packaged as a component even your web application itself.
Each Reahl component used by an application can have its own configuration file located in a common configuration directory.
The tutorial.componentconfigbootstrap example shows how to make a component read configuration from such a file. The example uses its own config setting (componentconfig.showheader) to control whether AddressBookPanel has a heading or not.
class AddressBookPanel(Div): def __init__(self, view): super().__init__(view) config = ExecutionContext.get_context().config if config.componentconfig.showheader: self.add_child(H(view, 1, text='Addresses')) for address in Session.query(Address).all(): self.add_child(AddressBox(view, address)) self.add_child(AddAddressForm(view))
AddressConfig (which inherits from
Configuration) defines the
available configuration settings and associated information. Declare
each config setting by assigning an instance of
ConfigSetting to a
class attribute of AddressConfig with the name you want:
class AddressConfig(Configuration): filename = 'componentconfig.config.py' config_key = 'componentconfig' showheader = ConfigSetting(default=False, description='Whether the title should be shown as a heading too')
It is good practice to always provide a default value for a setting. If all your settings have sensible defaults, your application can be started up and run without any config file present at all–something a new user will appreciate. Similarly, the description is used to enable useful output when you run, for example:
reahl listconfig -i etc/
In the setup.cfg file, AddressConfig is added as the “configuration” of the component:
component = configuration = "reahl.doc.examples.tutorial.componentconfigbootstrap.componentconfigbootstrap:AddressConfig"
componentconfig.showheader = True