Render an object in to an iterable of `Segment` instances. This method contains the logic for rendering objects with the console protocol. You are unlikely to need to use it directly, unless you are extending the library. Args: renderable (RenderableType): An ob
(
self, renderable: RenderableType, options: Optional[ConsoleOptions] = None
)
| 1292 | return measurement |
| 1293 | |
| 1294 | def render( |
| 1295 | self, renderable: RenderableType, options: Optional[ConsoleOptions] = None |
| 1296 | ) -> Iterable[Segment]: |
| 1297 | """Render an object in to an iterable of `Segment` instances. |
| 1298 | |
| 1299 | This method contains the logic for rendering objects with the console protocol. |
| 1300 | You are unlikely to need to use it directly, unless you are extending the library. |
| 1301 | |
| 1302 | Args: |
| 1303 | renderable (RenderableType): An object supporting the console protocol, or |
| 1304 | an object that may be converted to a string. |
| 1305 | options (ConsoleOptions, optional): An options object, or None to use self.options. Defaults to None. |
| 1306 | |
| 1307 | Returns: |
| 1308 | Iterable[Segment]: An iterable of segments that may be rendered. |
| 1309 | """ |
| 1310 | |
| 1311 | _options = options or self.options |
| 1312 | if _options.max_width < 1: |
| 1313 | # No space to render anything. This prevents potential recursion errors. |
| 1314 | return |
| 1315 | render_iterable: RenderResult |
| 1316 | |
| 1317 | renderable = rich_cast(renderable) |
| 1318 | if hasattr(renderable, "__rich_console__") and not isinstance(renderable, type): |
| 1319 | render_iterable = renderable.__rich_console__(self, _options) |
| 1320 | elif isinstance(renderable, str): |
| 1321 | text_renderable = self.render_str( |
| 1322 | renderable, highlight=_options.highlight, markup=_options.markup |
| 1323 | ) |
| 1324 | render_iterable = text_renderable.__rich_console__(self, _options) |
| 1325 | else: |
| 1326 | raise errors.NotRenderableError( |
| 1327 | f"Unable to render {renderable!r}; " |
| 1328 | "A str, Segment or object with __rich_console__ method is required" |
| 1329 | ) |
| 1330 | |
| 1331 | try: |
| 1332 | iter_render = iter(render_iterable) |
| 1333 | except TypeError: |
| 1334 | raise errors.NotRenderableError( |
| 1335 | f"object {render_iterable!r} is not renderable" |
| 1336 | ) |
| 1337 | _Segment = Segment |
| 1338 | _options = _options.reset_height() |
| 1339 | for render_output in iter_render: |
| 1340 | if isinstance(render_output, _Segment): |
| 1341 | yield render_output |
| 1342 | else: |
| 1343 | yield from self.render(render_output, _options) |
| 1344 | |
| 1345 | def render_lines( |
| 1346 | self, |