nteract / hydrogen

:atom: Run code interactively, inspect data, and plot. All the power of Jupyter kernels, inside your favorite text editor.
https://nteract.gitbooks.io/hydrogen/
MIT License
3.93k stars 333 forks source link

connect to kernel started in different atom window #1578

Open Jogala opened 5 years ago

Jogala commented 5 years ago

Hi,

say I start a kernel in one atom window (instance), I get the message:

To connect another client to this kernel, use: --existing kernel-da00963e-ba55-484b-8b97-bd6f156046df.json

Now I start a second atom instance via new window.

How do I now connect to the already existing kernel?

Connect to already existing kernel does not work, hydrogen does not find any kernel.

I can easily connect an other ipython terminal to the already running kernel via

jupyter console --existing kernel-da00963e-ba55-484b-8b97-bd6f156046df.json ...

Why do I want this: I want to use both of my screen for coding, but atom does not allow to decouple a "open tab".

Thanks a lot in advance!

kylebarron commented 5 years ago

Having Hydrogen talk to itself in multiple Atom windows is hard.

You can get around this by using kernel gateways. Go through the remote server process to create a configuration file and a token, and paste:

[{
  "name": "Local Jupyter server",
  "options": {
    "baseUrl": "http://localhost:8888",
    "token": "my_secret_token"
  }
}]

Making sure to replace my_secret_token with your actual token.

Then connect to kernels by opening up the Connect to Remote Server command. You can connect to those among separate Atom windows.

Jogala commented 5 years ago

Thanks for your help, here is a summary how I got it to work:

I started a jupyter notebook:

jupyter notebook

In the output it will give you the information:

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
    http://localhost:8890/?token=dd8aee32b60ec284c78c598a4d62fb58e4a7ba6173b364c2

Then got to the browser that was opend by jupyter notebook, and open a new notebook:

New -> Python 3

Now open atom, go to hydrogen->setting-> Kernel Gateways and enter

[{"name": "Local Jupyter server", "options": {"baseUrl": "http://127.0.0.1:8890"}}]

Then open a python file and press "ctrl + shift +P" type connect and select

Hydrogen: Connect To Remote Kernel Select Local Juypter server

Choose Authenticate with a token, copy paste the token from above. (Make sure you do not by accident copy paste a line break!)

Select the name of the already opend notebook.

Now you are connected to the already existing kernel!

If you want to connect to the same kernel from a jupyter consol, type:

'#connect_info'

which will give you all the informatino neccessary:

{ "shell_port": 34967, "iopub_port": 49621, "stdin_port": 42383, "control_port": 57795, "hb_port": 51133, "ip": "127.0.0.1", "key": "610cb4d1-a7625d55f5c6f77f6f4d40ff", "transport": "tcp", "signature_scheme": "hmac-sha256", "kernel_name": "" }

Paste the above JSON into a file, and connect with: $> jupyter --existing or, if you are local, you can connect with just: $> jupyter --existing kernel-7918426d-09d4-48c0-826a-6d538dbd6101.json or even just: $> jupyter --existing if this is the most recent Jupyter kernel you have started.

Any suggestions?

kylebarron commented 5 years ago

Set the token in the configuration file, then it doesn't change for each session. No need to deal with the json kernel files. You can spawn a session from Hydrogen through the remote kernel menu.

Jogala commented 5 years ago

You mean setting the token in the configuration file, or configuration text edit of hydrogen atom settings? It is always a different token when I start the kernel gateway via jupyter notebook, hence setting it there would not make sens or?

By the way the text edit is always buggy. If I copy paste text into it and then try to edit it, text disappears randomly.

The JSON file is just if you want to connect with a terminal, without atom.

By the way, do you know how I can redirect the output (stdout and stderr) to a juypter console that is connected to the kernel? I really dislike the Hydrogen Output Area. It would be cool to have it working like with spyder. There the error messages are also 'clickable'. I guess I will open a new issue for that topic.

mfripp commented 5 years ago

@kylebarron Can you say what you mean by "Go through the remote server process"? I'd like to launch an IPython kernel inside a running Python app on the local machine (i.e., showing its namespace) and then connect to it via Hydrogen to test out new code as I write it. I can use IPython.start_kernel() to start a kernel in the Python app, but I can't see any way to connect to this from Hydrogen, even when I have the %connect_info settings. And I haven't found a way to launch a remote server from within the Python app.

kylebarron commented 5 years ago

I meant this: set the remote server url to localhost:8888 in Hydrogen's config. Then if you start Jupyter outside Atom with jupyter notebook, it'll start on localhost at port 8888 (unless you've changed it) on your local computer. At that point, connecting to Jupyter on your local computer is the same as connecting to a remote computer; you just have to connect to that port. So go through the process to connect to a remote server (the one that points to localhost:8888) and then it'll connect to the Jupyter started outside Atom.

I've never created kernels from the Python API, so I don't know if IPython.start_kernel() connects the process to localhost or not.

mfripp commented 5 years ago

@kylebarron, Thanks for this advice. I subsequently found similar instructions in the documentation. However, I'm still not getting this to work. I have set a token in ~/.jupyter/jupyter_notebook_config.py and run jupyter notebook from the command line. At this point I can connect to the notebook server from my web browser at http://localhost:8888/?token=<my_token>.

I have also setup the Kernel Gateways for hydrogen as [{"name": "localhost notebook", "options": {"baseURL": "http://localhost:8888", "token": ""}}]. However, when I try to connect to a remote gateway and choose "localhost notebook", I then get asked for a token or cookie to authenticate, and even if I provide the right token there, I get a message that "Connection to gateway failed".

Is this currently broken? I am using Hydrogen 2.8.0. I tried upgrading to a newer version last week, but that stopped Hydrogen from working, so I uninstalled and reinstalled 2.8.0. Could that be part of the problem?

wadethestealth commented 5 years ago

Instead of giving hydrogen localhost give it your localhost ip. Also Hydrogen 2.13 is fixed so I'd recommend that anyways @mfripp

kylebarron commented 5 years ago

@mfripp It's working for me on Hydrogen 2.13.0. You should check that your token is exactly the same. My config is

[{"name": "Remote: 8888","options": {"baseUrl": "http://localhost:8888","token": "<token>"}}]

and when using the token from

> cat ~/.jupyter/jupyter_notebook_config.py | grep c.NotebookApp.token

works fine for me

When you upgrade to a new version you'll probably have to rebuild the package again. Go to the settings or click the red bug in the bottom bar.