Remove elements from the report. The element to remove is searched for by its title. Optionally, tags may be specified as well to narrow down the search to elements that have the supplied tags. Parameters ---------- title : str The title
(self, *, title=None, tags=None, remove_all=False)
| 2273 | ) |
| 2274 | |
| 2275 | def remove(self, *, title=None, tags=None, remove_all=False): |
| 2276 | """Remove elements from the report. |
| 2277 | |
| 2278 | The element to remove is searched for by its title. Optionally, tags |
| 2279 | may be specified as well to narrow down the search to elements that |
| 2280 | have the supplied tags. |
| 2281 | |
| 2282 | Parameters |
| 2283 | ---------- |
| 2284 | title : str |
| 2285 | The title of the element(s) to remove. |
| 2286 | |
| 2287 | .. versionadded:: 0.24.0 |
| 2288 | tags : array-like of str | str | None |
| 2289 | If supplied, restrict the operation to elements with the supplied |
| 2290 | tags. |
| 2291 | |
| 2292 | .. versionadded:: 0.24.0 |
| 2293 | remove_all : bool |
| 2294 | Controls the behavior if multiple elements match the search |
| 2295 | criteria. If ``False`` (default) only the element last added to the |
| 2296 | report will be removed. If ``True``, all matches will be removed. |
| 2297 | |
| 2298 | .. versionadded:: 0.24.0 |
| 2299 | |
| 2300 | Returns |
| 2301 | ------- |
| 2302 | removed_index : int | tuple of int | None |
| 2303 | The indices of the elements that were removed, or ``None`` if no |
| 2304 | element matched the search criteria. A tuple will always be |
| 2305 | returned if ``remove_all`` was set to ``True`` and at least one |
| 2306 | element was removed. |
| 2307 | |
| 2308 | .. versionchanged:: 0.24.0 |
| 2309 | Returns tuple if ``remove_all`` is ``True``. |
| 2310 | """ |
| 2311 | remove_idx = [] |
| 2312 | for idx, element in enumerate(self._content): |
| 2313 | if element.name == title: |
| 2314 | if tags is not None and not all(t in element.tags for t in tags): |
| 2315 | continue |
| 2316 | remove_idx.append(idx) |
| 2317 | |
| 2318 | if not remove_idx: |
| 2319 | remove_idx = None |
| 2320 | elif not remove_all: # only remove last occurrence |
| 2321 | remove_idx = remove_idx[-1] |
| 2322 | del self._content[remove_idx] |
| 2323 | else: # remove all occurrences |
| 2324 | remove_idx = tuple(remove_idx) |
| 2325 | self._content = [ |
| 2326 | e for idx, e in enumerate(self._content) if idx not in remove_idx |
| 2327 | ] |
| 2328 | |
| 2329 | return remove_idx |
| 2330 | |
| 2331 | @fill_doc |
| 2332 | def _add_or_replace(self, *, title, section, tags, html_partial, replace=False): |