# Add cell to notebook from another cell

**URL:** <https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333>\
**Category:** Help\
**Created:** [October 18, 2018, 1:40am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333 "2018-10-18T01:40:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 1:40am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/1 "2018-10-18T01:40:36Z")

</div>

Sure that I just missed how to do this. How does one add a cell to the notebook programmatically from another cell?

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 2:07am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/2 "2018-10-18T02:07:15Z")

</div>

Do you mean [import](https://beta.observablehq.com/@mbostock/introduction-to-imports)?

There is no programmatic interface for editing notebooks. (At least, not currently.)

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 2:16am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/3 "2018-10-18T02:16:51Z")

</div>

Not importing - I have source class definitions that are being stored in a Gist. Fetch the source and place in an existing cell - which implements the class into the notebook - renames the cell to the name of the class. Works great.

Right now I create all the cells needed to hold the classes by hand.  
Was hoping could create them on the fly.

not that big of a deal. thx

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 3:21am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/4 "2018-10-18T03:21:22Z")

</div>

It is possible to require the Gist directly, rather than copy-and-paste:

> **[Require from GitHub](https://beta.observablehq.com/@mbostock/require-from-github)**
>
> An Observable notebook by Mike Bostock.

But you’d still need to create a cell for each class (each with the corresponding require) if you want them to be separate cells.

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 3:30am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/5 "2018-10-18T03:30:33Z")

</div>

I’m just doing:  
In the destination cell:

```auto
mutable Sample = ' '

```

The code in another cell has the line:

```auto
 mutable Sample = Function('return (' + classSourceFromGist + ')')();

```

It simple and works awesome.

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 3:42am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/6 "2018-10-18T03:42:32Z")

</div>

Why mutable? This should work, too:

```auto
Sample = Function('return (' + classSourceFromGist + ')')()

```

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 3:45am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/7 "2018-10-18T03:45:06Z")

</div>

TypeError: Assignment to constant variable Sample

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 4:13am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/8 "2018-10-18T04:13:07Z")

</div>

btw - I’m not copy/pasting. It’s all automated.

Go to the third blue button. Just above you will see an error - that is because the class does not exist in the notebook yet. Press the button ExampleClass. Will see the magic happen.

> **[004 - DMT Gist Tool](https://beta.observablehq.com/@potofcoffee2go/004-dmt-gist-clerk)**
>
> An Observable notebook by PotOfCoffee2Go.

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 4:30am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/9 "2018-10-18T04:30:30Z")

</div>

You’ll have to delete the `mutable Sample` cell, too.

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 4:37am UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/10 "2018-10-18T04:37:10Z")

</div>

Did that and still get the type error - well, now Sample doesn’t have a reference.

 ![Selection_005](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/d/d3ffecd4d9a59450389a14665a84dd7f167b53bd.jpeg)

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 1:49pm UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/11 "2018-10-18T13:49:14Z")

</div>

Right, the snippet I posted is a top-level cell definition, not an assignment within a cell.

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 7:14pm UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/12 "2018-10-18T19:14:01Z")

</div>

It took a while for

> [@mbostock](#):
>
> is a top-level cell definition

to work its way thru the cobwebs in my head. But it finally clicked. For me, that simple realization connected so many other “dots” in my understanding about how Observable cells work.

Would you agree that except maybe in very rare edge cases,

> “there is no need to ever create an empty cell from another cell”

because no matter how you look at it… at top-level the cell always has to be defined. So might as well do that when you are writing the notebook.

Or am I still missing the boat?

* * *

I know there are as many ways out there to load gist source text into a cell and execute it as there are developers. Here is mine.

```js
Sample = new SourceFromGist('8014cc196d580699feab787abc9398a0', 'sample class.js')

```

This snippet in another cell implements fetching the source

```js
class SourceFromGist {
  constructor(gistid, filename) { return this.sourceFromGist(gistid,filename) }

  async sourceFromGist(gistid, filename, opts) {
    if (!(gistid && filename)) {return 'GistID and gist Filename is required to fetch code'}

    let gistRsp = await (await fetch(`https://api.github.com/gists/${gistid}`,opts)).json();
    return Function('return (' + gistRsp.files[filename].content + ')')();
  }
}

```

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 18, 2018, 7:33pm UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/13 "2018-10-18T19:33:41Z")

</div>

Yep, the ideal is each cell defines a value, and other cells may reference that value but don’t assign to (mutate) it.

I’ve updated my notebook to give an example of loading a Gist, too:

> **[Require from GitHub](https://beta.observablehq.com/@mbostock/require-from-github)**
>
> An Observable notebook by Mike Bostock.

---

<div class="post-metadata">

**Author:** ![PotOfCoffee2Go](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/potofcoffee2go/32/1091_2.png) [@PotOfCoffee2Go](https://talk.observablehq.com/u/PotOfCoffee2Go)\
**Post date:** [October 18, 2018, 7:37pm UTC](https://talk.observablehq.com/t/add-cell-to-notebook-from-another-cell/1333/14 "2018-10-18T19:37:37Z")

</div>

Ok! I’ll check-out your book. And, thanks a lot mike for your help.
