From a1d305595f9f3286102475e93fa2fb42a982b8df Mon Sep 17 00:00:00 2001 From: Andrew Wells Date: Tue, 1 Aug 2023 13:25:25 -0500 Subject: [PATCH] Implement (slightly hacky) dark mode by post-processing the formatted HTML --- lime/explanation.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lime/explanation.py b/lime/explanation.py index a189729ab..cc1710cc5 100644 --- a/lime/explanation.py +++ b/lime/explanation.py @@ -7,6 +7,7 @@ import json import string import numpy as np +import re from .exceptions import LimeError @@ -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. @@ -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, @@ -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. @@ -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: @@ -324,4 +331,9 @@ def jsonize(x): ''' % (random_id, predict_proba_js, predict_value_js, exp_js, raw_js) out += u'' + 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) + return out