Spaces:
Runtime error
Runtime error
porting annotation to a separate file
Browse files- annotator.py +107 -0
annotator.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import html
|
2 |
+
from htbuilder import H, HtmlElement, styles
|
3 |
+
from htbuilder.units import unit
|
4 |
+
|
5 |
+
# Only works in 3.7+: from htbuilder import div, span
|
6 |
+
div = H.div
|
7 |
+
span = H.span
|
8 |
+
|
9 |
+
# Only works in 3.7+: from htbuilder.units import px, rem, em
|
10 |
+
px = unit.px
|
11 |
+
rem = unit.rem
|
12 |
+
em = unit.em
|
13 |
+
|
14 |
+
# Colors from the Streamlit palette.
|
15 |
+
# These are red-70, orange-70, ..., violet-70, gray-70.
|
16 |
+
PALETTE = [
|
17 |
+
"#ff4b4b",
|
18 |
+
"#ffa421",
|
19 |
+
"#ffe312",
|
20 |
+
"#21c354",
|
21 |
+
"#00d4b1",
|
22 |
+
"#00c0f2",
|
23 |
+
"#1c83e1",
|
24 |
+
"#803df5",
|
25 |
+
"#808495",
|
26 |
+
]
|
27 |
+
|
28 |
+
OPACITIES = [
|
29 |
+
"33", "66",
|
30 |
+
]
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
def annotation(body, label="", background=None, color=None, **style):
|
35 |
+
"""
|
36 |
+
from https://github.com/tvst/st-annotated-text/blob/master/annotated_text/util.py
|
37 |
+
"""
|
38 |
+
|
39 |
+
color_style = {}
|
40 |
+
|
41 |
+
if color:
|
42 |
+
color_style['color'] = color
|
43 |
+
|
44 |
+
if not background:
|
45 |
+
label_sum = sum(ord(c) for c in label)
|
46 |
+
background_color = PALETTE[label_sum % len(PALETTE)]
|
47 |
+
background_opacity = OPACITIES[label_sum % len(OPACITIES)]
|
48 |
+
background = background_color + background_opacity
|
49 |
+
|
50 |
+
return (
|
51 |
+
span(
|
52 |
+
style=styles(
|
53 |
+
background=background,
|
54 |
+
border_radius=rem(0.33),
|
55 |
+
padding=(rem(0.125), rem(0.5)),
|
56 |
+
overflow="hidden",
|
57 |
+
**color_style,
|
58 |
+
**style,
|
59 |
+
))(
|
60 |
+
|
61 |
+
html.escape(body),
|
62 |
+
|
63 |
+
span(
|
64 |
+
style=styles(
|
65 |
+
padding_left=rem(0.5),
|
66 |
+
text_transform="uppercase",
|
67 |
+
))(
|
68 |
+
span(
|
69 |
+
style=styles(
|
70 |
+
font_size=em(0.67),
|
71 |
+
opacity=0.5,
|
72 |
+
))(
|
73 |
+
html.escape(label),
|
74 |
+
),
|
75 |
+
),
|
76 |
+
)
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
def get_annotated_html(*args):
|
81 |
+
|
82 |
+
out = div()
|
83 |
+
|
84 |
+
for arg in args:
|
85 |
+
if isinstance(arg, str):
|
86 |
+
out(html.escape(arg))
|
87 |
+
|
88 |
+
elif isinstance(arg, HtmlElement):
|
89 |
+
out(arg)
|
90 |
+
|
91 |
+
elif isinstance(arg, tuple):
|
92 |
+
out(annotation(*arg))
|
93 |
+
|
94 |
+
elif isinstance(arg,list):
|
95 |
+
for el in arg:
|
96 |
+
if isinstance(el, str):
|
97 |
+
out(html.escape(el))
|
98 |
+
|
99 |
+
elif isinstance(el, HtmlElement):
|
100 |
+
out(el)
|
101 |
+
|
102 |
+
elif isinstance(el, tuple):
|
103 |
+
out(annotation(*el))
|
104 |
+
else:
|
105 |
+
raise Exception("Oh noes!")
|
106 |
+
|
107 |
+
return str(out)
|