wuub / SublimeREPL

SublimeREPL - run an interpreter inside ST2 (Clojure, CoffeeScript, F#, Groovy, Haskell, Lua, MozRepl, NodeJS, Python, R, Ruby, Scala, shell or configure one yourself)
https://github.com/wuub/SublimeREPL
Other
2.14k stars 312 forks source link

Attach IPython REPL to already-running kernel #162

Open MattDMo opened 11 years ago

MattDMo commented 11 years ago

Hi wuub, I'm starting to use the IPython Notebook more, and I'd love to be able to share data between it and SublimeREPL, but as far as I can tell there's no way to direct ipy_repl.py to connect to an existing kernel on the system, since you can't pass it command-line parameters, and I don't know enough about the IPython internals to call it directly. Ideally, you could send ipy_repl.py an option in the config file to pop up a box where the kernel ID could be entered, or starts a new one if nothing is entered.

What do you think?

MattDMo commented 11 years ago

Just as an expansion, optionally you'd want to enter an IP:port combination as well, since, if I can figure out how to do it, I might run the notebook server on my web host, so I can access it from home and work.

wuub commented 11 years ago

Notebook integration is definitely on the radar, but w/o ability to somehow embed IPython's rich output (think matplotlib graphs) it seems a bit too limited. Do you think it will be useful even in the plain text mode?

MattDMo commented 11 years ago

I think it would, since I'm mainly interested (at the moment) in having access to the command history, variables, functions, etc. I have plotting working well on Windows, Mac, and Linux, so I can always replot if I want to. Mainly, I'd just like to have a single server running that I can connect to from wherever, even if I have to restart SublimeREPL.

rornor commented 11 years ago

I wanted this too. Here is one possible approach. initialize() function from IPythonConsoleApp (ZMQTerminalIPythonApp) class accepts arguments, as those passed on CLI. So I changed SublimeREPL\config\Python\ipy_repl.py as with this patch:

--- ipy_repl.bak        2013-08-17 15:50:24.406250000 +0200
+++ ipy_repl.py 2013-08-17 16:22:31.750000000 +0200
@@ -49,7 +49,13 @@

 embedded_shell = ZMQTerminalIPythonApp(config=cfg, user_ns={})
-embedded_shell.initialize()
+
+if sys.argv[-1] == "--existing":
+    from IPython.lib.kernel import find_connection_file
+    latest_conn_path = find_connection_file('')
+    embedded_shell.initialize([sys.argv[-1], latest_conn_path])
+else:
+    embedded_shell.initialize()

 if os.name == "nt":
     # OMG what a fugly hack

with idea to pass additional argument to SublimeREPL IPython command, if attaching to existing kernel is wanted action, thus SublimeREPL\config\Python\Main.sublime-menu:

--- Main.bak    2013-08-17 16:14:12.375000000 +0200
+++ Main.sublime-menu   2013-08-17 16:21:33.421875000 +0200
@@ -78,6 +78,28 @@
                             "SUBLIMEREPL_EDITOR": "$editor"
                         }
                     }
+                    },
+                    {"command": "repl_open",
+                     "caption": "Python - IPython (reuse latest kernel)",
+                     "id": "repl_python_ipython_existing",
+                     "mnemonic": "p",
+                     "args": {
+                        "type": "subprocess",
+                        "encoding": "utf8",
+                        "autocomplete_server": true,
+                        "cmd": {
+                            "osx": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py", "--existing"],
+                            "linux": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py", "--existing"],
+                            "windows": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py", "--existing"]
+                        },
+                        "cwd": "$file_path",
+                        "syntax": "Packages/Python/Python.tmLanguage",
+                        "external_id": "python",
+                        "extend_env": {
+                            "PYTHONIOENCODING": "utf-8",
+                            "SUBLIMEREPL_EDITOR": "$editor"
+                        }
+                    }
                     }
                 ]}
             ]

and SublimeREPL\config\Python\Default.sublime-commands:

--- Default.bak 2013-08-17 16:14:06.375000000 +0200
+++ Default.sublime-commands    2013-08-17 16:21:03.796875000 +0200
@@ -34,5 +34,13 @@
             "id": "repl_python_ipython",
             "file": "config/Python/Main.sublime-menu"
         }
+    },
+    {
+        "caption": "SublimeREPL: Python - IPython (reuse latest kernel)",
+        "command": "run_existing_window_command", "args":
+        {
+            "id": "repl_python_ipython_existing",
+            "file": "config/Python/Main.sublime-menu"
+        }
     }
 ]

So I have additional command in ST command palette, to attach to running IPython kernel.

I noticed small issue however, which seems like possible SublimeREPL bug, so I'll ping the dev @wuub - problem is that on closing "normal" (not connected to existing kernel) IPython SublimeREPL, it doesn't clean the connection file from IPython security folder, leaving the ghost connection file which doesn't describe any running IPython instance. Because of this, trying to launch for example ipython qtconsole --existing from command prompt, will block starting the app until $HOME\.ipython\profile_default\security\*.json is cleaned from ghost json files.

wuub commented 11 years ago

, it doesn't clean the connection file from IPython security folder

I can confirm this. I wonder if there is a cleanup command I am supposed to run after embedded_shell.start()

Eh, diving into IPython's source code again I guess.

rornor commented 11 years ago

@wuub, there is cleanup function as cleanup_connection_file() in couple of IPython modules

MattDMo commented 10 years ago

@rornor since wuub has SublimeREPL development on hold for now, do you have any idea how to implement cleanup_connection_file()? I'm fiddling around with it, but can't seem to get it to work...

rornor commented 9 years ago

@MattDMo It seems that with current IPython version (2.2) you don't have to deal with cleaning connection files - IPython knows if connection file points to live kernel. Of course, it is nice gesture to clean after self, but I haven't looked into it