Is there a way to send python scripts from a text editor or an IDE directly to Slicer’s python? Copy and paste occasionally fails or creates weird issues (I am on remote connection). I am not a big fan of jupyter notebooks either.
I thought about various ways but in the end I like copy-paste the best. With current slicer it doesn’t become part of the command history and it stops after the first error so I haven’t had any trouble. What trouble do you have?
Sometimes I also put a comment like this at the top of the a file and then I have the exec line in my python history so I can just hit up arrow until I get to it and hit return to re-execute the file.
"""
filePath = "/Users/pieper/slicer/latest/OBJFileReader/textureImport_test.py"
exec(open(filePath).read())
"""
It would be possible to make something more integrated to an editor, like programming it to post the python code to the WebServer’s exec endpoint or by writing a snippet to a special file that Slicer could monitor with a QFileSystemWatcher, but in the end none of that seemed better than copy-paste for my use cases.
What I have been tempted to do is add a way to search the Slicer python console history to re-execute lines or previously pasted blocks. It would be especially nice if this persisted across runs. Since the data we need is in the log files this wouldn’t be hard to implement but I’ve never gotten around to it.
I have first have to login into a citrix server and then launch turbovnc to connect to our lab server interactively. Occasionally copy and paste functionality gets confused about what is being copied and pasted (local to remote, remote to remote, remote to local, local to local), and to get things back in order I have to restart both sessions.
in Rstudio there is an option to highlight a piece of code and direct it to the active terminal window (you can have as many terminal windows you like). I was hoping there might be a way to start Slicer from command line such that text sent to that terminal find its way to the Slicer’s python environment. So something like this:
Jupyter notebook can be misused many ways, but it is the ideal tool for replacing copy-pasting of code snippets. It is much easier than carefully selecting text in an editor.
If you want to run Python scripts from files then you can use the Ctrl-G shortcut in Slicer. First time you use it you select a Python script to run. Next time you don’t need to select a file, just press hit Ctrl-G and then Enter
(as the previously selected path is used by default).
I guess everyone can develop what works best for them and maybe we can add more hooks in Slicer to make things easier. I find copy-paste to be such a universally supported and useful feature that I’ve never felt the need to develop anything further. For example I’m frequently testing the same code on mac/windows/linux and it would be a waste of time if I needed to manually configure each editor. Instead I just use vi (gvim typically) where it’s easy to use the keyboard to select blocks of code and then paste them to Slicer.
That said, if you are working on a module, then the Reload and Reload and Test buttons can be very valuable.
I know mine is a corner case, but sometimes the copy/paste buffer gets confused very quickly and I have to reset sessions every few minutes.
When I am working locally copy/paste is just fine, and yes the ability to reload the modules is also great. That’s what I was sort of wondering for python scripts. Is there any open-source, simple programming editor that can actually be incorporated to Slicer as an extension to facilitate that?
@lassoan is there a way to have jupiter notebooks to save things as at least mostly pyton + markdown, as opposed to JSON? JSON just makes interacting with the code on github much more difficult.
Even though jupyter notebook (.pynb) file content is in json, most editors, including GitHub’s online editor allows viewing and editing the actual formatted content. Example online viewers and editors:
- Github online in-place viewer
- Github online in-place editor
- nbviewer for very fast viewing of large notebooks
Interestingly, a new notebook variant, marimo, uses Python code instead of json as source format. However, I’m not sure if it can run code inside the Python environment in the running Slicer process.
I’m not sure about “simple” but at least you can run vim in Slicer with this wasm compiled version:
>>> ww = slicer.qSlicerWebWidget()
>>> ww.url = "https://rhysd.github.io/vim.wasm"
>>> ww.show()
I didn’t spend much time on it, but I’m sure it’s possible to add hooks to move code in and out of Slicer using the javascript interface from python.
This seems to work as well, and may be simpler:
>>> ww.url = "https://ace.c9.io/build/kitchen-sink.html"
THis is interesting, I will give it a try.
Meanwhile, what I was thinking is that we already have a Texts module in Slicer. Would it be possible it to support code highlighting and indentations and have route the code the python console?
Yes, that could work too. Interesting that this approach would save code snippets along with the scene, and that could be useful for sharing work, something like what people do with notebooks.
+1
I think this would be great actually. Having the code baked into the scene would be even more useful than having in an external notebook (particularly for small projects).
Turned into a feature request for votes: Support python text highlighting in Text module