Repository: spyder-ide/spyder

Issue:
In Spyder 5.2.1, History pane is not being updated after entering commands in the IPython console
This problem is described here:

https://stackoverflow.com/questions/70656204/spyder-history-pane-issues

and I can confirm it's the case with the latest `5.x`.


Discussion:

Resolving patch:
diff --git a/spyder/plugins/history/plugin.py b/spyder/plugins/history/plugin.py
--- a/spyder/plugins/history/plugin.py
+++ b/spyder/plugins/history/plugin.py
@@ -30,7 +30,8 @@ class HistoryLog(SpyderDockablePlugin):
     """
 
     NAME = 'historylog'
-    REQUIRES = [Plugins.Preferences, Plugins.Editor, Plugins.Console]
+    REQUIRES = [Plugins.Preferences, Plugins.Console]
+    OPTIONAL = [Plugins.IPythonConsole]
     TABIFY = Plugins.IPythonConsole
     WIDGET_CLASS = HistoryWidget
     CONF_SECTION = NAME
@@ -76,6 +77,12 @@ def on_console_available(self):
         console = self.get_plugin(Plugins.Console)
         console.sig_refreshed.connect(self.refresh)
 
+    @on_plugin_available(plugin=Plugins.IPythonConsole)
+    def on_ipyconsole_available(self):
+        ipyconsole = self.get_plugin(Plugins.IPythonConsole)
+        ipyconsole.sig_append_to_history_requested.connect(
+            self.append_to_history)
+
     @on_plugin_teardown(plugin=Plugins.Preferences)
     def on_preferences_teardown(self):
         preferences = self.get_plugin(Plugins.Preferences)
@@ -86,6 +93,12 @@ def on_console_teardown(self):
         console = self.get_plugin(Plugins.Console)
         console.sig_refreshed.disconnect(self.refresh)
 
+    @on_plugin_teardown(plugin=Plugins.IPythonConsole)
+    def on_ipyconsole_teardown(self):
+        ipyconsole = self.get_plugin(Plugins.IPythonConsole)
+        ipyconsole.sig_append_to_history_requested.disconnect(
+            self.append_to_history)
+
     def update_font(self):
         color_scheme = self.get_color_scheme()
         font = self.get_font()
diff --git a/spyder/plugins/history/widgets.py b/spyder/plugins/history/widgets.py
--- a/spyder/plugins/history/widgets.py
+++ b/spyder/plugins/history/widgets.py
@@ -73,7 +73,6 @@ def __init__(self, name, plugin, parent):
         self.dockviewer = None
         self.wrap_action = None
         self.linenumbers_action = None
-        self.editors = []
         self.filenames = []
         self.font = None
 


Test patch:
diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py
--- a/spyder/app/tests/test_mainwindow.py
+++ b/spyder/app/tests/test_mainwindow.py
@@ -4472,5 +4472,30 @@ def test_rename_files_in_editor_after_folder_rename(main_window, mocker,
     assert codeeditor.filename == osp.join(str(tmpdir), new_path, fname)
 
 
+@pytest.mark.slow
+@flaky(max_runs=3)
+def test_history_from_ipyconsole(main_window, qtbot):
+    """
+    Check that we register commands introduced in the IPython console in
+    the History pane.
+    """
+    # Wait for the console to be up
+    shell = main_window.ipyconsole.get_current_shellwidget()
+    qtbot.waitUntil(lambda: shell._prompt_html is not None,
+                    timeout=SHELL_TIMEOUT)
+
+    # Run some code in the console
+    code = '5 + 3'
+    with qtbot.waitSignal(shell.executed):
+        shell.execute(code)
+
+    # Check that code is displayed in History
+    history = main_window.get_plugin(Plugins.History)
+    history.switch_to_plugin()
+    history_editor = history.get_widget().editors[0]
+    text = history_editor.toPlainText()
+    assert text.splitlines()[-1] == code
+
+
 if __name__ == "__main__":
     pytest.main()