Using Jupyter Notebooks in Visual Studio Code

jupyter_notebook_vs_code

Skill - Using Jupyter Notebooks in Visual Studio Code

Table of Contents

Skills Required

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

jupyter_notebook_in_vs_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

jupyter_notebook_py_file

  • 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
    jupyter_notebook_kernel_output

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
    jupyter_notebook_variable_explorer
  • 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 command Create New Blank Jupyter Notebook
    jupyter_notebook_create_new
  • The cell user interface controls of ipynb file are shown in the below image
    jupyter_notebook_ipynb_file
  • Even plotting outputs are displayed in the cell output section. An example cell output with plots can be seen in the image below
    jupyter_notebook_inline_output

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
    jupyter_notebook_convert_to_python

Video

Video on this post can be seen here


References

Table of Contents

Comments