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
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
16 changes: 14 additions & 2 deletions spyder/plugins/ipythonconsole/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,12 @@ 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, give_focus=False)
Social-Mean marked this conversation as resolved.
Show resolved Hide resolved
else:
self.create_new_client(give_focus=False)

# ---- Private methods
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -715,6 +720,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 Down Expand Up @@ -747,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, 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