MCPcopy
hub / github.com/Textualize/rich / export_html

Method export_html

rich/console.py:2244–2319  ·  view source on GitHub ↗

Generate HTML from console contents (requires record=True argument in constructor). Args: theme (TerminalTheme, optional): TerminalTheme object containing console colors. clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``. c

(
        self,
        *,
        theme: Optional[TerminalTheme] = None,
        clear: bool = True,
        code_format: Optional[str] = None,
        inline_styles: bool = False,
    )

Source from the content-addressed store, hash-verified

2242 write_file.write(text)
2243
2244 def export_html(
2245 self,
2246 *,
2247 theme: Optional[TerminalTheme] = None,
2248 clear: bool = True,
2249 code_format: Optional[str] = None,
2250 inline_styles: bool = False,
2251 ) -> str:
2252 """Generate HTML from console contents (requires record=True argument in constructor).
2253
2254 Args:
2255 theme (TerminalTheme, optional): TerminalTheme object containing console colors.
2256 clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``.
2257 code_format (str, optional): Format string to render HTML. In addition to '{foreground}',
2258 '{background}', and '{code}', should contain '{stylesheet}' if inline_styles is ``False``.
2259 inline_styles (bool, optional): If ``True`` styles will be inlined in to spans, which makes files
2260 larger but easier to cut and paste markup. If ``False``, styles will be embedded in a style tag.
2261 Defaults to False.
2262
2263 Returns:
2264 str: String containing console contents as HTML.
2265 """
2266 from html import escape
2267
2268 assert (
2269 self.record
2270 ), "To export console contents set record=True in the constructor or instance"
2271 fragments: List[str] = []
2272 append = fragments.append
2273 _theme = theme or DEFAULT_TERMINAL_THEME
2274 stylesheet = ""
2275
2276 render_code_format = CONSOLE_HTML_FORMAT if code_format is None else code_format
2277
2278 with self._record_buffer_lock:
2279 if inline_styles:
2280 for text, style, _ in Segment.filter_control(
2281 Segment.simplify(self._record_buffer)
2282 ):
2283 text = escape(text)
2284 if style:
2285 rule = style.get_html_style(_theme)
2286 if style.link:
2287 text = f'<a href="{style.link}">{text}</a>'
2288 text = f'<span style="{rule}">{text}</span>' if rule else text
2289 append(text)
2290 else:
2291 styles: Dict[str, int] = {}
2292 for text, style, _ in Segment.filter_control(
2293 Segment.simplify(self._record_buffer)
2294 ):
2295 text = escape(text)
2296 if style:
2297 rule = style.get_html_style(_theme)
2298 style_number = styles.setdefault(rule, len(styles) + 1)
2299 if style.link:
2300 text = f'<a class="r{style_number}" href="{style.link}">{text}</a>'
2301 else:

Callers 5

test_export_htmlFunction · 0.95
test_export_html_inlineFunction · 0.95
save_htmlMethod · 0.95
export.pyFile · 0.80
default_styles.pyFile · 0.80

Calls 5

filter_controlMethod · 0.80
simplifyMethod · 0.80
get_html_styleMethod · 0.80
joinMethod · 0.80
escapeFunction · 0.70

Tested by 2

test_export_htmlFunction · 0.76
test_export_html_inlineFunction · 0.76