pg.View¶
Accessible via pg.View, pg.views.View.
- class View(**kwargs)[source]¶
Bases:
objectBase class for views.
A view defines an unique way/format of rendering an object (e.g. int, str, list, dict, user-defined object). For example, pg.HtmlView is a view that renders an object into HTML. pg.HtmlTreeViews is a concrete pg.HtmlView that renders an object into a HTML tree view.
Classes:
Extension for the View class.
Methods:
create(view_id, **kwargs)Creates a View instance with the given view ID.
dir()Returns all registered View classes with their view IDs.
extension_method(method_name)Decorator that dispatches a View method to a View.Extension method.
render(value, *[, name, root_path])Renders the input value.
- class Extension[source]¶
Bases:
objectExtension for the View class.
View developers should always create a corresponding
Extensionclass as an inner class of theViewclass. TheExtensionclass defines custom rendering logic for user-defined types and provides methods that can be bound to theViewmethods via the@pg.View.extension_methoddecorator.Example:
class MyView(pg.View): VIEW_TYPE = 'my_view' class Extension(pg.View.Extension): def _my_view_render(self, value, *, view, **kwargs): return view.render(value, **kwargs) def _my_view_title(self, value, *, view, **kwargs): return view.render_title(value, **kwargs) @pg.View.extension_method('_my_view_title') def title(self, value: Any, **kwargs): return pg.Html('<h1>' + str(value) + '</h1>') @pg.View.extension_method('_my_view_render') def render(self, value: Any, **kwargs): return pg.Html(str(value))
To use the
Extensionclass, users can subclass it and override the extension methods in their own classes.For example:
class MyObject(MyView.Extension): def _my_view_title(self, value, *, view, **kwargs): return pg.Html('Custom title for ' + str(value) + '</h1>') def _my_view_render(self, value, *, view, **kwargs): return self._my_view_title(value, **kwargs) + pg.Html(str(value))
In this example,
MyObjectsubclasses theExtensionclass ofMyViewand overrides the_my_view_titleand_my_view_rendermethods to provide custom view rendering for the object.Methods:
supported_view_classes(cls)Returns all non-abstract View classes that the current class supports.
- classmethod supported_view_classes(cls)¶
Returns all non-abstract View classes that the current class supports.
A class can inherit from multiple
View.Extensionclasses. For example:class MyObject(View1.Extension, View2.Extension): ...
In this case,
MyObjectsupports bothView1andView2.
- static create(view_id, **kwargs)[source]¶
Creates a View instance with the given view ID.
- Return type:
- classmethod extension_method(method_name)[source]¶
Decorator that dispatches a View method to a View.Extension method.
- Return type:
A few things to note: 1) The View method being decorated must have a value argument, based on
which the Extension method will be dispatched.
The View method’s value argument will map to the Extension method’s self argument.
The Extension method can optionally have a view argument, which will be set to the current View class.
- Parameters:
method_name – The name of the method in the Extension class to dispatch from current View method.
- Returns:
A decorator that dispatches a View method to a View.Extension method.
- abstract render(value, *, name=None, root_path=None, **kwargs)[source]¶
Renders the input value.
- Return type:
Content- Parameters:
value – The value to render.
name – (Optional) The referred name of the value from its container.
root_path – (Optional) The path of value under its object tree.
**kwargs – Additional keyword arguments passed from pg.view or wrapper functions (e.g. pg.to_html).
- Returns:
The rendered content.