Coverage for src/python/ensembl/vep/render/page.py: 100%
32 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« 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 self-contained HTML page.
16Mirrors the information architecture of VEP's summary page -- a masthead, a fixed side menu of
17anchors, then one section per topic -- with the Ensembl skin and no external requests. Everything
18the page needs is inlined, so it opens from a filesystem with no network.
19"""
21from __future__ import annotations
23from collections.abc import Sequence
24from xml.sax.saxutils import escape
26from attrs import define, field
28from ensembl.vep.render.table import TABLE_STYLES
29from ensembl.vep.render.theme import Colour, Metric, stylesheet
31__all__ = ["Section", "render_page"]
33LAYOUT_STYLES = f"""
34.vep-masthead {{
35 border-bottom: 1px solid {Colour.MEDIUM_LIGHT_GREY.var};
36 padding: {Metric.STANDARD_GUTTER.value};
37}}
39.vep-masthead h1 {{
40 margin: 0;
41 font-size: 20px;
42 font-weight: {Metric.FONT_WEIGHT_LIGHT.value};
43}}
45.vep-masthead p {{
46 margin: 4px 0 0;
47 color: {Colour.DARK_GREY.var};
48}}
50.vep-layout {{
51 display: flex;
52 align-items: flex-start;
53 gap: {Metric.STANDARD_GUTTER.value};
54 padding: {Metric.STANDARD_GUTTER.value};
55}}
57.vep-menu {{
58 position: sticky;
59 top: {Metric.STANDARD_GUTTER.value};
60 min-width: 200px;
61 max-height: calc(100vh - 2 * {Metric.STANDARD_GUTTER.value});
62 overflow-y: auto;
63 border-left: 2px solid {Colour.BLUE.var};
64 padding-left: 12px;
65}}
67.vep-menu ul {{
68 list-style: none;
69 margin: 0;
70 padding: 0;
71}}
73.vep-menu li {{
74 padding: 3px 0;
75}}
77.vep-menu .vep-menu-group {{
78 margin-top: 12px;
79 color: {Colour.SOFT_BLACK.var};
80 font-weight: {Metric.FONT_WEIGHT_BOLD.value};
81}}
83.vep-menu .vep-menu-group:first-child {{
84 margin-top: 0;
85}}
87.vep-content {{
88 flex: 1;
89 min-width: 0;
90}}
92.vep-section {{
93 margin-bottom: {Metric.STANDARD_GUTTER.value};
94}}
96.vep-section h2 {{
97 font-size: 15px;
98 font-weight: {Metric.FONT_WEIGHT_NORMAL.value};
99 color: {Colour.SOFT_BLACK.var};
100 margin: 0 0 12px;
101 padding-bottom: 4px;
102 box-shadow: inset 0 -1.5px 0 {Colour.BLUE.var};
103}}
105.vep-figures {{
106 display: flex;
107 flex-wrap: wrap;
108 align-items: flex-start;
109 gap: {Metric.STANDARD_GUTTER.value};
110}}
112.vep-figures > svg {{
113 max-width: 100%;
114 height: auto;
115}}
116"""
119@define(frozen=True, kw_only=True, auto_attribs=True)
120class Section:
121 """One anchored block of the page.
123 Attributes:
124 identifier (str): The anchor, and what the side menu links to.
125 title (str): The heading, and the menu entry.
126 blocks (tuple[str, ...]): Ready-made HTML fragments -- a table, a chart, a graph -- laid
127 out side by side.
128 group (str): Optional heading grouping consecutive entries in the side menu.
129 """
131 identifier: str
132 title: str
133 blocks: tuple[str, ...] = field(default=())
134 group: str = field(default="")
137def _menu(sections: Sequence[Section]) -> str:
138 """Return the side menu linking to each section."""
139 entries = []
140 previous_group = ""
141 for section in sections:
142 if section.group and section.group != previous_group:
143 entries.append(f'<li class="vep-menu-group">{escape(section.group)}</li>')
144 entries.append(f'<li><a href="#{escape(section.identifier)}">{escape(section.title)}</a></li>')
145 previous_group = section.group
146 listing = "\n ".join(entries)
147 return f'<nav class="vep-menu">\n <ul>\n {listing}\n </ul>\n </nav>'
150def _section(section: Section) -> str:
151 """Return one section as HTML."""
152 figures = "\n ".join(section.blocks)
153 return (
154 f'<section class="vep-section" id="{escape(section.identifier)}">\n'
155 f" <h2>{escape(section.title)}</h2>\n"
156 f' <div class="vep-figures">\n {figures}\n </div>\n'
157 f" </section>"
158 )
161def render_page(title: str, sections: Sequence[Section], subtitle: str = "") -> str:
162 """Return a complete HTML document.
164 Args:
165 title (str): The document title and masthead heading.
166 sections (Sequence[Section]): The blocks to show, in order.
167 subtitle (str): Optional line under the heading, e.g. the run that produced the page.
169 Returns:
170 str: A standalone document that makes no external requests.
171 """
172 styles = stylesheet() + TABLE_STYLES + LAYOUT_STYLES
173 rendered_sections = "\n ".join(_section(section) for section in sections)
174 lead = f"<p>{escape(subtitle)}</p>" if subtitle else ""
175 return f"""<!DOCTYPE html>
176<html lang="en">
177<head>
178<meta charset="utf-8">
179<meta name="viewport" content="width=device-width, initial-scale=1">
180<title>{escape(title)}</title>
181<style>
182{styles}
183</style>
184</head>
185<body>
186 <header class="vep-masthead">
187 <h1>{escape(title)}</h1>
188 {lead}
189 </header>
190 <div class="vep-layout">
191 {_menu(sections)}
192 <main class="vep-content">
193 {rendered_sections}
194 </main>
195 </div>
196</body>
197</html>
198"""