Some VS Code extensions support Jupyter.
This is an excellent thing that can be executed in Jupyter as a Cell by writing # %%
, but if you want to save it as .ipynb, copy and paste it to Jupyter's Cell on the browser every # %%
It is necessary and troublesome.
So I made a tool to convert .py to .ipynb that contains # %%
hello.py
#%%
print("Hello")
#%%
print("World!!")
If there is such a py
Convert to ipynb like this
This-> py2ipynb.py
Note that nbformat is required, but it should not be required separately as it will be installed together with pip install jupyter
.
For example, if you want to convert hello.py, give it as an argument and execute it, and hello.ipynb will be generated in the same directory.
python py2ipynb.py hello.py
After all, it's simple because I just read py line by line, find out if there is a # %%
at the beginning, and donbformat.v4.new_code_cell ()
.
It seems that the extension of VS Code is # %%
and it is separated as Cell, so I'm just looking there.
Recommended Posts