Take a numpy.ndarray containing strings, and convert it into html. If the ndarray contains a single scalar string, that string is converted to html via our sanitized markdown parser. If it contains an array of strings, the strings are individually converted to html and then composed int
(text_arr, enable_markdown)
| 153 | |
| 154 | |
| 155 | def text_array_to_html(text_arr, enable_markdown): |
| 156 | """Take a numpy.ndarray containing strings, and convert it into html. |
| 157 | |
| 158 | If the ndarray contains a single scalar string, that string is converted to |
| 159 | html via our sanitized markdown parser. If it contains an array of strings, |
| 160 | the strings are individually converted to html and then composed into a table |
| 161 | using make_table. If the array contains dimensionality greater than 2, |
| 162 | all but two of the dimensions are removed, and a warning message is prefixed |
| 163 | to the table. |
| 164 | |
| 165 | Args: |
| 166 | text_arr: A numpy.ndarray containing strings. |
| 167 | enable_markdown: boolean, whether to enable Markdown |
| 168 | |
| 169 | Returns: |
| 170 | The array converted to html. |
| 171 | """ |
| 172 | if not text_arr.shape: |
| 173 | # It is a scalar. No need to put it in a table. |
| 174 | if enable_markdown: |
| 175 | return plugin_util.markdown_to_safe_html(text_arr.item()) |
| 176 | else: |
| 177 | return plugin_util.safe_html(text_arr.item()) |
| 178 | warning = "" |
| 179 | if len(text_arr.shape) > 2: |
| 180 | warning = plugin_util.markdown_to_safe_html( |
| 181 | WARNING_TEMPLATE % len(text_arr.shape) |
| 182 | ) |
| 183 | text_arr = reduce_to_2d(text_arr) |
| 184 | if enable_markdown: |
| 185 | table = plugin_util.markdowns_to_safe_html( |
| 186 | text_arr.reshape(-1), |
| 187 | lambda xs: make_table(np.array(xs).reshape(text_arr.shape)), |
| 188 | ) |
| 189 | else: |
| 190 | # Convert utf-8 bytes to str. The built-in np.char.decode doesn't work on |
| 191 | # object arrays, and converting to an numpy chararray is lossy. |
| 192 | decode = lambda bs: bs.decode("utf-8") if isinstance(bs, bytes) else bs |
| 193 | text_arr_str = np.array( |
| 194 | [decode(bs) for bs in text_arr.reshape(-1)] |
| 195 | ).reshape(text_arr.shape) |
| 196 | table = plugin_util.safe_html(make_table(text_arr_str)) |
| 197 | return warning + table |
| 198 | |
| 199 | |
| 200 | def process_event(wall_time, step, string_ndarray, enable_markdown): |
no test coverage detected
searching dependent graphs…