# Jupyter Notebook 101

### What are Jupyter Notebooks?

A Jupyter Notebook is a document that helps with creating and sharing computational documents. Just like Google Docs are an online version of Microsoft Word, Jupyter Notebooks are documents that can execute code online.  Notebooks have the file extension .ipynb (**I**nteractive **PY**thon **NO**tebook), and each cell of code is executed in a REPL (Read-Eval-Print-Loop) fashion.&#x20;

> **What is REPL**? Read-Eval-Print-Loop is an interactive programming environment.&#x20;
>
> 1. Read in: The user creates some code and sends it to be processed.
> 2. Eval: The code is evaluated.
> 3. Print: The results are printed out for the user.
> 4. Loop.  The ability to repeat steps 1-3 as an iterative method. <br>

[Jupyter](https://jupyter.org/) is an open source project. Many organizations and universities use it for data science, data discovery, and visualization workflows, and it is the most popular data science interface for code execution.&#x20;

Accessed via a browser, it is has a kernel (in the video below it is Python) and has a UI to access the file system:

{% embed url="<https://www.youtube.com/watch?index=1&list=PLG7TPzTSJYkdIy0aSBcPR6CYD8l2z7utq&pp=gAQBiAQB&t=10s&v=WFHMJ726aP0>" %}

### How does a Jupyter Notebook work?

Jupyter Notebooks are built on the IPython kernel, famous for its REPL (Read-Eval-Print-Loop) capabilities. REPL interfaces take user inputs, execute the code, and presents the result to the user. This feedback loop can be repeated multiple times in each Notebook. IPython is a command line terminal through which we can interactively execute python commands.&#x20;

Jupyter Notebooks contain cells that contain atomic commands. Each cell can be executed to get the result from a programming environment. For example, a cell might make a request to an API from a third-party system, and then return the result.&#x20;

<figure><img src="/files/O19hynUEVX0YfMeRFXym" alt="screenshot of a Jupyter task"><figcaption><p>A cell with one line of code. The evaluation is printed below the cell.</p></figcaption></figure>

{% embed url="<https://www.youtube.com/watch?index=2&list=PLG7TPzTSJYkdIy0aSBcPR6CYD8l2z7utq&pp=gAQBiAQB&v=-v50jsIpyLU>" %}

<br>

In addition to code cells, you can add text content (in Markdown format!)

<figure><img src="/files/ykgw643Epz5D5Dj64h7J" alt="Notebook markdown cell"><figcaption><p>Markdown cell</p></figcaption></figure>

{% embed url="<https://www.youtube.com/watch?index=3&list=PLG7TPzTSJYkdIy0aSBcPR6CYD8l2z7utq&pp=gAQBiAQB&v=JNrepLxDRPQ>" %}

When the Markdown cell is Run - it will display the text inside the Notebook:

<figure><img src="/files/hH4rargT9J9ajLh5g90K" alt="rendering the markdown inside the notebook"><figcaption><p>Rendering the markdown inside the notebook</p></figcaption></figure>

### Building on previous cells

Variables created in a cell are stored in the notebook kernel, and are available for use in subsequent cells:

<figure><img src="/files/JbYIHUHbe6DRJ1jj05oz" alt="Variable in the first cell can be referred to in the second cell"><figcaption><p>Variable in the first cell can be referred to in the second cell</p></figcaption></figure>

Cells in the notebook build on the results of previously run cells.  One way to think of a cell is as a microservice.  The microservices are called in order, and complete a full application when the Notebook is completed.

In the video below, we access a JSON file for an API key, and then make an API call for weather data:

{% embed url="<https://www.youtube.com/watch?index=4&list=PLG7TPzTSJYkdIy0aSBcPR6CYD8l2z7utq&pp=gAQBiAQB&v=Dt-ANGHETfM>" %}

Once we have collected the API data, we can load it into a dataframe and visualize the data.

{% embed url="<https://www.youtube.com/watch?index=5&list=PLG7TPzTSJYkdIy0aSBcPR6CYD8l2z7utq&pp=gAQBiAQB&v=d3QQhNCSO60>" %}

### Why use Jupyter Notebooks for runbook automation?

DevOps/SRE groups create Runbooks to automate their workflows. Runbooks are comprised of steps that must be completed in order to complete a task.  Many of these steps are accompanied by scripts that help in completeing the step.  By placing the Runbook inside a Notebook container - the code can actually be evaluated while the Runbook is being used!

By applying the Jupyter Notebook concept to automate the infrastructure workflows simplifies the task of a DevOps/SRE. It also aids in decoupling and debugging various systems quickly.&#x20;

PS: Unskript uses Jupyter Notebook under the hood. You could build or consume some knowledge shared by many engineers at the [awesome-cloud-ops](https://github.com/unskript/Awesome-CloudOps-Automation) repo and run it via the [Docker container](https://hub.docker.com/unskript).&#x20;

### Crash Course with Jupyter Notebook

#### Installation

There are many ways to install and run Jupyter Notebooks. Over the years, cloud platforms and several new-age startups have implemented Jupyter Notebooks - [Google Collab](https://colab.research.google.com/), [Deepnote](https://deepnote.com/), and [Naas.ai](https://www.naas.ai/).<br>

I’m using the Anaconda Distribution of Jupyter Notebook. Search for “anaconda download”; the first link you find is probably from Anaconda.com, which distributes the Jupyter Notebooks and several other products like Anaconda Server.&#x20;

1. Download and Install the [Anaconda Opensource Distribution](https://www.anaconda.com/products/distribution); it fits our use case to build and run a basic Jupyter Notebook.
2. Open the Anaconda Navigator

<figure><img src="https://lh4.googleusercontent.com/nVRzwswxRYE8TH9HvQBaB1HdX3QsDfkiu2DZ8oCOt3aNyEz3KYaVfqulMEIVVCZ7-rwSiLWRCaIB63PKaZkJV0Fps-Ueru9m2UGXXcnJRYEq1GICUyZ0jGYO1OlDdTrAgla5CgQuGovcAmlcluRwqcYAVGcoEi6K4Sdnkt_zRiR6bsve13v65erfY5pqhQ" alt="screenshot of Anaconda manager"><figcaption></figcaption></figure>

<br>

1. Launch Jupyter Notebook! (make you are not launching JupyterLab)
2. A browser tab would be launched at <http://localhost:8889/tree>

<figure><img src="https://lh6.googleusercontent.com/Fw-sdJmJJRLMzp1xtRfhtw4APR_5s7yVmA1DiBqp0YVTGxBIbaSDXYZiFrlpCq1KwVcWo3Prfa7al0k0YTEZ98wHJNtof3E81YHFDrIyOJvposEDk_5iNW7AWWnK7kBVuHP325UcSvV0TutUlokduvLT0UDXxi0DMlg8yAwrUEJBbe1hWs2tFXOWGrJEog" alt="Jupyter screen shot"><figcaption></figcaption></figure>

&#x20;

#### Building a notebook

<br>

Next up, hit New -> Python 3 (ipykernel)&#x20;

#### ![creating a new notebook](https://lh4.googleusercontent.com/wijailL-HndyR2STYWJ2mPOQaHzhQFz2t0JFHZL6wXPX1EjwJ6fECLkYlrfIg-T-3hm7Cg103tCKUQfODuuuvg8iQVeSN_uhJqlFS101PqkE7iHQwo_G4TWD3bSInXb07jui_cUY0upcsGh-B8lllFA9JBiGNqaJS8KnYX05IVq__BPW7W89fio-SW0ZKw)

Each Jupyter notebook contains multiple cells, which contain Python code. Python code in each cell gets executed when you run the cell. Cells can also be configured for text so that you can input instructions and guidance needed for the following code snippet. (markdown!).

<br>

<figure><img src="https://lh3.googleusercontent.com/Juz4LOeEIOP-noFA7qwa82e0ffIw02s-PSxC2XH-2kWZin8MwOJ7sxnXCJ3jWWpJDRxyQFfbEkLlah6gySbNca5VsK-V8ZiwYQ95IYuKX0KPjkiZH-3jNXwgIHmNf9WzKweHcfeAjtkeBp20rbmLzXXG7o7Wpxj1oB2FfqFUyfut5IwcuuB64ZLxVgJXdw" alt="cell menu showing configuration options"><figcaption></figcaption></figure>

<br>

Please note each cell carries the programming context from the above cells.

#### Running it!&#x20;

A notebook (containing multiple cells) can be run in one go by using Cell -> Run Cells (as shown in the image above).<br>

Alternatively, if you are a command line fan, you can use the below command to run a specific notebook.&#x20;

\> jupyter notebook notebook.ipynb

### unSkript and Jupyter Notebooks

As discussed above, unSkript uses opensource Jupyter Notebooks under the hood and provides a seamless way of debugging/triggering complex infrastructure scripts.&#x20;

<br>

unSkript has many open-source runbooks (aka notebooks) at the [awesome-cloud-ops](https://github.com/unskript/Awesome-CloudOps-Automation) GitHub repository. So give us a [star](https://github.com/unskript/Awesome-CloudOps-Automation) and raise an issue if you feel we are missing something. &#x20;

### Resources

Few more resources on getting started to understand Jupyter Notebooks:<br>

* [Jupyter Notebook Docs](https://jupyter-notebook.readthedocs.io/en/stable/notebook.html)
* [How to use Jupyter Notebooks?](https://www.dataquest.io/blog/jupyter-notebook-tutorial/)
* [A detailed guide on Jupyter Notebooks](https://realpython.com/jupyter-notebook-introduction/)
* [The definitive guide on Jupyter Notebooks](https://www.datacamp.com/tutorial/tutorial-jupyter-notebook)
* [\[Video\] Jupyter Notebook Tutorial for Python](https://www.youtube.com/watch?v=2WL-XTl2QYI)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.unskript.com/unskript-documentation/fundamentals/jupyter-notebooks-101.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
