Python interactor new features [load script, recent scripts]

Hi,

The Python interactor is not user-friendly at all and it would be great if it has a section in GUI for choosing and loading a script (.py file) as well as a history of recently loaded scripts (e.g. right-click on the Python interaction window and choosing “load script” or select from “recent scripts”; or add these options in File>).

The following topic is a solution but is not user-friendly.
Load python script from harddisk in Slicers´s Python Interactor? - Support - 3D Slicer Community

Thank you in advance.

+1 for improving the python console. I’d also like to see it save/restore history and support searching history (like Ctrl-R in bash). In fact why not let you search through the histories of all sessions. Also tab-completion of file paths would be great. Combining traditional command line features with some nice menus and dialogs could make things a lot easier.

I’ve thought about working on these features from time to time, but always find that workarounds like exec(open(path).read()) that are good enough once you know them.

1 Like

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. This feature also somewhat serves a history of recently loaded scripts (the displayed native file selector can show recent files in the current folder, favorite folders, etc.)
  • If you copy-paste code into an empty line then instead of line-by-line execution, the code is run at once (fixing the annoying indentation errors due to presence of empty lines).

Command history is preserved in the application log, so you can always look up all commands of the last 10 or so sessions. It would be very easy to save the command history and reload them when Slicer is restarted. However, I don’t feel a motivation to implement it, because if I want to re-run some code many times then I usually put it into a Jupyter notebook (Slicer can run as a Jupyter kernel) or I put it into a scripted loadable module template (if I want to do interactive debugging in a Python IDE). If you want to rerun a set of scripts then it is also very easy to drop a few buttons on a scripted loadable module template and make those scripts run at the click of a button. You can also assign keyboard shortcuts to loading specific Python files and run them.

1 Like