Are you still using Jupyter Notebook? Enjoy a comfortable Python life with Jupyter Life (.py) with VS Code ?!

Do you guys have a good Python life?

Suddenly, how do you usually deal with Python code?

I think that there are many people who normally create a .py``` file and run it, or write code in a code block using Jupyter Notebook or Jupyter Lab and execute it block by block. I will. Since I am a file in `.ipynb``` format that Jupyter can handle Python code materials distributed in university classes, I used to browse and execute code using Jupyter Notebook.

But everyone, don't you hate launching Jupyter Notebook and taking one or two browser tabs? Many Chrome users, in particular, think so.

I used to use Microsoft's Visual Studio Code (VS Code) to handle `` `.ipynb``` files because of that idea, but honestly it's hard to handle. It is not possible to intuitively restart the kernel or forcibly terminate it. (By the way, I recently learned that VS Code seems to terminate the kernel when the file is closed.)

So, in my case, I continued to use Jupyter Notebook in the end by first using Jupyter Notebook → trying to open `` `.ipynb``` with VS Code → returning to Jupyter Notebook. (I didn't use Jupyter Lab personally because I'm not used to UI.)

However, when I was looking at Microsoft's documentation, I found something interesting. I'm new to Python, so you might say "I've had it before!", But VS Code says that you can touch the code in a `` .py``` file like Jupyter. I knew.

If you say "I know the function! I've had it for a long time!", Please go back to the browser ... and if you don't, I hope you can see it till the end. (I would be grateful if you could do LGTM!)


What kind of function

The following is the image described in the Microsoft documentation, but please prepare a .py file and write `# %%` on the first line there. It is declared that this is the head of the code block in Jupyter, and if you write the code under it, it will work on VS Code like a single code block.

Microsoftのドキュメントから引用

Normally, as shown in the image below, there is a code block, and it is written in it and executed, but the same thing can be done with the `` `.py``` file.

Microsoftのドキュメントから引用

Benefits of this feature

I personally feel that there is only merit (laugh)

Advantage 1: File management is easier by using .py files

First, making it a `.py``` file makes it easier to manage on your Git system. In the case of the .ipynb``` file, the contents are arranged like JSON, so it is very difficult to check the difference on Git. There were times when the array of contents was corrupted and the `` .ipynb file couldn't be opened at all (although I self-healed). So, Git users can use `` `.py because it is easy to manage the `` `.py``` file, which has a simple content and contains only the source code. I personally think it's better to manage it.

Also, from the above points, you can browse the source code without opening the file in the Jupyter environment, so even if you want to share only this part in Slack, you can share it quickly.

Benefit 2: .ipynb_checkpoints are not generated

I think this .ipynb_checkpoints``` is the one that is sober and annoying. When executing the .ipynb``` file in this folder, the automatic save function is applied in Jupyter Notebook etc. and the temporary execution state is saved, but the destination is this `` .ipynb_checkpoints `` Folder. Especially when fetching files from a folder by machine learning etc., if there is `` `.ipynb_checkpoints```, it may cause a malfunction, so you need to be careful. Isn't this not generated only for the benefit, especially for those who are touching code such as machine learning?

Advantage 3: Fast response

When opening a .ipynb file in VS Code, it takes a long time after the user selects the file until the file is read and the code block is displayed. On the other hand, whether you open the `.py``` file with Jupyter Notebook or VS Code, the response when opening this file is very fast, so handle .ipynb``` with VS Code. I think there are many people who feel a hurdle to this.

Also, when using it personally, when I used `` `.ipynb``` in VS Code, it sometimes freezes or I do not know the execution status, but now I think that the response is quick and stable because such a thing has not happened (thanks to merit 4).

Advantage 4: Easy to restart / terminate the kernel

As you can see by looking at the first image again, if you use this function, the execution result will be output in the separate window on the right. It's actually like a Jupyter environment, but until now, if you want to restart / terminate the kernel, save the `.ipynb``` file once, close it, and reopen it. Must be. Also, as mentioned earlier in Merit 3, when opening `.ipynb, the response is often quite slow and frustrating ... but Jupyter in the `` .py file If you want to restart / terminate the kernel after opening the environment, just close the Jupyter window on the right! I think that stress will be reduced considerably.

Advantage 5: You can still use the same functions as Jupyter + You can also use useful functions with α

One of the merits of handling Jupyter files with VS Code is that you can check the storage status of variables. I think it's a simple and convenient function, but when you handle Jupyter with `.py```, you can check the variable storage status from the Jupyter window. There is also a section at the bottom of the Jupyter window called `Type code here and press shift-enter to run```, where you can quickly execute any code. It is convenient because it can be used when you do not need to write it in a file such as a print statement but want to check the situation.

Advantage 6: Easy to manage for each project

It's been a little long, but please stay with me for a while ...

VS Code has a feature called Workspace, which allows you to manage the environment and code for each project. The same is true for Python projects, and multiple projects are placed on one computer, such as the directory where the Python execution environment and code are placed, and the files that were opened the last time the workspace was opened are displayed again. If you do (I think most people do), this is a useful feature. Also, since the Python execution environment can be managed for each Workspace, isn't it a useful function especially for those who are using Virtualenv, Pipenv, etc.?

Benefit 7: Chrome tabs are not taken!

This is very important for heavy Chrome users ... The Jupyter Notebook page doesn't have tabs and the icons are even easier to see.

スクリーンショット 2021-01-04 194958.png

Eh, there are too many tabs? It's because of that, I'm sure.

Advantage 8: High affinity with WSL (Windows users only)

The last merit is the affinity with WSL. Many Windows users are already familiar with WSL, which has been booming recently. It is very useful to be able to set up a Linux environment such as Ubuntu in Windows. In this WSL and VS Code, there is an extension function dedicated to WSL so that it can be handled conveniently. By using this, you can operate the Python environment in the WSL environment more conveniently. Personally, this extension is important.

Disadvantages of this feature

On the other hand, there are some disadvantages, so I will introduce them properly.

Disadvantage 1: No execution result remains

As mentioned in Merit 1, the contents of the .ipynb file contains JSON-like array data. Therefore, unlike the `.py``` file, you can see the execution result, that is, the contents output by the print statement, the matplotlib image, the pandas table, etc. as they are. If you want to share the execution result with someone, it is better to pass it as a file in `.ipynb``` format.

Summary

The downside, I could only come up with one ... but personally, I'm very much looking forward to this feature of using the Jupyter environment with `.py``` files in this VS Code. It's very easy to set up (just prepare the file and add # %% ``), so why not give it a try?

Recommended Posts

Are you still using Jupyter Notebook? Enjoy a comfortable Python life with Jupyter Life (.py) with VS Code ?!
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Debug with VS Code using boost python numpy
Build a python execution environment with VS Code
What are you using when testing with Python?
How to make a Python package using VS Code
Python with VS Code (Windows 10)
Using Graphviz with Jupyter Notebook
Let's run jupyter natively supported by VS Code with python3.8
Debug Python with VS Code
Using jupyter notobook with VS Code (mac OS) (personal memo)
How to batch start a python program created with Jupyter notebook
Build a comfortable psychological experiment / analysis environment with PsychoPy + Jupyter Notebook
Create a simple Python development environment with VS Code and Docker
How to build Python and Jupyter execution environment with VS Code
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
Make a sound with Jupyter notebook
Try running Jupyter with VS Code
[VS Code] ~ Tips when using python ~
Install python with mac vs code
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
Prepare a Python virtual environment for your project with venv with VS Code
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
I was addicted to creating a Python venv environment with VS Code
Linking python and JavaScript with jupyter notebook
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Try to draw a life curve with python
Python (Windows 10) Virtual Environment / Package with VS Code
You can easily create a GUI with Python
Try using conda virtual environment with Jupyter Notebook
What are you comparing with Python is and ==?
Use Python in Anaconda environment with VS Code
Make your Python environment "easy" with VS Code
How to quickly create a machine learning environment using Jupyter Notebook with UbuntuServer 16.04 LTS
A memo that uses an interactive display mode like Jupyter notebook with VSCode + Python
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)