From a58ef95bc8feff1cdf69998f8705c88a84e1ff24 Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Fri, 10 Jan 2025 20:03:39 +0800 Subject: [PATCH 1/9] IPython console: Start Spyder and connect to an existing ipython kernel --- spyder/app/cli_options.py | 7 +++++++ spyder/plugins/ipythonconsole/plugin.py | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spyder/app/cli_options.py b/spyder/app/cli_options.py index 9dee61ab853..b7083387859 100644 --- a/spyder/app/cli_options.py +++ b/spyder/app/cli_options.py @@ -157,6 +157,13 @@ def get_options(argv=None): default=None, help="Choose a configuration directory to use for Spyder." ) + parser.add_argument( + '--connect-to-kernel', + type=str, + dest="connection_file", + default=None, + help="Start Spyder and connect to an existing ipython kernel." + ) parser.add_argument('files', nargs='*') options = parser.parse_args(argv) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index cd7374b3789..870a45a2e7c 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -559,7 +559,13 @@ def on_close(self, cancelable=False): return self.get_widget().close_all_clients() def on_mainwindow_visible(self): - self.create_new_client(give_focus=False) + cli_options = self.get_command_line_options() + connection_file = cli_options.connection_file + if connection_file is not None: + self.create_client_for_kernel(connection_file) + else: + self.create_new_client(give_focus=False) + # ---- Private methods # ------------------------------------------------------------------------- From 85e55fc77dcb0567ded373ec5b1d78380a935139 Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Fri, 10 Jan 2025 20:17:31 +0800 Subject: [PATCH 2/9] code format --- spyder/plugins/ipythonconsole/plugin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index 870a45a2e7c..9dc211ce2d0 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -566,7 +566,6 @@ def on_mainwindow_visible(self): else: self.create_new_client(give_focus=False) - # ---- Private methods # ------------------------------------------------------------------------- def _on_project_loaded(self, path): From d176f3ec51f3411791a4a2440a2435968bfc0ad6 Mon Sep 17 00:00:00 2001 From: Social_Mean Date: Sun, 12 Jan 2025 16:26:22 +0800 Subject: [PATCH 3/9] Improve help for this new option. Co-authored-by: Carlos Cordoba --- spyder/app/cli_options.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spyder/app/cli_options.py b/spyder/app/cli_options.py index b7083387859..aa1cb2feefb 100644 --- a/spyder/app/cli_options.py +++ b/spyder/app/cli_options.py @@ -162,7 +162,10 @@ def get_options(argv=None): type=str, dest="connection_file", default=None, - help="Start Spyder and connect to an existing ipython kernel." + help=( + "Connect to an existing kernel whose info is available in a " + "kernel-*.json file" + ) ) parser.add_argument('files', nargs='*') From 7d1515cffce98da6437fe54e732b7a80ba6d70f9 Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Sun, 12 Jan 2025 18:33:04 +0800 Subject: [PATCH 4/9] Give focus to the Editor and not the console at startup --- spyder/plugins/ipythonconsole/plugin.py | 5 +++-- spyder/plugins/ipythonconsole/widgets/main_widget.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index 9dc211ce2d0..3e811f54163 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -562,7 +562,7 @@ def on_mainwindow_visible(self): cli_options = self.get_command_line_options() connection_file = cli_options.connection_file if connection_file is not None: - self.create_client_for_kernel(connection_file) + self.create_client_for_kernel(connection_file, give_focus=False) else: self.create_new_client(give_focus=False) @@ -720,6 +720,7 @@ def create_client_for_kernel( sshkey=None, password=None, server_id=None, + give_focus=False, can_close=True, ): """ @@ -752,7 +753,7 @@ def create_client_for_kernel( The created client. """ return self.get_widget().create_client_for_kernel( - connection_file, hostname, sshkey, password, server_id, can_close + connection_file, hostname, sshkey, password, server_id, give_focus, can_close ) def get_client_for_file(self, filename): diff --git a/spyder/plugins/ipythonconsole/widgets/main_widget.py b/spyder/plugins/ipythonconsole/widgets/main_widget.py index 5fd06d36a78..8f19c28f4a7 100644 --- a/spyder/plugins/ipythonconsole/widgets/main_widget.py +++ b/spyder/plugins/ipythonconsole/widgets/main_widget.py @@ -1726,7 +1726,7 @@ def create_new_client(self, give_focus=True, filename='', special=None, return client def create_client_for_kernel(self, connection_file, hostname, sshkey, - password, server_id=None, can_close=True): + password, server_id=None, give_focus=False, can_close=True): """Create a client connected to an existing kernel.""" given_name = None master_client = None @@ -1772,6 +1772,7 @@ def create_client_for_kernel(self, connection_file, hostname, sshkey, additional_options=self.additional_options(), handlers=self.registered_spyder_kernel_handlers, server_id=server_id, + give_focus=give_focus, can_close=can_close, ) From 93db429b1a5c9767a1fb42d0a1b8b57207557a27 Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Sun, 12 Jan 2025 18:35:25 +0800 Subject: [PATCH 5/9] format code --- spyder/plugins/ipythonconsole/plugin.py | 8 +++++++- spyder/plugins/ipythonconsole/widgets/main_widget.py | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index 3e811f54163..275ce078430 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -753,7 +753,13 @@ def create_client_for_kernel( The created client. """ return self.get_widget().create_client_for_kernel( - connection_file, hostname, sshkey, password, server_id, give_focus, can_close + connection_file, + hostname, + sshkey, + password, + server_id, + give_focus, + can_close, ) def get_client_for_file(self, filename): diff --git a/spyder/plugins/ipythonconsole/widgets/main_widget.py b/spyder/plugins/ipythonconsole/widgets/main_widget.py index 8f19c28f4a7..974eaf454b4 100644 --- a/spyder/plugins/ipythonconsole/widgets/main_widget.py +++ b/spyder/plugins/ipythonconsole/widgets/main_widget.py @@ -1726,7 +1726,8 @@ def create_new_client(self, give_focus=True, filename='', special=None, return client def create_client_for_kernel(self, connection_file, hostname, sshkey, - password, server_id=None, give_focus=False, can_close=True): + password, server_id=None, give_focus=False, + can_close=True): """Create a client connected to an existing kernel.""" given_name = None master_client = None From ba5307f5fb0394394f191c842858dc6db38c1f22 Mon Sep 17 00:00:00 2001 From: Social_Mean Date: Wed, 15 Jan 2025 08:29:02 +0800 Subject: [PATCH 6/9] Use the `find_connection_file` method from `IPythonConsoleWidget` to get the absolute path to the file in the place where it's saved by Jupyter-client. Co-authored-by: Carlos Cordoba --- spyder/plugins/ipythonconsole/plugin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index 275ce078430..f1163bfc36a 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -562,7 +562,10 @@ def on_mainwindow_visible(self): cli_options = self.get_command_line_options() connection_file = cli_options.connection_file if connection_file is not None: - self.create_client_for_kernel(connection_file, give_focus=False) + self.create_client_for_kernel( + self.get_widget().find_connection_file(connection_file), + give_focus=False, + ) else: self.create_new_client(give_focus=False) From 5d258092894e2ec2893aed50ed0fbab9ca8a674c Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Wed, 15 Jan 2025 08:41:51 +0800 Subject: [PATCH 7/9] Add `give_focus` to the docstring of method `create_client_for_kernel`. --- spyder/plugins/ipythonconsole/plugin.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index f1163bfc36a..d649a0b4894 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -745,6 +745,9 @@ def create_client_for_kernel( running. server_id: str, optional The remote server id to which this client is connected to. + give_focus : bool, optional + True if the new client should gain the window + focus, False otherwise. The default is True. can_close: bool, optional Whether the client can be closed. This is useful to prevent closing the client that will be connected to a remote kernel before the From 8d9a54d4b8a39e72ee01f6aa8bbea85719d90f3b Mon Sep 17 00:00:00 2001 From: ming <2052760@tongji.edu.cn> Date: Wed, 15 Jan 2025 09:10:58 +0800 Subject: [PATCH 8/9] Add docstring for `IPythonConsole.on_mainwindow_visible`. --- spyder/plugins/ipythonconsole/plugin.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py index d649a0b4894..d63e7281c68 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -559,6 +559,10 @@ def on_close(self, cancelable=False): return self.get_widget().close_all_clients() def on_mainwindow_visible(self): + """ + Connect to an existing kernel if a `kernel-*.json` file is given via + command line options. Otherwise create a new client. + """ cli_options = self.get_command_line_options() connection_file = cli_options.connection_file if connection_file is not None: From fc2a4bc2f9aca242b4d7aa3581c6705e2ed6270f Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Wed, 15 Jan 2025 12:24:18 -0500 Subject: [PATCH 9/9] API: Bump its version and add mention about change in Changelog --- changelogs/Spyder-6.md | 7 +++++++ spyder/api/_version.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/changelogs/Spyder-6.md b/changelogs/Spyder-6.md index b244e9c8e74..e6fd6ac3efc 100644 --- a/changelogs/Spyder-6.md +++ b/changelogs/Spyder-6.md @@ -1,5 +1,12 @@ # History of changes for Spyder 6 +## Version 6.0.4 (Unreleased) + +### API changes + +* Add `give_focus` kwarg to the `create_client_for_kernel` method of the + IPython console plugin. + ## Version 6.0.3 (2024/12/10) ### Important fixes diff --git a/spyder/api/_version.py b/spyder/api/_version.py index 9f0658846e2..80205c98f49 100644 --- a/spyder/api/_version.py +++ b/spyder/api/_version.py @@ -22,5 +22,5 @@ updated. """ -VERSION_INFO = (1, 2, 0) +VERSION_INFO = (1, 3, 0) __version__ = '.'.join(map(str, VERSION_INFO))