Skill - Using Jupyter Notebooks in Visual Studio Code
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
In this post we are going to learn about how to use jupyter notebooks and add jupyter notebook features to python files in Visual Studio Code
Why the Jupyter Notebooks approach
- Jupyter notebooks is the way to go approach for experimenting and debugging with python code or execute code in small parts while developing it.
- Each part of a code is called a cell in a Jupyter notebook
- Jupyter notebooks can also be used to graphically visualize the variables using the variable explorer.
- One more use case of jupyter notebooks is that you can load data into variables once, and then play with them as many times as you wish by changing the code.
This is particularly useful if you load huge data into your variables
Add Jupyter notebook features to existing python code
- As shown in the image above, just add
# %%
in the line above your code to make it a jupyter notebook cell - The cell controls like
Run Cell
,Run Above
will automatically displayed by Visual Studio Code - Upon running the cells in python file, we will get to see output in the right-side output pane. An example image is shown below
Variable Explorer to examine the variables graphically
- Once the code cells are executed, the variables will be persisted
- These can be seen graphically using the variable explorer button as shown in the below image
- Variable Explorer is very handy if you want to examine the variables in the middle of complete code execution, just like debugging
Using jupyter notebooks in Visual Studio Code
- Jupyter Notebooks are saved with
.ipynb
extension - To create a new ipynb file, open command pallete using
Ctrl+Shift+P
and select the commandCreate New Blank Jupyter Notebook
- The cell user interface controls of ipynb file are shown in the below image
- Even plotting outputs are displayed in the cell output section. An example cell output with plots can be seen in the image below
Convert a jupyter notebook to a python file
- Open a jupyter notebook ipynb file in Visual Studio Code
- Click the ‘convert and save to python’ button as shown in the image below
Video
Video on this post can be seen here
References
- Official tutorial page - https://code.visualstudio.com/docs/python/jupyter-support?ocid=AID2463683&wt.mc_id=ai-c9-sejuare
- Video by Microsoft - https://youtu.be/_C0vbLV6WdA
Comments
Post a Comment