zope-viewlet

raw JSON →
6.0 verified Mon Apr 27 auth: no python

Zope Viewlets provide a mechanism for reusable components in Zope-based web applications, enabling modular view rendering. Current version: 6.0, supported Python >=3.9. Release cadence is infrequent, tied to Zope framework updates.

pip install zope.viewlet
error AttributeError: module 'zope.viewlet' has no attribute 'ViewletBase'
cause Importing from the package root instead of the viewlet submodule.
fix
Use: from zope.viewlet.viewlet import ViewletBase
error TypeError: can't pickle ViewletManagerBase objects
cause Viewlet managers are not pickle-safe by default; common when using session or caching.
fix
Implement __getstate__ and __setstate__ in your manager class, or avoid pickling them.
error ModuleNotFoundError: No module named 'zope.viewlet'
cause Package not installed or Python environment not activated.
fix
Install: pip install zope.viewlet. Verify with pip list.
breaking 6.0 drops support for Python 2 and Python <3.9. If upgrading from 5.x, check Python version compatibility.
fix Ensure Python >=3.9. Update imports and remove any Python 2 compatibility code.
deprecated ViewletManagerBase no longer automatically assigns 'manager' attribute to viewlets. You must now pass it explicitly via update().
fix Override update() to set self.manager = manager or receive manager as argument.
gotcha Importing directly from zope.viewlet (e.g., 'from zope.viewlet import ViewletBase') fails with AttributeError because submodules are not imported by default.
fix Use the appropriate submodule path: from zope.viewlet.viewlet import ViewletBase, from zope.viewlet.manager import ViewletManagerBase, from zope.viewlet.interfaces import IViewlet, etc.

Define a viewlet and manager. Viewlets must implement IViewlet, managers IViewletManager.

from zope.viewlet.viewlet import ViewletBase
from zope.viewlet.manager import ViewletManagerBase
from zope.viewlet.interfaces import IViewlet, IViewletManager

class MyViewlet(ViewletBase):
    def update(self):
        self.message = 'Hello from viewlet'
    def render(self):
        return f'<div>{self.message}</div>'

class MyManager(ViewletManagerBase):
    pass

# typical usage involves registration via ZCML or component architecture
print('Viewlet and Manager defined')