Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Dark Mode for explanation.show_in_notebook method #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lime/explanation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import string
import numpy as np
import re

from .exceptions import LimeError

Expand Down Expand Up @@ -186,6 +187,7 @@ def show_in_notebook(self,
labels=None,
predict_proba=True,
show_predicted_value=True,
dark_mode=False,
**kwargs):
"""Shows html explanation in ipython notebook.

Expand All @@ -196,6 +198,7 @@ def show_in_notebook(self,
display(HTML(self.as_html(labels=labels,
predict_proba=predict_proba,
show_predicted_value=show_predicted_value,
dark_mode=dark_mode,
**kwargs)))

def save_to_file(self,
Expand Down Expand Up @@ -223,6 +226,7 @@ def as_html(self,
labels=None,
predict_proba=True,
show_predicted_value=True,
dark_mode=False,
**kwargs):
"""Returns the explanation as an html page.

Expand All @@ -231,10 +235,13 @@ def as_html(self,
If you ask for a label for which an explanation wasn't
computed, will throw an exception. If None, will show
explanations for all available labels. (only used for classification)
predict_proba: if true, add barchart with prediction probabilities
predict_proba: if true, add barchart with prediction probabilities
for the top classes. (only used for classification)
show_predicted_value: if true, add barchart with expected value
show_predicted_value: if true, add barchart with expected value
(only used for regression)
dark_mode: if true, updates all text color to white in the resulting HTML
(only useful when using show_in_notebook() from a notebook rendered
in dark mode)
kwargs: keyword arguments, passed to domain_mapper

Returns:
Expand Down Expand Up @@ -324,4 +331,9 @@ def jsonize(x):
''' % (random_id, predict_proba_js, predict_value_js, exp_js, raw_js)
out += u'</body></html>'

if (dark_mode):
out = out.replace("\"black\"", "\"white\"")
out = out.replace("all: initial;", "all: initial; color: white;")
out = re.sub(r"svg.append\('text(((?!fill).)*);", r"svg.append('text\1.style('fill', 'white');", out)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex finds all svg texts (which don't adhere to any css or html color styling) for which a color is not specified (to avoid touching the purposefully colorful words) and decorates them with another style (fill: white)


return out