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)) diff --git a/spyder/app/cli_options.py b/spyder/app/cli_options.py index 9dee61ab853..aa1cb2feefb 100644 --- a/spyder/app/cli_options.py +++ b/spyder/app/cli_options.py @@ -157,6 +157,16 @@ 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=( + "Connect to an existing kernel whose info is available in a " + "kernel-*.json file" + ) + ) 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..d63e7281c68 100644 --- a/spyder/plugins/ipythonconsole/plugin.py +++ b/spyder/plugins/ipythonconsole/plugin.py @@ -559,7 +559,19 @@ 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) + """ + 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: + self.create_client_for_kernel( + self.get_widget().find_connection_file(connection_file), + give_focus=False, + ) + else: + self.create_new_client(give_focus=False) # ---- Private methods # ------------------------------------------------------------------------- @@ -715,6 +727,7 @@ def create_client_for_kernel( sshkey=None, password=None, server_id=None, + give_focus=False, can_close=True, ): """ @@ -736,6 +749,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 @@ -747,7 +763,13 @@ 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..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, 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 +1773,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, )