\n'
f'
\n' # noqa
f'
\n' # noqa
f'
\n' # noqa
f'')
sec_ret_str = f'
{label}' if show_label else ''
elif type == Media.VIDEO_PAIR:
assert isinstance(content, (list, tuple)) and len(content) == 2
ret_str = f'\n' # noqa
ret_str += f'
\n'
f'
\n' # noqa
f'
\n' # noqa
f'
\n' # noqa
f'
')
sec_ret_str = f'
{label}' if show_label else ''
else:
raise NotImplementedError
if self.allow_annotation:
ret_str = f'
'
if cols_span > 1:
ret_str = f'
{ret_str} | \n'
sec_ret_str = f'
{sec_ret_str} | \n' if not sec_ret_str == '' else sec_ret_str
else:
ret_str = f'
{ret_str} | \n'
sec_ret_str = f'
{sec_ret_str} | \n' if not sec_ret_str == '' else sec_ret_str
return [ret_str, sec_ret_str]
def format_row(self):
all_sample_html = []
current_content_list = copy.deepcopy(self.content_list)
current_rows_meta = copy.deepcopy(self.rows_meta)
while len(current_content_list) > 0:
sample_id = 0
batch_content_list = current_content_list[:self.slice_size]
current_content_list = current_content_list[self.slice_size:]
batch_rows_meta = current_rows_meta[:self.slice_size]
current_rows_meta = current_rows_meta[self.slice_size:]
current_sample_html = []
for one_content, one_row_meta in zip(batch_content_list,
batch_rows_meta):
one_row_str = '
'
if not self.allow_annotation:
one_row_str += '\n'.join([v[0] for v in one_content])
else:
one_row_str += '\n'.join([
v[0].replace('#sample_id#', f'{sample_id}')
for v in one_content
])
row_meta = '#;#'.join(one_row_meta)
one_row_str += (
f' | \n'
)
one_row_str += '
'
one_row_str += '\n'.join([v[1] for v in one_content]) # noqa
if self.allow_annotation: # noqa
one_row_str += f' | \n' # noqa
one_row_str += '
'
# if self.allow_annotation:
# one_row_str = f'
'
current_sample_html.append(one_row_str)
sample_id += 1
all_sample_html.append('
' + '\n'.join(current_sample_html) +
'
')
return all_sample_html
def add_record(self,
content,
label='',
type=Media.TEXT,
row_id=1,
col_id=1,
cols_span=1,
annotation_meta=None,
show_label=True):
if row_id >= len(self.content_list):
self.content_list.append([])
self.rows_meta.append([])
if row_id != len(self.content_list) - 1:
raise RuntimeError(
'row_id should be next number of the last row_id.')
if col_id > len(self.content_list[row_id]):
raise RuntimeError(
'col_id should be next number of the last col_id.')
format_col = self.format_col(content,
f"{row_id}-{col_id}: {label}",
type,
show_label=show_label,
cols_span=cols_span)
annotation_meta = annotation_meta if annotation_meta else ''
if col_id == len(self.content_list[row_id]):
self.content_list[row_id].append(format_col)
self.rows_meta[row_id].append(annotation_meta)
else:
self.content_list[row_id][col_id] = format_col
self.rows_meta[row_id][col_id] = annotation_meta
def save_html(self, path):
html_body = self.format_row()
if isinstance(html_body, list) and len(html_body) > 1:
try:
os.makedirs(path, exist_ok=True)
except: # noqa
print('Create folder path failed.')
for html_id, one_html in enumerate(html_body):
ret_html_list = [
self.html_start, self.html_head, self.html_style,
self.html_body.replace('{BODY}', one_html)
]
if self.allow_annotation:
ret_html_list.append(self.label_button)
ret_html_list.append(self.html_script)
ret_html_list.append(self.html_end)
ret_html = '\n'.join(ret_html_list)
FS.put_object(ret_html.encode(),
os.path.join(path, f"{html_id}.html"))
else:
ret_html_list = [
self.html_start, self.html_head, self.html_style,
self.html_body.replace('{BODY}', html_body[0])
]
if self.allow_annotation:
ret_html_list.append(self.label_button)
ret_html_list.append(self.html_script)
ret_html_list.append(self.html_end)
ret_html = '\n'.join(ret_html_list)
FS.put_object(ret_html.encode(), path)