Skip to content

Commit

Permalink
Add option to return HMTL object from visualize_text() (#548)
Browse files Browse the repository at this point in the history
Summary:
Related issue: #544

Adds flag `return_html`to `visualize_text()` that allows for the IPython HTML to be returned if the flag is set to `True`.

This is useful for cases where users may want to save the output from outside of a notebook etc.

Usage looks like:

```python
from captum.attr import visualization as viz
.... # get attributions and data record

html_obj = viz.visualize_text([score_viz])
```

Pull Request resolved: #548

Reviewed By: vivekmig

Differential Revision: D25424019

Pulled By: bilalsal

fbshipit-source-id: 27a90f2775d90cbc848858fe1b439ddc2855cba4
  • Loading branch information
cdpierse authored and facebook-github-bot committed Dec 9, 2020
1 parent 68d4f24 commit 4e40f9f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions captum/attr/_utils/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def format_word_importances(words, importances):

def visualize_text(
datarecords: Iterable[VisualizationDataRecord], legend: bool = True
) -> None:
) -> HTML:
assert HAS_IPYTHON, (
"IPython must be available to visualize text. "
"Please run 'pip install ipython'."
Expand Down Expand Up @@ -567,4 +567,7 @@ def visualize_text(

dom.append("".join(rows))
dom.append("</table>")
display(HTML("".join(dom)))
html = HTML("".join(dom))
display(html)

return html

0 comments on commit 4e40f9f

Please sign in to comment.