New extension: ScriptEditor - a simple programming editor

ScriptEditor (formely named SlicerEditor) provides open-source Monaco programming editor as an extension. It is available for preview builds of 3D Slicer.

It allows auto complete, text highlighting and saving of the python scripts as text nodes into the scene. To execute a code, highlight in the editor, hit copy and then manually paste it into the python console. More information is available at GitHub - SlicerMorph/SlicerScriptEditor: a simple programming editor for Slicer based on monaco

We are looking for usability suggestions and improvement.

ScriptEditor was prototyped during the 41st Project Week by @pieper @oothomas and @smrolfe.

ScriptEditor is created and made available by funding from National Science Foundation (MorphoCloud: DBI/2301405; Imageomics Institute: OAC/2118240)

10 Likes

Looks nice and useful!
Thank you!

This is a very cool idea! :sunglasses::+1:

Thank you for the contribution, I’ll give this a try!

To avoid user confusion, we need to improve the extension name:

  • We already have “Segment Editor”, we’ll have a “Model Editor”, and probably other similar module and extension names will come up, so it would be inappropriate for an extension to claim to be The Slicer Editor. It has to be more specific.
  • We are also trying to reduce the number of extensions that use “Slicer” as name prefix, because it is harder to find extensions if most of them are called SlicerXyz. For example it can be quite annoying when you are trying to find an extension but you don’t remember the exact name and you need to browse two alphabetically ordered lists (the extension names wirh and without the Slicer prefix).

For example, ScriptEditor or PythonEditor extension names would work well.

4 Likes

yes, we have gone through some thoughts about the extension idea. There is nothing into the tool that makes it specific to Python (and in fact we do want to use it for R integration as well), so perhaps Script Editor or Code Editor although I am not sure it is a much of an improvement over Slicer Editor, it suffers from the same things.

On that note, I also do not like extensions being prefixed with Slicer. However, it think this is stemming from the extension submission reqiurements checklist, which requires the repo to be name prefixed with Slicer. 2nd item here

Obviously it is not mandatory to have the repo name reflect the actual extension name. But it is natural to do so. if my extension is Editor, then I wouldn’t want the repo to be SlicerEditor. And if I am going to call the repo SlicerEditor, I would rather not get confused where to use Editor and where to use SlicerEditor (in Cmake files etc) and simply call everything SlicerEditor too…

If we want more unique names, I think we should change that requirement.

1 Like

The recommendation is: Repository name is Slicer+ExtensionName. In this case: extension name is ScriptEditor, repository name is SlicerScriptEditor. I’ll clarify this further in the extension checklist.

1 Like

Great extension, thanks.

Here are some thoughts:

  • add a button to execute the code directly from the extension (and with a CTRL+ENTER shortcut) to avoid copy/paste that requires the mouse.
  • have the extension better integrated from the Python console: e.g. add a button in the console that opens the ScriptEditor extension panel (for discoverability). Or make as much as possible useful features (e.g. auto-completion) from the ScriptEditor extension available within the Python console
1 Like

Thank you for the feed back.

This is also what I want, but there is currently a usability issue. We tried directly sending the code (both via a run button and/or a shortcut). The issue is code is executed silently, and unless it produces a visual output (e.g., loading a data), or generates error, there is nothing in the python window. It is not even in the history buffer. This is a very confusing behavior, particularly for the people new to scripting/programming.

By making it an explicit action, our goal is to avoid this confusion. I know it is not convenient, so I would like to know what alternative there might be to have the executed code show up in the python console history buffer.

We tried directly sending the code

I guess that means the python console API should be improved… sorry I do not know much about it. Maybe @jcfr does.
Alternatively, you can “simulate” a user copy/paste with Qt. Maybe there are ways to creating synthetic QEvents that reproduce a copy/paste by a user…

Those are great suggestions @finetjul - agreed we should fix the issues rather than remove the features.

Also since this is based on the same core editor of VS code, there are lots of other features we could explore.

Thanks you, that will be very usefull
I think that another usefull extension to help beginner script in Slicer would be to have an easy way for a user to get the line of code executed when clicking a button/doing an action.
I read that such an extension is technically difficult but it would be very usefull for newcomers

This is specifically like what stata does with its command history? Is that the specific feature in another application that you wish Slicer had as well? Or is there a feature from a different application?

Looks great, looking forward to trying it!

I’m not familiar with stata so I don’t know. The thing I was thinking about is COMSOL that allow you to save your model as MATLAB code. That way if there’s something that you don’t know how to code you can just do it in the GUI, save it as matlab code and copy paste the code.
As someone who got into slicer scripting recently, that’s the feature I wish for the most. It would have been especially helpfull when I was just starting.

For a concrete example:
Last time I was loading a and generating picture of liver segmentation. I wanted to change the contrast of the CT to be the abdomen default value. This action takes only a few clicks but I struggled to find the corresponding line of code : slicer.modules.volumes.logic().ApplyVolumeDisplayPreset(VolumeDisplayNode, “CT_ABDOMEN”)

1 Like

The method described here works pretty well for me. It would be nice if it could be simplified but I don’t think there’s any completely automatic solution.

https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-find-a-python-function-for-any-slicer-features

2 Likes

Looks amazing! Looking forward to try it.

1 Like

Slightly revised version of this extension is now on the catalogue. The name was changed to ScriptEditor to avoid confusion with Segment Editor.

1 Like