Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add command line option to connect to an existing kernel at startup (IPython console) #23444

Merged
merged 9 commits into from
Jan 15, 2025
7 changes: 7 additions & 0 deletions changelogs/Spyder-6.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spyder/api/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
updated.
"""

VERSION_INFO = (1, 2, 0)
VERSION_INFO = (1, 3, 0)
__version__ = '.'.join(map(str, VERSION_INFO))
10 changes: 10 additions & 0 deletions spyder/app/cli_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -715,6 +727,7 @@ def create_client_for_kernel(
sshkey=None,
password=None,
server_id=None,
give_focus=False,
Social-Mean marked this conversation as resolved.
Show resolved Hide resolved
can_close=True,
):
"""
Expand All @@ -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
Expand All @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down
Loading