esparto.design.adaptors
Info
The content_adaptor
function is called internally when an explicit Content
class is not provided.
Objects are matched to a suitable Content
class through single dispatch.
import esparto as es
# Text automatically converted to Markdown content.
page = es.Page(title="New Page")
page["New Section"] = "Example _markdown_ text."
page.tree()
{'New Page': [{'New Section': [{'Row 0': [{'Column 0': ['Markdown']}]}]}]}
adaptors
content_adaptor(content)
Wrap content in the required class. If Layout object is passed, return unchanged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
Content |
Any content to be added to the document. |
required |
Returns:
Type | Description |
---|---|
Union[esparto.design.content.Content, esparto.design.layout.Layout, Dict[str, Any]] |
Content: Appropriately wrapped content. |
Source code in esparto/design/adaptors.py
@ft.singledispatch
def content_adaptor(content: Content) -> Union[Content, Layout, Dict[str, Any]]:
"""
Wrap content in the required class. If Layout object is passed, return unchanged.
Args:
content (Any): Any content to be added to the document.
Returns:
Content: Appropriately wrapped content.
"""
if not issubclass(type(content), Content):
raise TypeError(f"Unsupported content type: {type(content)}")
return content
content_adaptor_bokeh(content)
Convert Bokeh Layout to FigureBokeh content.
Source code in esparto/design/adaptors.py
@content_adaptor.register(BokehObject)
def content_adaptor_bokeh(content: BokehObject) -> FigureBokeh:
"""Convert Bokeh Layout to FigureBokeh content."""
return FigureBokeh(content)
content_adaptor_core(content)
Convert text or image to Markdown or Image content.
Source code in esparto/design/adaptors.py
@content_adaptor.register(str)
@content_adaptor.register(Path)
def content_adaptor_core(content: Union[str, Path]) -> Content:
"""Convert text or image to Markdown or Image content."""
content = str(content)
guess = mt.guess_type(content)
if guess and isinstance(guess[0], str):
file_type = guess[0].split("/")[0]
if file_type == "image":
return Image(content)
elif file_type == "text":
content = Path(content).read_text()
else:
raise TypeError(f"{content}: {file_type}")
return Markdown(content)
content_adaptor_df(content)
Convert Pandas DataFrame to DataFramePD content.
Source code in esparto/design/adaptors.py
@content_adaptor.register(DataFrame)
def content_adaptor_df(content: DataFrame) -> DataFramePd:
"""Convert Pandas DataFrame to DataFramePD content."""
return DataFramePd(content)
content_adaptor_dict(content)
Pass through dict of {"title": content}
.
Source code in esparto/design/adaptors.py
@content_adaptor.register(dict)
def content_adaptor_dict(content: Dict[str, Any]) -> Dict[str, Any]:
"""Pass through dict of `{"title": content}`."""
if not (len(content) == 1 and isinstance(list(content.keys())[0], str)):
raise ValueError("Content dict must be passed as {'title': content}")
return content
content_adaptor_layout(content)
If Layout object is passed, return unchanged.
Source code in esparto/design/adaptors.py
@content_adaptor.register(Layout)
def content_adaptor_layout(content: Layout) -> Layout:
"""If Layout object is passed, return unchanged."""
return content
content_adaptor_mpl(content)
Convert Matplotlib Figure to FigureMpl content.
Source code in esparto/design/adaptors.py
@content_adaptor.register(Figure)
def content_adaptor_mpl(content: Figure) -> FigureMpl:
"""Convert Matplotlib Figure to FigureMpl content."""
return FigureMpl(content)
content_adaptor_plotly(content)
Convert Plotly Figure to FigurePlotly content.
Source code in esparto/design/adaptors.py
@content_adaptor.register(PlotlyFigure)
def content_adaptor_plotly(content: PlotlyFigure) -> FigurePlotly:
"""Convert Plotly Figure to FigurePlotly content."""
return FigurePlotly(content)