Load python script from harddisk in Slicers´s Python Interactor?

I was looking for that for ages about a month ago, so I just want to share the solution here (it may be obvious to you experts):

You can load and run a python script from the hard drive in the Python interactor (one-line command) with

    >> exec(open("filename.py").read())

This works in Slicer 4.11+ and seems to require Python3.

In Windows I used:

    >> exec(open(r"C:\Users\Yourname\Python\test.py").read())

Use slashes and do not forget the initial “r” …

Regards
Rudolf

3 Likes

Yes, that’s a very handy pattern. I use it a lot for quick testing. It has the advantage that any global variables define in the .py file are still around in the python terminal for further testing. I typically put that command with the path as a comment at the top of my script file so I can easily copy-paste it into the terminal when I start a new slicer.

1 Like

Haha that is exactly what I did, but forgot about my comment and searched the whole internet again today :slight_smile:

Dear Rudolf Bumm,
Amazing! I’ve try to find this solution more than year I think :0)
PS Also thanks for your MONAI label video presentation in summer '22!

1 Like

FYI, I’ve made some improvements in the past few days (will be available in the Slicer Preview Release from Wednesday):

  • Instead of exec(open(path).read()) you can hit Ctrl-r. Last path is remembered, so you can rerun a script using Ctrl-r, Enter.
  • If you copy-paste code into an empty line then instead of line-by-line execution, the entire code on the clipboard is executed at once (fixing the annoying indentation errors due to presence of empty lines).
2 Likes

I wish we could try to follow ipython hotkey behavior wherever possible. Ctrl-r is incremental backward search in ipython and other readline based prompt systems and it’s a commonly used shortcut. Many of the common keyboard shortcuts](Keyboard Shortcuts in the IPython Shell | Python Data Science Handbook) like Ctrl-e , Ctrl-y etc work already in our python interactor and if we are adding features it’s nice to follow existing patterns.

But I should also add that I like the new features - they will save a lot of time!

It should not be a problem to switch the Ctrl-R shortcut to a more standard one now, especially because previously this feature have been practically unusable. I can push an update today. What should be the keyboard shortcut?

Thanks!

I’m not sure what would be best. Let’s discuss at the developer meeting. There are some existing cross platform issues we could address at the same time and come up with a good scheme so that slicer hotkeys are as compatible as possible with other hotkey conventions.

I’ll also add Ctrl-l for clearing the terminal.

Ctrl-r for reverse search could be useful and it would not be hard to add, especially if we do it in a popup window instead of struggling with using the terminal window for it (IPython shell uses a special mode with the (reverse-i-search) prompt, while we could simply open a small search box).

We could also easily add Ctrl-k/u to cut content before/after the cursor, but it seems marginally useful (you can do Shift+Home/End, Ctrl-x instead).

However, most other shortcuts don’t seem to make sense for me, such as using

  • Ctrl-a/e instead of Home/End keys
  • Ctrl-b/f instead of left/right arrow keys
  • Ctrl-p/n instead of up/down arrow keys
  • Ctrl-d instead of Delete key
  • Ctrl-t for switching the previous two characters (really, a shortcut for this??)
  • Ctrl-y instead of Ctrl-v for pasting

Ctrl-r is the one I really care about. The rest are very common for people used to emacs-based features.
For me, even the chrome text window I’m using on mac allows Ctrl-[bfnpeatkyt] and that’s handy for people like me who are used to them from bash or ipython. Yes, even Ctrl-t to transpose two characters works, although I don’t use that one.

After discussion on the slicer dev call, we think Ctrl-g is not otherwise used and can be associated with the concept of ‘go’.