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

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

Related

Select code and run IDE/code editor for 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

Python debugging and breakpoint

I usually use Matlab for processing results. However I'm required now to use Python. I want to set a breakpoint in a Python Jupyter notebook to go through functions. I was able to set up breakpoint but I can't actually go line by line debugging with this breakpoint. In Matlab I used to press F10 to do this.
In JupyterLab with the Xeus Python kernel (often tagged with 'XPython' when you are choosing the kernel) you can use the visual debugger. The run-next-line feature is the 'Next' button in the 'CALLSTACK' pane of the debugger panel. See the animation here for the 'Next' tooltip when hovering. And, although not featured in that animation, the tool tip actually shows 'Next (F10)' presently in my browser.
You can try it by clicking here. The session that spins up starts with a notebook open that has a nice introduction and guide to the features.
More can be found about the visual debugger here.
Current 'default' launches via MyBinder, have the Xeus Python kernel and the debugger available already. You can run the following code in a cell in any of those sessions to pull in the debugger demonstration notebook.
!curl -o debugger.ipynb https://raw.githubusercontent.com/jupyterlab/debugger/dfd6bf9d51a7a0cd78ca54f4173fcf527bd4d7fd/examples/index.ipynb
You'll find when you open it, that notebook also is set to trigger opening with the correct kernel necessary for debugging selected. Note that won't always be the first choice for when you start a new notebook.

Missing 'Find in Selection' in VS Code when editing Jupyter Notebooks

The 'find in selection' button is missing from VSCode when working with Jupyter Notebooks. It slows down development so I would like to ask if anybody knows how to activate it?
First image shows the search/replace when in a python file. Second image shows the missing button when in a notebook.
Python file:
Jypyter Notebook:
The .ipynb file is written by a kind of markdown.
The only way is to rightclick the tab and reopen .ipynb by builtin text editor for now.
As Jan 2022, within a cell in a jupyternotebook you can search and replace in selection when pushing F3. As comment before, this function is not available when using Ctrl + H
More info :
How to find and replace text in a single cell when using Jupyter extension inside Visual Studio Code
https://github.com/microsoft/vscode/issues/141493
https://github.com/microsoft/vscode/issues/121218

"New Console for notebook" for jupyter notebook in visual studio code

Jupyter lab has this feature where I can have a ipython console for every notebook I have opened. Whenever I run a cell inside this notebook, the console will have all the variables defined and modules imported corresponding to notebook. In addition, we can run extra commands and helps in debugging at times. Is there a similar feature in VS code? I really like it and would like to move completely to vs code. Python interactive command line in vscode is the closest to this that I found. However, it is not attached to the notebook and I have to run all the code inside the notebook which is a bit tedious.
I believe this would work Connecting a terminal to an existing kernel
However, you're likely looking for a way to do this within VS code. You might be able to do this by running %connect_info in a cell, starting a terminal, and then running the appropriate jupyter command.
Something like so:
jupyter console --existing kernel-2c0993da-95c7-435a-9140-118c10d33e1a.json
If you're refering to .py files you can do that the same way you would in pycharm.
First, you need to put a breakpoint in the code:
Them you run the code with the debugger:
Then, when the code reaches the breakpoint, you will be able to play with the variables, like the Jupyter terminal:
I also like to have a JupyterLab-style console open that is connected to a notebook. This is my workaround in order to achieve this in Visual Studio Code (at least it works when my kernel is a remote Jupyter session).
Suppose your notebook is called hello.ipynb.
Create a dummy file called hello.py.
Open hello.py, right-click in the code window and choose Run Current File in Interactive Window. This opens the JupyterLab-style console.
Change the kernel for the interactive window to the same kernel that the notebook hello.ipynb is using.
(Optional) Close the hello.py tab since it is not needed.
Now I have an interactive window sharing everything with the notebook.

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