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

Add command line option to connect to an existing kernel #23130

Open
Social-Mean opened this issue Dec 1, 2024 · 4 comments · May be fixed by #23444
Open

Add command line option to connect to an existing kernel #23130

Social-Mean opened this issue Dec 1, 2024 · 4 comments · May be fixed by #23444

Comments

@Social-Mean
Copy link

Can I connect to an existing kernel when I start Spyder?

Now, Spyder can connect to an existing kernel by clicking the menu icon of the ipython terminal, and selecting connect to existing ipython kernel, and inputting the JSON file name of the kernel.

But this needs manually operation. I need a way to connect to the existing kernel through the command line or through python code.

For example,

spyder --existing-kernel kernel-123456.json

by command line options.

Is there a method to meet my needs, and if not, will it be supported in the future?

@dalthviz
Copy link
Member

dalthviz commented Jan 8, 2025

Hi @Social-Mean sorry for the late response and thank you for the feedback! I think currently there is not a way to do that, sorry! However, I'm not sure if this is something that could be eventually achieved with the new remote development logic that has been worked on Spyder 6 🤔

Just in case, is the use case described here something that could be eventually covered by the remote development work @ccordoba12 ?

@Social-Mean
Copy link
Author

Hi @dalthviz thank you for your comment.

I think it is necessary for me to tell the background of my raising this issue here.
I am developing a software and want to embed Python into the software so that users can call some functions to control my software to complete some functions. However, my software currently lacks a good editor. Since I usually use spyder as an editor to write Python code, I am thinking: If I could embed Spyder into my software, that would be awesome. The current way I think of using Spyder to send commands to my software is by connecting to the existing Python kernel of my software. But currently I have encountered a series of difficulties:

  1. Starting the Python kernel would block my software.
  2. I want to create a button in my software that, when clicked, will open the Spyder editor and automatically connect to the existing kernel of my software.

I'm not sure if this method is good or reasonable, after all, I'm not an expert on computers. This is just a method I came up with recently. If anyone has relevant experience in this area, please feel free to give comments and suggestions.

@Social-Mean
Copy link
Author

By the way, it seems to me that the remote development feature in Spyder 6 (via SSH) is connecting Spyder to a remote server kernel. But my software is a locally running software. I'm not sure if remote development works for me.

@ccordoba12
Copy link
Member

ccordoba12 commented Jan 9, 2025

@Social-Mean, thanks for providing a good motivation for your use case. I think this is something we could add support for, but only if you're willing to give us a hand with the implementation.

This is crude description of what you'd need to do:

  • Add the new command line option to spyder/app/cli_options.py. I suggest using --connect-to-kernel instead of the name you mentioned above.

  • Handle the option in the on_mainwindow_visible method of IPython console plugin with the create_client_for_kernel method:

    def on_mainwindow_visible(self):
    self.create_new_client(give_focus=False)

  • You can take a look at how we do something similar in the Projects plugin for inspiration:

    def on_mainwindow_visible(self):
    # Open project passed on the command line or reopen last one.
    cli_options = self.get_command_line_options()
    initial_cwd = self._main.get_initial_working_directory()
    if cli_options.project is not None:
    logger.debug('Opening project from the command line')
    project = osp.normpath(
    osp.join(initial_cwd, cli_options.project)
    )
    self.open_project(
    project,
    workdir=cli_options.working_directory
    )
    else:
    self.get_widget().set_pane_empty()
    logger.debug('Reopening project from last session')
    self.get_widget().reopen_last_project()

@ccordoba12 ccordoba12 changed the title Connect to an Existing Kernel When Start Add command line option to connect to an existing kernel Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants