Coverage for pesummary/core/webpage/tables.py: 99.2%
120 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
1# Licensed under an MIT style license -- see LICENSE.md
3from pesummary.core.webpage.base import Base
5__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"]
8class table_of_images(Base):
10 def __init__(self, content, rows, columns, html_file, code, cli,
11 autoscale=False, unique_id=None, captions=None, extra_div=False,
12 mcmc_samples=False, margin_left=None, display=None,
13 container_id=None, close_container=True,
14 add_to_open_container=False):
15 """
17 Parameters
18 ----------
19 content: nd list
20 nd list containing the image paths that you want to include in your
21 table. Sorted by columns [[column1], [column2]]
22 """
23 self.content = content
24 self.rows = rows
25 self.columns = columns
26 self.html_file = html_file
27 self.code = code
28 self.cli = cli
29 self.captions = captions
30 self.autoscale = autoscale
31 self.unique_id = unique_id
32 self.extra_div = extra_div
33 self.mcmc_samples = mcmc_samples
34 self.margin_left = margin_left
35 self.display = display
36 self.container_id = container_id
37 self.close_container = close_container
38 self.add_to_open_container = add_to_open_container
39 if self.unique_id is not None:
40 self.modal_id = "Modal_{}".format(self.unique_id)
41 self.demo_id = "demo_{}".format(self.unique_id)
42 else:
43 self.modal_id = "MyModal"
44 self.demo_id = "demo"
45 self._add_scripts()
47 def _add_scripts(self):
48 self.add_content("<link rel='stylesheet' href='../css/image_styles.css'>\n")
50 def _insert_image(self, path, width, indent, _id, justify="center"):
51 string = "<img src='{}' alt='No image available' ".format(path) + \
52 "style='align-items:center; width:{}px;'".format(width)
53 string += "id={} onclick='{}(\"{}\"".format(_id, self.code, _id)
54 if self.code != "changeimage":
55 string += ", \"{}\"".format(self.modal_id)
56 if self.mcmc_samples:
57 string += ", mcmc_samples=\"{}\"".format(self.mcmc_samples)
58 string += ")'>\n"
59 self.add_content(string, indent=indent)
61 def make(self):
62 if not self.add_to_open_container:
63 self.add_content("<script>")
64 self.add_content("$(document).ready(function(){", indent=2)
65 self.add_content("$('[data-toggle=\"popover\"]').popover();", indent=4)
66 self.add_content("});", indent=2)
67 self.add_content("</script>")
68 self.add_content("<style>")
69 self.add_content(".popover {", indent=2)
70 self.add_content("max-width: 550px;", indent=4)
71 self.add_content("width: 550px;", indent=4)
72 self.add_content("}", indent=2)
73 self.add_content("</style>")
74 self.make_container(
75 display=self.display, container_id=self.container_id
76 )
77 self.make_div(2, _class="mx-auto d-block", _style=None)
78 if self.rows == 1:
79 _id = self.content[0][0].split("/")[-1][:-4]
80 self._insert_image(self.content[0][0], 900, 4, _id, justify="left")
81 self.make_div(2, _class=None, _style="float: right;")
82 for num, i in enumerate(self.content[1]):
83 _id = i.split("/")[-1][:-4]
84 if self.margin_left is not None:
85 _style = "margin-left: {}".format(self.margin_left)
86 else:
87 _style = None
88 self.make_div(2, _class="row", _style=_style)
89 self.make_div(4, _class="column")
90 self.add_content("<a>", 6)
91 self._insert_image(i, 415, 8, _id, justify=None)
92 self.add_content("</a>", 6)
93 if self.captions:
94 self.add_content("<div class='row justify-content-center'>", indent=6)
95 self.make_div(6, _class="col-sm-4", _style=None)
96 self.add_content("<button value='test' class='btn btn-info "
97 "btn-xs' style='cursor: pointer' "
98 "data-toggle='popover' "
99 "data-placement='top' data-content='%s'>"
100 "Caption</button>" % (self.captions[1][num]),
101 indent=8)
102 self.end_div(6)
103 self.end_div(6)
104 self.end_div(4)
105 self.end_div(2)
106 self.end_div(2)
107 else:
108 ind = 0
109 width = "450"
110 captions_margin_left = "-70"
111 _class = "row justify-content-center"
112 self.make_div(4, _class=_class, _style=None)
113 if self.margin_left is not None:
114 _style = "margin-left: {}".format(self.margin_left)
115 else:
116 _style = None
117 self.make_div(6, _class="row", _style=_style)
118 for idx, i in enumerate(self.content):
119 if self.autoscale:
120 width = str(1350 / len(i))
121 captions_margin_left = str(-1350. / (9.5 * len(i)))
122 self.make_div(8, _class="column", _style="padding-left: 1em;")
123 for num, j in enumerate(i):
124 _id = j.split("/")[-1][:-4]
125 self.make_div(10, _class='container',
126 _style=("display: inline-block; width: auto; "
127 "padding: 0;"))
128 self.add_content(
129 "<a href='#%s' data-slide-to='%s'>\n" % (
130 self.demo_id, ind
131 ), indent=6)
132 self._insert_image(j, width, 8, _id, justify=None)
133 self.add_content("</a>\n", indent=6)
134 if self.cli or self.captions:
135 self.add_content("<div class='row justify-content-center'>", indent=6)
136 self.add_content(
137 "<div class='col-sm-{}'>".format(len(i) + 1), indent=6
138 )
139 if self.cli:
140 self.add_content("<button value='test' class='btn "
141 "btn-info btn-xs' style='cursor: pointer' "
142 "data-toggle='popover' data-placement='top' "
143 "data-content='%s'>"
144 "Command Line</button>" % (self.cli[idx][num]),
145 indent=10)
146 self.end_div(10)
147 if self.captions:
148 if self.cli:
149 _style = "margin-left: {}px;".format(
150 captions_margin_left
151 )
152 else:
153 _style = None
154 self.make_div(
155 6, _class="col-sm-{}".format(len(i) + 1),
156 _style=_style
157 )
158 self.add_content("<button value='test' class='btn "
159 "btn-info btn-xs' style='cursor: pointer' "
160 "data-toggle='popover' "
161 "data-placement='top' data-content='%s'>"
162 "Caption</button>" % (self.captions[idx][num]),
163 indent=10)
164 self.end_div(10)
165 self.end_div(10)
166 if self.extra_div:
167 self.end_div(10)
168 if self.cli or self.captions:
169 self.end_div(10)
170 ind += 1
171 self.end_div(8)
172 self.end_div(6)
173 self.end_div(4)
174 self.end_div(2)
175 if self.close_container:
176 self.end_container()