Repository: spyder-ide/spyder

Issue:
Color scheme changes after kernel restart
## Description

The color scheme shown in the console window changes after you restart the kernel

**What steps will reproduce the problem?**

1. Open a new IPython console
2. Cause an error (run an undefined function "prin")
3. Observe the color scheme -- as expected
4. Restart that console's kernel (I used ctrl-.)
2. Cause an error (run an undefined function "prin")
3. Observe the color scheme -- has now changed to very bright colors

![2018-01-17_12-11-18](https://user-images.githubusercontent.com/2075083/35061968-0760b4d8-fb80-11e7-9662-b36825e47e59.gif)


## Versions and main components

* Spyder Version: 3.2.6
* Python Version:  3.6.3
* Qt Version: 5.6.2
* PyQt Version: 5.6
* Operating system: Windows


## Dependencies

IPython >=4.0    :  6.1.0 (OK)
cython >=0.21    :  0.26.1 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
nbconvert >=4.0  :  5.3.1 (OK)
numpy >=1.7      :  1.13.3 (OK)
pandas >=0.13.1  :  0.22.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pyflakes >=0.6.0 :  1.6.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.7.4 (OK)
qtconsole >=4.2.0:  4.3.1 (OK)
rope >=0.9.4     :  0.10.5 (OK)
sphinx >=0.6.6   :  1.6.3 (OK)
sympy >=0.7.3    :  1.1.1 (OK)





Discussion:
Thanks for reporting. We'll take a look at this in a future release.
Confirmed this is happening on Windows 7, but it is not happening on Ubuntu.

Found the magic command `%colors`.  If `%colors linux` is entered, then it uses the very bright colors.  If `%colors lightbg` is used, then it looks normal.  So, for some reason, on Windows, when the kernel is restarted, the color scheme is changing from `lightbg` to `linux`, but on Ubuntu, it is not changing.  
@csabella, do you have time to take a look at this one?
@ccordoba12   I started taking a look at this, but it's a mystery so far.  There are two places in the Spyder code where `%colors` is invoked, but using `debug_print` on Ubuntu, those methods aren't called during a kernel reset.  I need to check debug_print on Windows, but I'm also thinking that this may be happening in qtconsole and not Spyder.  I need to do some more digging.  
I believe this was caused by IPython pull requests 10260 (https://github.com/ipython/ipython/pull/10260), 10281, and 9838, but I can't install a version before 5.3.0 to check it out.  Probably the best thing for Spyder to do would be to call the overrides of the %colors on a restart.

EDIT:  I was able to downgrade to IPython 5.1.0 and it did not happen on that version, so I am certain those were the pull requests that introduced this.
> Probably the best thing for Spyder to do would be to call the overrides of the %colors on a restart.

What do you mean by that?
> What do you mean by that?

I apologize; I've been looking at `debug_print` so much that I knew what I meant.  :-) 

In the IPython console, in `client.py`, there is a method called `configure_shellwidget` with the following:
```
        if not create_qss_style(self.shellwidget.syntax_style)[1]:
            self.shellwidget.silent_execute("%colors linux")
        else:
            self.shellwidget.silent_execute("%colors lightbg")
```

When a Spyder is first opened or when a new console is created, this code is called.  However, it is not called on a restart of the kernel.  It would seem that the IPython defaults take over and change the color settings in the console.  So I was thinking that maybe we need to execute this on a restart.
Ok, good call! Could you open a PR with that insight? Thanks!

Resolving patch:
diff --git a/spyder/widgets/ipythonconsole/client.py b/spyder/widgets/ipythonconsole/client.py
--- a/spyder/widgets/ipythonconsole/client.py
+++ b/spyder/widgets/ipythonconsole/client.py
@@ -232,10 +232,7 @@ def configure_shellwidget(self, give_focus=True):
         self.shellwidget.executed.connect(self.shellwidget.get_cwd)
 
         # To apply style
-        if not create_qss_style(self.shellwidget.syntax_style)[1]:
-            self.shellwidget.silent_execute("%colors linux")
-        else:
-            self.shellwidget.silent_execute("%colors lightbg")
+        self.set_color_scheme(self.shellwidget.syntax_style, reset=False)
 
         # To hide the loading page
         self.shellwidget.sig_prompt_ready.connect(self._hide_loading_page)
@@ -432,9 +429,9 @@ def set_infowidget_font(self):
         font = get_font(option='rich_font')
         self.infowidget.set_font(font)
 
-    def set_color_scheme(self, color_scheme):
+    def set_color_scheme(self, color_scheme, reset=True):
         """Set IPython color scheme."""
-        self.shellwidget.set_color_scheme(color_scheme)
+        self.shellwidget.set_color_scheme(color_scheme, reset)
 
     def shutdown(self):
         """Shutdown kernel"""
@@ -450,7 +447,7 @@ def interrupt_kernel(self):
     @Slot()
     def restart_kernel(self):
         """
-        Restart the associanted kernel
+        Restart the associated kernel.
 
         Took this code from the qtconsole project
         Licensed under the BSD license
@@ -475,7 +472,10 @@ def restart_kernel(self):
                         before_prompt=True
                     )
                 else:
-                    sw.reset(clear=True)
+                    # For issue 6235.  IPython was changing the setting of
+                    # %colors on windows by assuming it was using a dark
+                    # background.  This corrects it based on the scheme.
+                    self.set_color_scheme(sw.syntax_style)
                     sw._append_html(_("<br>Restarting kernel...\n<hr><br>"),
                                     before_prompt=False)
             else:
diff --git a/spyder/widgets/ipythonconsole/shell.py b/spyder/widgets/ipythonconsole/shell.py
--- a/spyder/widgets/ipythonconsole/shell.py
+++ b/spyder/widgets/ipythonconsole/shell.py
@@ -133,14 +133,15 @@ def set_bracket_matcher_color_scheme(self, color_scheme):
         mpcolor = bsh.get_matched_p_color()
         self._bracket_matcher.format.setBackground(mpcolor)
 
-    def set_color_scheme(self, color_scheme):
+    def set_color_scheme(self, color_scheme, reset=True):
         """Set color scheme of the shell."""
         self.set_bracket_matcher_color_scheme(color_scheme)
         self.style_sheet, dark_color = create_qss_style(color_scheme)
         self.syntax_style = color_scheme
         self._style_sheet_changed()
         self._syntax_style_changed()
-        self.reset(clear=True)
+        if reset:
+            self.reset(clear=True)
         if not dark_color:
             self.silent_execute("%colors linux")
         else:


Test patch: