Adding Rich Content to Tabs
Flyte reports allow you to embed rich HTML content, such as data tables and visualizations, into specific tabs. By using the Tab class, you can organize complex task outputs into a structured, multi-tab interface.
Adding Content to Specific Tabs
To add content to a specific tab, use flyte.report.get_tab() to retrieve a Tab instance by name, then use its log() or replace() methods.
from flyte import report
# Get or create a specific tab
summary_tab = report.get_tab("Summary")
# Add HTML content to the tab
summary_tab.log("<h2>Execution Summary</h2>")
summary_tab.log("<p>Task completed successfully.</p>")
# Replace all content in the tab
summary_tab.replace("<h1>New Summary</h1>")
The Tab class (defined in report/_report.py) manages a list of HTML strings. When log() is called, it appends the string to the content list. When replace() is called, it clears the list and adds the new string.
Embedding DataFrames
You can embed Pandas DataFrames by converting them to HTML strings. This is useful for displaying tabular data directly in the Flyte console.
import pandas as pd
from flyte import report
def log_dataframe(df: pd.DataFrame):
# Get the data tab
data_tab = report.get_tab("Data View")
# Convert DataFrame to HTML and log it
html_table = df.to_html(classes="table table-striped")
data_tab.log(f"<h3>Dataset Preview</h3>{html_table}")
Embedding Visualizations
Visualizations from libraries like Plotly or Matplotlib can be embedded by exporting them as HTML or SVG strings.
import plotly.express as px
from flyte import report
def log_plot(df: pd.DataFrame):
fig = px.scatter(df, x="x", y="y", title="Results")
# Get the visualization tab
viz_tab = report.get_tab("Visualizations")
# Export Plotly figure to HTML (div only)
plot_html = fig.to_html(full_html=False, include_plotlyjs='cdn')
viz_tab.log(plot_html)
Managing Tab Content
The Tab class provides three primary methods for content management:
log(content: str): Appends a valid HTML string to the tab. The string should not be a complete HTML document (no<html>or<body>tags), as it is inserted into a<div>in the final report.replace(content: str): Clears all existing content in the tab and replaces it with the provided HTML string.get_html() -> str: Returns the concatenated HTML content of the tab, separated by newlines. This is used internally byReport.get_final_report()to build the final UI.
Important Considerations
- HTML Safety: The
Reportclass (inreport/_report.py) does not escape the content added to tabs. It assumes the provided strings are safe, valid HTML. - Initialization: You should not instantiate
Tabdirectly. Always useflyte.report.get_tab(name)to ensure the tab is correctly registered with the currentReportcontext. - Main Tab: By default,
flyte.report.log()andflyte.report.replace()operate on a tab named"main". Usingget_tab()allows you to create additional tabs alongside the default one.