Guess the template to use based on the blocks provided
(blocks: BaseElementList)
| 64 | |
| 65 | |
| 66 | def guess_template(blocks: BaseElementList) -> t.Type[IPythonTemplate]: |
| 67 | """Guess the template to use based on the blocks provided""" |
| 68 | app_template: t.Type[IPythonTemplate] |
| 69 | |
| 70 | # DashboardTemplate: Contains only Plot, BigNumber, and DataTable blocks |
| 71 | if all([isinstance(block, (b.Plot, b.BigNumber, b.DataTable)) for block in blocks]): |
| 72 | app_template = DashboardTemplate |
| 73 | # TitledPagesTemplate: Contains text blocks with at least two headings |
| 74 | elif ( |
| 75 | len( |
| 76 | filter_blocks_by_predicate( |
| 77 | blocks, lambda block: isinstance(block, b.Text) and block.content.startswith("# ") |
| 78 | ) |
| 79 | ) |
| 80 | >= 2 |
| 81 | ): |
| 82 | app_template = TitledPagesTemplate |
| 83 | # DescriptivePagesTemplate: Contains at least two text blocks that are followed by a different block type |
| 84 | elif "".join(["1" if isinstance(block, (b.Text)) else "0" for block in blocks]).count("10") >= 2: |
| 85 | app_template = DescriptivePagesTemplate |
| 86 | # AssetListTemplate: Does not contain any text or code blocks |
| 87 | elif not any(filter_blocks_by_types(blocks, (b.Text, b.Code))): |
| 88 | app_template = AssetListTemplate |
| 89 | # AssetCodeListTemplate: Does not contain any text blocks |
| 90 | elif not any(filter_blocks_by_types(blocks, b.Text)): |
| 91 | app_template = AssetCodeListTemplate |
| 92 | # ReportTemplate: The default template if we can't make a guess |
| 93 | else: |
| 94 | app_template = ReportTemplate |
| 95 | |
| 96 | display_msg( |
| 97 | f"Automatically selecting the `{app_template.name}` template. You can override this with `template=template_name` from the following templates: {', '.join(_registry.keys())}." |
| 98 | ) |
| 99 | |
| 100 | return app_template |
| 101 | |
| 102 | |
| 103 | class IPythonTemplate: |
nothing calls this directly
no test coverage detected