in databao/core/executor.py [0:0]
def _to_html(self, *, plot_mimebundle: dict[str, Any] | None = None) -> str:
import html
modality_hints = self.meta.get(OutputModalityHints.META_KEY, OutputModalityHints())
html_parts = {}
text_html = f"<pre>{html.escape(self.text.strip())}</pre>" # TODO markdown to HTML
html_parts["text"] = text_html
if self.code is not None:
code = self.code.strip()
if len(code) > 0:
code_html = f"<pre><code>{html.escape(code)}</code></pre>"
html_parts["code"] = code_html
if self.df is not None:
html_parts["df"] = self._dataframe_to_html(self.df)
if modality_hints.should_visualize and plot_mimebundle is not None:
vis_html: str | None = None
if (plot_html := plot_mimebundle.get("text/html")) is not None:
vis_html = plot_html
elif (png_bytes := plot_mimebundle.get("image/png")) is not None:
png_base64 = base64.b64encode(png_bytes).decode("utf-8")
vis_html = f'<img src="data:image/png;base64,{png_base64}" alt="Plot"/>'
elif (jpeg_bytes := plot_mimebundle.get("image/jpeg")) is not None:
jpeg_base64 = base64.b64encode(jpeg_bytes).decode("utf-8")
vis_html = f'<img src="data:image/jpeg;base64,{jpeg_base64}" alt="Plot"/>'
if vis_html is not None:
html_parts["visualization"] = vis_html
# Determine which section should be expanded by default
expand_keys = ["visualization"] if "visualization" in html_parts else ["df", "text"]
section_names = {"text": "Response", "df": "Data", "visualization": "Visualization", "code": "Code"}
html_parts = {
k: f"<details{' open' if k in expand_keys else ''}><summary>{section_names[k]}</summary>{v}</details>"
for k, v in html_parts.items()
}
html_code = "\n\n".join(html_parts.values())
return self._postprocess_html(html_code)