pg.View

Accessible via pg.View, pg.views.View.

class View(**kwargs)[source]

Bases: object

Base 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()

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: object

Extension for the View class.

View developers should always create a corresponding Extension class as an inner class of the View class. The Extension class defines custom rendering logic for user-defined types and provides methods that can be bound to the View methods via the @pg.View.extension_method decorator.

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 Extension class, 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, MyObject subclasses the Extension class of MyView and overrides the _my_view_title and _my_view_render methods 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.Extension classes. For example:

class MyObject(View1.Extension, View2.Extension):
  ...

In this case, MyObject supports both View1 and View2.

Return type:

Set[Type[pg.View]]

Returns:

All non-abstract View classes that the current class supports.

static create(view_id, **kwargs)[source]

Creates a View instance with the given view ID.

Return type:

pg.View

classmethod dir()[source]

Returns all registered View classes with their view IDs.

Return type:

Dict[str, Type[pg.View]]

classmethod extension_method(method_name)[source]

Decorator that dispatches a View method to a View.Extension method.

Return type:

Any

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.

  1. The View method’s value argument will map to the Extension method’s self argument.

  2. 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.