/ JUPYTER-NOTEBOOK, VSCODE, PYTHON

My First Jupyter Notebook on Visual Studio Code (Python kernel)

In this blogpost, I will share the steps that you can follow in order to generate and use a Jupyter Notebook on Visual Studio Code (VSCode). During the development of this blogpost I used a Python kernel in a Windows computer.

Pre-requisites

In order to complete the steps of this blogpost, you need to install the following in your windows computer:

  • Visual Studio Code: you can find the steps to install it here.
  • Python Extension for Visual Studio Code: you can find the steps to install it here.
  • Python Interpreter: you can find the steps to install it here.

Setting Up a Jupyter Notebook on VSCode

1) Creating a Workspace on VSCode

  • The first step of this guide will be to create a folder to store all our Jupyter Notebook projects. For this blog post, I am creating the folder Jupyter-Notebooks in the following directory C:\Users\User1\Documents\Cyb3rPandaH.

  • Open the VSCode application.

  • Go to the File menu and select Open Folder.

  • Search for the Jupyter-Notebooks folder that was created previously and click on Select Folder.

  • By opening the Jupyter-Notebook folder, it becomes your workspace within Visual Studio Code. We are now ready to create our first Jupyter Notebook file.

2) Creating a Jupyter Notebook

  • Click on the New File icon, next to the name of our workspace.

  • Type a name for your file using the ipynb extension and press enter.
  • By using this extension, we are telling VSCode to process this file as a Jupyter Notebook file.

  • After hitting the Enter key, you will see a message at the bottom left of your screen that says Python Extension Loading.

  • In additon, because this is your first time creating a Jupyter Notebook using VSCode, you will get an additional message that says Python requires ipykernel to be installed. This message is telling us that we need to install a Kernel.
  • A kernel is a process that runs interactive code in a particular language such as Python or R and return output to us.
  • In this example we are using Python language, therefore you need to install the ipykernel Python kernel for VSCode. Click on Install.

  • VSCode will open a terminal and install the ipykernel component for you.
  • Wait for the installation process to complete.

  • After the installation process is completed successfully, VSCode will connect your Jupyter Notebook to the ipykernel Python kernel.

  • You can close the VSCode terminal window by clicking on the Close Panel button that is located at the top right of the terminal window.
  • The installation process for the ipykernel Python kernel will not be required when creating future Jupyter Notebooks because we just installed it in VSCode.

  • Awesome!! VSCode has created a Jupyter Notebook server locally in your computer. We now have our first Jupyter Notebook file created with the Python extension loaded and connected to a Python Kernel.

Basic Interaction With Jupyter Notebooks

1) Inserting and Deleting Cells

  • You can add a new cell by clicking on the Insert Cell Bellow ➕ (At the bottom left of each cell) button or by hitting the B key one time after selecting a cell.

  • You can delete a cell by clicking on the Delete Cell button 🗑️ (At the right of each cell) or by hitting the D key two times after selecting a cell.

2) Switch Cell Content Type

  • By default, the content type of a cell is code. You can change the content type to narrative text by clicking on the Change to Markdown button M⬇️ (At the top left of each cell).

# This is my first Jupyter Notebook
Thank you!

  • You can change the content type of a cell back to code by clicking on the Change to Code buttom { } (At the top left of each cell).

3) Running Markdown and Code Cells

  • After adding content to a Markdown cell, you can get the output of this type of cell just by clicking on anywhere outside the cell or by hitting Shift + Enter keys combination (Using the second option will also add a new cell below).

  • After adding content to a code cell, you can get the output of this type of cell just by clicking on the Run Cell button ▶️ (At the top left of each cell) or by hitting Shift + Enter keys combination (Using either both options will add a new cell below).
# Importing Python library
import pandas as pd

# Initializing a Dictionary
data = {'User': ['Cyb3rPandaH','Cyb3rCuy'], 'Country': ['Peru','United Sttes']}

# Creating a Pandas Dataframe
pd.DataFrame.from_dict(data)

Importing Non-Installed Python Libraries

When trying to import a non-installed Python library, we will get an error similar to the one showed in the image below.

# Importing Python library
import requests

You can find all the steps required to install a Python library using pip in VSCode here. After completing these steps, you can try and run the code within the cell again.

References

  • https://code.visualstudio.com/docs/python/jupyter-support
  • https://blog.openthreatresearch.com/installing_vscode_windows
  • https://blog.openthreatresearch.com/installing_python_extension_vscode
  • https://blog.openthreatresearch.com/installing_python_interpreter
  • https://blog.openthreatresearch.com/installing_python_library_vscode