Get a visual instance from an object. If the object does not support the Visual protocol and is a Rich renderable, it will be wrapped in a [RichVisual][textual.visual.RichVisual]. Args: widget: The parent widget. obj: An object. markup: Enable markup. Raise
(widget: Widget, obj: object, markup: bool = True)
| 73 | |
| 74 | |
| 75 | def visualize(widget: Widget, obj: object, markup: bool = True) -> Visual: |
| 76 | """Get a visual instance from an object. |
| 77 | |
| 78 | If the object does not support the Visual protocol and is a Rich renderable, it |
| 79 | will be wrapped in a [RichVisual][textual.visual.RichVisual]. |
| 80 | |
| 81 | Args: |
| 82 | widget: The parent widget. |
| 83 | obj: An object. |
| 84 | markup: Enable markup. |
| 85 | |
| 86 | Raises: |
| 87 | VisualError: If there is no Visual could be returned to render `obj`. |
| 88 | |
| 89 | Returns: |
| 90 | A Visual instance to render the object, or `None` if there is no associated visual. |
| 91 | """ |
| 92 | _rich_traceback_omit = True |
| 93 | if isinstance(obj, Visual): |
| 94 | # Already a visual |
| 95 | return obj |
| 96 | # The visualize method should return a Visual if present. |
| 97 | visualize = getattr(obj, "visualize", None) |
| 98 | if visualize is None: |
| 99 | # Doesn't expose the textualize protocol |
| 100 | from textual.content import Content |
| 101 | |
| 102 | if isinstance(obj, str): |
| 103 | return Content.from_markup(obj) if markup else Content(obj) |
| 104 | |
| 105 | if is_renderable(obj): |
| 106 | if isinstance(obj, Text): |
| 107 | return Content.from_rich_text(obj, console=widget.app.console) |
| 108 | |
| 109 | # If its is a Rich renderable, wrap it with a RichVisual |
| 110 | return RichVisual(widget, rich_cast(obj)) |
| 111 | else: |
| 112 | # We don't know how to make a visual from this object |
| 113 | raise VisualError( |
| 114 | f"unable to display {obj.__class__.__name__!r} type; must be a str, Rich renderable, or Textual Visual object" |
| 115 | ) |
| 116 | # Call the textualize method to create a visual |
| 117 | visual = visualize() |
| 118 | if not isinstance(visual, Visual) and is_renderable(visual): |
| 119 | return RichVisual(widget, visual) |
| 120 | return visual |
| 121 | |
| 122 | |
| 123 | class Visual(ABC): |
no test coverage detected
searching dependent graphs…