Python Jupyter Notebook with VSCode

If you are a Python developer, I highly recommend watching this video from Microsoft VS Code Team.

Many Python developers use Jupyter notebooks during development. Jupyter notebooks are usually easy to debug because they can be executed block by block in a web browser. Microsoft has finally improved the Python extension to enable running Jupyter Notebooks in VSCode. VS Code's Python extension allows you to save files created with Jupyter Notebook (ipynb) as files with the py extension. And the saved py extension file is compatible with Jupyter Notebook in Vs Code. This feature is amazing.



If you watch this video, you will have no choice but to turn your Python development tools into VSCode.

Install Python extension

 First in the VSCode, install python extension from Microsoft.


Then open or make a new python file. You can see that the Python source code is syntax highlighted.
And at the bottom of VSCode you can see your current Python. If you are using multiple Python, choose one here.



Instal Jupyter Notebook

Press "Ctrl + Shift + P", and enter "Create new Blank Jupyter Notebook."


If you havn't installed Jupyter Notebook before, VSCode asks you if you want to install it. If you select "Install", VSCode installs all required packages for you.

Now you are ready to use Jupyter Notebook in the VSCode.


The best thing about using Jupyter Notebook in VSCode is that you can save the file as a py file and use it directly in Jupyter Notebook. In general, Jupyter Notebook has a big advantage over using the ipynb extension.
Jupyter Notebook files from VSCode can be used directly as Python scripts or reworked in Jupyter Notebook.


Do your Jupyter Notebook programming

Now you can use Jupyter Notebook for your python programming like this.



Save the file you have created

Now it's time to save your output. As I said before, you can save in Jupyter Notebook format or python extension format.


If you are familiar with using Jupyter Notebook, saving and reading ipynb files is easy to understand.
Saving it as a Python file and linking it back to Jupyter Notebook is quite appealing, so I'll explain it a bit more.
I saved above file as "numpy_test2.py". Then you can see the file like this.


Even though it is a python file with a py extension, you can see that the code works block by block like the ipynb extension. Clicking on the desired command in the block will open a new window with the result. This is possible because it preserves the existing Python syntax and puts commands that can only be recognized by VSCode in comment statements.
If you open the above file in another editor, it looks like this:



In other words, the "# %%" annotation plays an amazing role. This comment is ignored by the Python interpreter, so running it using Python commands should be fine.


댓글

이 블로그의 인기 게시물

VSCode - Lua programming and debugging

Remote C/C++ Debugging with VSCode #1 - Installation and simple debugging

Remote Python Debugging with VSCode