Select code and run IDE/code editor for Python - python

Is there an IDE/code editor for Python that allows a user to highlight and run a portion of syntax on the window like how SAS allows it in its UI? I'm aware of code blocks in Jupyter Notebook but I would like to know if there's something that's more flexible/free-form exists?

there something like this in Spyder you can divide your code in executable cells like in Jupyter or other notebooks and you can execute with Ctrl + Enter.
This hotkeys and shortcuts have been discussed here if you need more informations.
Stack answer on hotkeys in Spyder

Related

Does PyCharm have the feature for executing python script in parts/cells (like in VsCode - Run in interacting Window)?

when using VSCode, I can run python files in cells/parts as if it was Jupyter notebook, without actually having a notebook.
https://code.visualstudio.com/docs/python/jupyter-support-py
It means, you can run a python file, part by part, iteratively, like in Jupyter Notebooks, but in .py file.
it helps me to keep the code organized as a python file. (screenshot attached)
I wonder if the same feature exists in PyCharm. I couldn't find it.
I attach a screenshot of the feature in VsCode when I can run simple python file in interactive mode, part by part.
thanks.
The same feature exists in PyCharm.
Just right-click and select Execute selection in Python Console.
ok, answer found:
Magic Python in PyCharm
PyCharm supports Magic Python cell execution. To use Magic Python, you need to enable Scientific Mode in the View menu. You can then use #%% to indicate the start and end of cells. Individual Cells can be executed in the console by pressing CTRL+Enter.
In PyCharm, right-click on the root directory and select New > Python File. Give your file a meaningful name.
Enter
#%%
print("This is the first cell")
#%%
print("This is not executed when the first cell is run")
Enable Scientific Mode in the View menu.
Run the first cell by placing you mouse in the cell and pressing CTRL+Enter.
Run the second cell by clicking on the Play button (arrow) that appears in the gutter of the editor.
source(and credit to):
https://www.kevinsheppard.com/teaching/python/course/lesson-1/

How do I edit a cell in visual studio code interactive window

In vscode you can use "Jupyter: Create interactive window" to create an interactive window that resembles jupyter notebook. However, in the interactive window I am unable to do a couple of things that I enjoy from jupyter notebook, such as editing a cell or moving the cells around. Is there any way to do these things inside an interactive window, without having to create an .ipynb file?
Not a direct answer but: for editing you can use # %% to create 'cells' directly in the script you are working on. Then you can run the cells in the interactive window, this allows you to edit and re run. Explained in detail in the Jupyter code cells section from the Python Interactive Window Tutorial.
example from the Tutorial

Keyboard Shortcut to show documentation in VS code?

Like in Jupyter notebook shift+tab is the shortcut to show documentation and Tab key for suggestions (.ipynb). Similarly in VS code what is the shortcut if I am using it for Python?
In VS code, while using .JS suggestions are coming as I type but for .py its not showing any suggestions.
Looks like you need to install an extension for python for autocomplete to work. Try this link to install the required extension and get access to IntelliSense.
https://marketplace.visualstudio.com/items?itemName=ms-python.python
Then ctrl + space should bring up intellisense.

How can I create and execute a cell in Jupyter Notebook server from a separate process (e.g., an editor, vim)?

I want to call an api on a running jupyter notebook to create or edit an existing cell, and to run it. When this is done, the browser page should also update, to show both the contents of the cell, and the output.
The intended use case for this api is a plugin for the vim editor that will take highlighted lines and execute them. The motivation for using jupyter notebook is that will allow visualization of plots and images using matplotlib, even though both vim and the notebook will be running on a remote server. The user would have a browser page open to the notebook, and separately a terminal open potentially that is ssh'ed to the machine and editing python code.
VSCode does exactly this in its python interactive mode, but I prefer to use the vim editor natively than switch to vscode simply to be able to do this.
There are several plugins being actively developed for this, and without having used them, it seems only vimpyter interacts with jupyter on the browser. Others like jupyter-vim and vim-ipython open the notebook inside a vim buffer.
If you can live without inline plots I find confortable having a neoterm terminal buffer where I can send lines to and from IPython.
You could use vscode's built in jupyter notebook support to convert the notebook into a python file
this is not an exact answer, but I've written a couple Jupyter plugins that may be helpful to you in this endeavor, JupyterGraffiti and JupyterTerminals. Graffiti has an API you can call to execute some of its functions, and it supports inline terminals you can control. JupyterTerminals provides only those inline terminals, but also has buttons you can add to control them. You could start and run vim inside these terminals. (If it's useful an API could be added to the JupyterTerminals plugin like Graffiti has.)

VSCode Jupyter: is there a way to make python interactive window work as default console?

I've recently switched to VSCode, and am wondering if there's a way to make the Python Interactive Window from the Jupyter support in VSCode work like the console in Spyder where I just have to select code and press ctrl+enter to send it, without having to create cells everytime.
For now I'm resigned to work with the Terminal until my code is clean and then create a cell when I have reusable code, and would like to just work directly with the PIW.
You can always change the default console setting by:
Opening the Command Palette (⇧⌘P)
Typing "Preferences:Open Settings (JSON)
Edit this line:
"python.dataScience.sendSelectionToInteractiveWindow": false
You should be able to do this with the latest python extension. The select the code you want to execute and press shift-enter. Is that not working?
For me (now) interactive mode runs after setting "jupyter.sendSelectionToInteractiveWindow": true
#FranciscoRZ. You should have seen a popup option for this, but if it didn't come up for you it can just be manually set in VSCode options. Just change this guy here:
Python->Data Science: Send Selection To Interactive Window
That should get you what you are looking for.
OP's Note: Accepting this answer because it will be the right anwser starting with the February release of VS Code Python
If you have the notebook saved as a python percentage script (which is more git friendly)
each "cell" will be delimited by # %% and the default run command is "Shift+Ctrl".
Later once you are working in the interactive window, If you want a particular cell you wrote on the fly to be in you script, there is one button which says "Paste code into file" right next to the recently executed cell in the interactive window.
And in case you are using the notebook for the sake of being able to later export it to html or pdf, once executed in the interactive window, there is an export button as well.

Categories

Resources