> ## Documentation Index
> Fetch the complete documentation index at: https://docs.expectedparrot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Notebooks

> The Notebook object allows you to share your notebooks and scripts (*.ipynb* and *.py* files) by uploading them to Expected Parrot. You can also view and pull notebooks that other users have shared publicly or privately with you.

## Special note for Colab users

If you are using EDSL in a Colab notebook, please see [special instructions](/en/latest/colab_notebooks) on posting Colab notebooks to Expected Parrot ([Colab Notebooks](/en/latest/colab_notebooks#colab-notebooks)).

### Creating a Notebook object

There are three ways to create a Notebook object:

#### 1. From a *.ipynb* file

Pass the path to your *.ipynb* file to the constructor:

```python theme={null}
from edsl import Notebook

notebook = Notebook("your_notebook.ipynb") # replace with your file path
```

<Note>
  **Note**:

  You first need to save your notebook in order to pass the path\<filename> argument to the constructor.
</Note>

#### 2. From a *.py* script

Call the from\_script() method on the constructor and pass it the path to your *.py* script:

```python theme={null}
from edsl import Notebook

notebook = Notebook.from_script("your_script.py") # replace with your script path
```

#### 3. From data

For this method, your data must be a Python dict that conforms to the official Jupyter notebook format. Learn more about the format [here](https://nbformat.readthedocs.io/en/latest/format_description).

```python theme={null}
from edsl import Notebook

data = {
    "metadata": dict(),
    "nbformat": 4,
    "nbformat_minor": 4,
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": dict(),
            "source": "# Test notebook",
        },
    ],
}

notebook = Notebook(data=data)
```

#### 4. From self

To create a Notebook for a notebook that you are currently working in:

```python theme={null}
from edsl import Notebook

# Do not pass any arguments to the constructor
notebook = Notebook()
```

<Warning>
  **Warning**:

  For now, this method only works if you are using the VS Code IDE.
</Warning>

### Uploading a notebook to Expected Parrot

A notebook can be posted to Expected Parrot in the same ways as other EDSL objects: by calling the push() method on the object or calling the create method on a `Coop` client object and passing it the notebook.

Here we create a Notebook object and use the push() method to post it to Expected Parrot. You can optionally pass a description, a convenient alias URL and a visibility setting (*public*, *private* or *unlisted* by default) to the push() method:

```python theme={null}
from edsl import Notebook

notebook = Notebook("demo_notebook.ipynb")

notebook.push(
    description = "This is a demo notebook",
    alias = "demo-notebook",
    visibility = "public"
)
```

These can also be modified at Expected Parrot later on. We can see that the notebook has been posted publicly with a description and an alias URL (you can retrieve and refer to the object by either the UUID or URL):

```json theme={null}
{'description': 'This is a demo notebook',
'object_type': 'notebook',
'url': 'https://www.expectedparrot.com/content/121e2904-e09e-4859-80d5-dc98cb8c537a',
'alias_url': 'https://www.expectedparrot.com/content/RobinHorton/demo-notebook',
'uuid': '121e2904-e09e-4859-80d5-dc98cb8c537a',
'version': '0.1.47.dev1',
'visibility': 'public'}
```

Here we alternatively use the `Coop` client object to post the notebook:

```python theme={null}
from edsl import Coop, Notebook

coop = Coop()

notebook = Notebook("demo_notebook.ipynb")

coop.create(notebook, description="This is a demo notebook", visibility="public")
```

<Note>
  **Note**:

  (Note that we cannot reuse the alias unless we delete the object.) This will return a message with information about the object that was posted, and you will be able to view your notebook at Expected Parrot: [Content](https://www.expectedparrot.com/home/content).
</Note>

### Updating a notebook on Expected Parrot

A notebook can be updated on Expected Parrot in the same ways as other EDSL objects: by calling the patch() method on the object or calling the update method on a `Coop` client object and passing it the parameters to be modified.

Here we update the description of a notebook that we have already posted:

```python theme={null}
from edsl import Notebook

notebook = Notebook.pull("https://www.expectedparrot.com/content/RobinHorton/demo-notebook")

notebook.patch(
    "https://www.expectedparrot.com/content/RobinHorton/demo-notebook",
    description = "This is an updated demo notebook"
    )
```

Here we alternatively use the `Coop` client object:

```python theme={null}
from edsl import Coop

c = Coop()

c.patch(
    "121e2904-e09e-4859-80d5-dc98cb8c537a",
    description = "This is an updated demo notebook"
    )
```

Here we update the contents of the notebook itself by passing the value argument:

```python theme={null}
notebook = Notebook("demo_notebook.ipynb") # resaving the notebook

notebook.patch(
    "121e2904-e09e-4859-80d5-dc98cb8c537a",
    value = notebook
    )
```

### Saving a notebook to file

You can access notebooks that other users have posted publicly at Expected Parrot [Content](https://www.expectedparrot.com/login) page.

Notebooks can be copied and downloaded the same way as other EDSL objects: by calling the pull() method on the Notebook constructor or the get method on a `Coop` client object and passing the notebook’s uuid. You can also use the to\_file() method to save the notebook to a file:

```python theme={null}
from edsl import Notebook

notebook = Notebook.pull("121e2904-e09e-4859-80d5-dc98cb8c537a",)

notebook.to_file("new_demo_notebook.ipynb")
```

This allows you to edit and run the notebook on your local machine.

### Deleting a notebook from Expected Parrot

A notebook can be deleted from the platform in the same ways as other EDSL objects: by calling the delete() method on the constructor and passing it the uuid of the notebook to be deleted. You can also delete a notebook manually from your Expected Parrot account.

Here we delete a notebook using the Notebook object:

```python theme={null}
from edsl import Notebook

Notebook.delete(uuid = "121e2904-e09e-4859-80d5-dc98cb8c537a",)
```
