Coverage for src/python/ensembl/vep/render/report.py: 100%

60 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-18 14:41 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14"""A report: titled sections of tables and charts, rendered to one of three formats. 

15 

16The one place a run summary, a fact profile, or any later report becomes HTML, text or JSON. A 

17producer describes *what* it has -- headings, rows, labelled quantities -- and never how it should 

18look, which is what lets it live outside this package without a renderer of its own. 

19""" 

20 

21from __future__ import annotations 

22 

23import json 

24from collections.abc import Sequence 

25from enum import Enum 

26 

27from attrs import field, frozen 

28 

29from ensembl.vep.render.chart import Slice, render_bars, render_legend, render_pie 

30from ensembl.vep.render.page import Section, render_page 

31from ensembl.vep.render.table import TableFormat, render_table 

32 

33__all__ = ["ChartKind", "ReportFormat", "ReportSection", "render_report"] 

34 

35 

36class ReportFormat(Enum): 

37 """The formats a report can be rendered in.""" 

38 

39 HTML = "html" 

40 TEXT = "text" 

41 JSON = "json" 

42 

43 

44class ChartKind(Enum): 

45 """How a section's quantities are drawn, if at all.""" 

46 

47 NONE = "none" 

48 PIE = "pie" 

49 BARS = "bars" 

50 

51 

52@frozen(kw_only=True, auto_attribs=True) 

53class ReportSection: 

54 """One block of a report: a table, a chart, or both. 

55 

56 Attributes: 

57 identifier (str): The anchor, and what a page's side menu links to. 

58 title (str): The heading. 

59 headers (tuple[str, ...]): Column headings of the table, if any. 

60 rows (tuple[tuple[object, ...], ...]): The table's rows. 

61 slices (tuple[Slice, ...]): Labelled quantities, drawn as ``chart`` and, in the text 

62 formats, listed as a two-column table. 

63 chart (ChartKind): How the slices are drawn in HTML. 

64 """ 

65 

66 identifier: str 

67 title: str 

68 headers: tuple[str, ...] = field(default=()) 

69 rows: tuple[tuple[object, ...], ...] = field(default=()) 

70 slices: tuple[Slice, ...] = field(default=()) 

71 chart: ChartKind = field(default=ChartKind.NONE) 

72 

73 

74SLICE_HEADERS = ("Label", "Count") 

75 

76 

77def _html_blocks(section: ReportSection) -> tuple[str, ...]: 

78 """Return a section's table and chart as HTML fragments.""" 

79 blocks = [] 

80 if section.rows or section.headers: 

81 blocks.append(render_table(section.headers, section.rows, TableFormat.HTML)) 

82 if section.slices and section.chart is ChartKind.PIE: 

83 blocks.append(render_pie(section.slices, section.title)) 

84 blocks.append(render_legend(section.slices)) 

85 elif section.slices: 

86 blocks.append(render_bars(section.slices, section.title)) 

87 return tuple(blocks) 

88 

89 

90def _text_blocks(section: ReportSection) -> list[str]: 

91 """Return a section's heading and its tables as text.""" 

92 blocks = [f"\n## {section.title}\n"] 

93 if section.rows or section.headers: 

94 blocks.append(render_table(section.headers, section.rows, TableFormat.TEXT)) 

95 if section.slices: 

96 rows = [(item.label, item.value) for item in section.slices] 

97 blocks.append(render_table(SLICE_HEADERS, rows, TableFormat.TEXT)) 

98 return blocks 

99 

100 

101def _json_section(section: ReportSection) -> dict[str, object]: 

102 """Return a section as plain JSON-ready data.""" 

103 return { 

104 "identifier": section.identifier, 

105 "title": section.title, 

106 "headers": list(section.headers), 

107 "rows": [list(row) for row in section.rows], 

108 "slices": [{"label": item.label, "value": item.value} for item in section.slices], 

109 } 

110 

111 

112def render_report( 

113 title: str, subtitle: str, sections: Sequence[ReportSection], report_format: str | ReportFormat 

114) -> str: 

115 """Render a report in one of the published formats. 

116 

117 Args: 

118 title (str): The document title. 

119 subtitle (str): One line under it. 

120 sections (Sequence[ReportSection]): The blocks, in order. 

121 report_format (str | ReportFormat): Which format to render. 

122 

123 Returns: 

124 str: The rendered report. 

125 """ 

126 chosen = ReportFormat(report_format) 

127 if chosen is ReportFormat.HTML: 

128 page_sections = [] 

129 for section in sections: 

130 page_sections.append( 

131 Section(identifier=section.identifier, title=section.title, blocks=_html_blocks(section)) 

132 ) 

133 return render_page(title, page_sections, subtitle) 

134 if chosen is ReportFormat.JSON: 

135 payload = { 

136 "title": title, 

137 "subtitle": subtitle, 

138 "sections": [_json_section(section) for section in sections], 

139 } 

140 return json.dumps(payload, indent=2, default=str) 

141 

142 blocks = [f"# {title}", subtitle] 

143 for section in sections: 

144 blocks.extend(_text_blocks(section)) 

145 return "\n".join(blocks)