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

67 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"""Ensembl's design tokens. 

15 

16Transcribed from the frontend source of truth, ``Ensembl/ensembl-client`` 

17(``src/styles/design-tokens.css``), so refreshing the theme is a diff against one upstream file. 

18 

19The design system is light-only: there is no ``prefers-color-scheme`` block, no ``data-theme`` and 

20no dark token set anywhere upstream, so emitting one would invent a theme Ensembl does not have. 

21""" 

22 

23from __future__ import annotations 

24 

25from enum import Enum 

26 

27__all__ = ["Colour", "Font", "Metric", "stylesheet"] 

28 

29 

30def _custom_property(prefix: str, member_name: str) -> str: 

31 """Return the CSS custom property a token member is published as.""" 

32 return f"--{prefix}{member_name.lower().replace('_', '-')}" 

33 

34 

35class Colour(Enum): 

36 """The Ensembl palette, by its upstream token name.""" 

37 

38 BLACK = "#1b2c39" 

39 SOFT_BLACK = "#374148" 

40 WHITE = "#fff" 

41 OFF_WHITE = "#fefefe" 

42 

43 BLUE = "#0099ff" 

44 LIGHT_BLUE = "#33adff" 

45 ICE_BLUE = "#e5f5ff" 

46 TEAL = "#327c89" 

47 DUCKEGG_BLUE = "#96d0c9" 

48 NEON_BLUE = "#8ef4f8" 

49 

50 LIGHT_GREY = "#f1f2f4" 

51 MEDIUM_LIGHT_GREY = "#d4d9de" 

52 GREY = "#b7c0c8" 

53 MEDIUM_DARK_GREY = "#9aa7b1" 

54 DARK_GREY = "#6f8190" 

55 

56 RED = "#d90000" 

57 DARK_PINK = "#eb768a" 

58 PURPLE = "#ca81ff" 

59 ORANGE = "#ff9900" 

60 MUSTARD = "#cc9933" 

61 DARK_YELLOW = "#f8c041" 

62 GREEN = "#47d147" 

63 LIME = "#84fa3a" 

64 

65 @property 

66 def token(self) -> str: 

67 """Return the CSS custom property this colour is published as.""" 

68 return _custom_property("color-", self.name) 

69 

70 @property 

71 def var(self) -> str: 

72 """Return a ``var()`` reference to this colour, for use in a stylesheet.""" 

73 return f"var({self.token})" 

74 

75 

76class Font(Enum): 

77 """The two font stacks, with their upstream fallbacks.""" 

78 

79 BODY = "Lato, 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif" 

80 MONOSPACE = "'IBM Plex Mono', 'Liberation Mono', Courier, monospace" 

81 

82 @property 

83 def token(self) -> str: 

84 """Return the CSS custom property this stack is published as.""" 

85 return _custom_property("font-family-", self.name) 

86 

87 @property 

88 def var(self) -> str: 

89 """Return a ``var()`` reference to this stack, for use in a stylesheet.""" 

90 return f"var({self.token})" 

91 

92 

93class Metric(Enum): 

94 """Spacing and type metrics, by their upstream token name.""" 

95 

96 STANDARD_GUTTER = "30px" 

97 BODY_FONT_SIZE = "13px" 

98 BODY_LINE_HEIGHT = "1.5" 

99 FONT_WEIGHT_LIGHT = "300" 

100 FONT_WEIGHT_NORMAL = "400" 

101 FONT_WEIGHT_BOLD = "700" 

102 

103 @property 

104 def token(self) -> str: 

105 """Return the CSS custom property this metric is published as.""" 

106 return _custom_property("", self.name) 

107 

108 @property 

109 def var(self) -> str: 

110 """Return a ``var()`` reference to this metric, for use in a stylesheet.""" 

111 return f"var({self.token})" 

112 

113 

114def stylesheet() -> str: 

115 """Return the theme as CSS, for inlining into a self-contained page. 

116 

117 Emits the tokens as custom properties on ``:root`` followed by the handful of element rules 

118 ``ensembl-client``'s ``base.css`` sets, so a report matches the site without linking to it. 

119 Fonts are stacks only, with no ``@font-face``: the site self-hosts its woff2 files, and linking 

120 them would stop the page opening offline. 

121 """ 

122 declarations = [] 

123 for colour in Colour: 

124 declarations.append(f" {colour.token}: {colour.value};") 

125 for font in Font: 

126 declarations.append(f" {font.token}: {font.value};") 

127 for metric in Metric: 

128 declarations.append(f" {metric.token}: {metric.value};") 

129 tokens = "\n".join(declarations) 

130 

131 return f""":root {{ 

132{tokens} 

133}} 

134 

135*, 

136*::before, 

137*::after {{ 

138 box-sizing: border-box; 

139}} 

140 

141html {{ 

142 font-size: {Metric.BODY_FONT_SIZE.value}; 

143}} 

144 

145body {{ 

146 margin: 0; 

147 padding: 0; 

148 background: {Colour.OFF_WHITE.var}; 

149 color: {Colour.BLACK.var}; 

150 font-family: {Font.BODY.var}; 

151 line-height: {Metric.BODY_LINE_HEIGHT.value}; 

152 -webkit-font-smoothing: antialiased; 

153}} 

154 

155a {{ 

156 color: {Colour.BLUE.var}; 

157 text-decoration: none; 

158}} 

159 

160code, kbd, pre {{ 

161 font-family: {Font.MONOSPACE.var}; 

162}} 

163"""