hoangKnLai / vscode-ipython

VSCode Extension integrating Editor with IPython console.
MIT License
18 stars 5 forks source link

Run multi-line selection or section without %run #58

Closed dmn-seasony closed 4 weeks ago

dmn-seasony commented 3 months ago

If I execute the ipython.runSelections command with a single line selected, the line is directly copied to the terminal and executed. However if I select multiple lines or use the ipython.runSection command, the code instead gets saved to a file (~/.vscode/ipython/code.py) and executed with the %run -i magic.

However I would like to be able to directly see the lines that are executed in the terminal, and to see the return value of the last line (without explicitly using print), same as if I had typed the code directly into the terminal. It works fine to just copy the code and paste it into the terminal but that is cumbersome to do manually. Would it be possible to do that with this plugin?

hoangKnLai commented 3 months ago

Hi. I'll take a look but here some past solution that didn't work out:

As for printing the last line. IPython terminal by default doesn't print unless the code explicitly calls print() or a variable by itself being called in the terminal. Changing IPython terminal behavior is out of scope of this extension. So a compromise is that you can highlight the variable (double click or other selection method) and Run Selection (F9) handles the sending of text to the terminal. Also, consider the following:

for i in range(4): 
    ii = i + 1

if False:
   ii + 3

How should ii be printed? Every line or just last line? It is ambiguous and there will be use-case where neither one works. In the if case, it should never be printed but to know when to print would requires knowing the code / sequence of event vs. current workspace variable; i.e., a real big mess! So, for now, the likely best apparent answer is that you as a developer should explicitly use print when appropriate and use highlight + F9 to print specific workspace variable you like to see.

Onto the seeing the exact code being executed question. One way is to open the code.py in an editor like below. VSCode update code.py and you can see the changes live.

image

Hope this helps and thank you for posting! Any suggestion or comment are always welcome and appreciated.