API
Layout
- class modern_urwid.layout.Layout(xml_path: ~pathlib.Path, css_path: ~pathlib.Path | None = None, resources_cls=<class 'modern_urwid.layout.LayoutResourceHandler'>, xml_dir=None, css_dir=None)
Bases:
objectCreate a UI layout from XML and CSS.
This class is responsible for parsing XML and applying CSS styles to the created widgets. The root widget can be referenced with
get_root().- get_palettes()
Get all palettes used in this layout.
- get_root() Widget
Get the root XML widget that can be rendered.
- load()
Parse the XML and CSS. Make sure to call
register_widgets()first if neccessary.
- register_widgets(widgets: list[type[WidgetBuilder]])
Add custom widget builders for the XML parser. Note: do this before calling
load().
- style_widget(widget: Widget, classes=[], id=None) AttrMap
Style a widget according to any given classes or id it may have.
- class modern_urwid.layout.LayoutResourceHandler(layout: Layout, palettes=[], widgets: list[type[WidgetBuilder]] = [], css_variables: dict[str, str] = {})
Bases:
ResourceHandlerA base class for extending a layout’s functionality.
Reference properties from the base class (e.g. callbacks) with the
@prefix. Reference properties from the data dictionary with brackets surrounding the key (e.g."{user.name}").- get_css_variables() dict[str, str]
Get custom CSS variables, used to override stylesheet variables.
- get_palettes()
Get custom palettes.
- get_resource(name)
Get a custom resource (typically referenced by
@ResourceNamein XML).
- get_widget_builder(tag: str) type[WidgetBuilder] | None
Get a widget builder for a custom widget tag.
- on_load()
Called when loading the parent layout in
register().
- parse_resources_tag(element: Element)
Parse the
<mu:resources />tag
- parse_string_template(template)
Parse a given string template, e.g.
"User: {user.id}"
Layout Manager
- class modern_urwid.layout_manager.LayoutManager(loop: MainLoop | None = None)
Bases:
objectManages multiple layouts and shared custom widgets and palettes between them.
- get_loop() MainLoop
Get the urwid
MainLoop
- register_palette(palette)
Register a set of palette entries in the urwid
MainLoop.
- register_widget(cls: type[WidgetBuilder] | None = None) LambdaType
Register a custom widget builder.
This can be used either as a decorator (
@manager.register_widget()) or by directly passing a class (manager.register_widget(MyCustomBuilder)).
- run()
Run the urwid
MainLoop
- switch(name: str)
Switch to a different layout by name.
Calls the new layout’s
on_enter()method, and the old layout’son_exit()method.
Widget Builder
- class modern_urwid.builder.WidgetBuilder(element: Element, children: list[Widget | WidgetContainerMixin] = [])
Bases:
objectUtility class used to build custom widgets. Widgets can be made with Python or XML. Use the
LayoutManager.register_widget()decorator to register.- build(**kwargs) Widget
Build the widget. When overriding, use the
self.manager,self.element,self.childrenif applicable.
- render_from_xml(xml_path: ~pathlib.Path, resource_handler=<modern_urwid.resource_handler.ResourceHandler object>, css_path: ~pathlib.Path | None = None) XMLParser
Render a widget from XML. Note:
XMLParser.styleswill need to be registered in urwid palettes.
Constants
This module contains all of the constants used in modern_urwid.
Module variables:
XML_NS- XML namespace for modern-urwidRESOURCE_CHAR- The character used to reference resources from ResourceHandlerDEFAULT_STYLE- The default style for widgets