# Import a notebook dynamically for embedding

**URL:** <https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494>\
**Category:** Help\
**Created:** [April 29, 2022, 1:57am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494 "2022-04-29T01:57:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harris](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/harris/32/3326_2.png) [@harris](https://talk.observablehq.com/u/harris)\
**Post date:** [April 29, 2022, 1:57am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/1 "2022-04-29T01:57:36Z")

</div>

Does anyone have experience importing a notebook dynamically and embedding it? I’m trying to write a React component that will use the runtime to embed a notebook. I can’t use the ES6 syntax suggested by the runtime docs

```javascript
import define from "https://api.observablehq.com/@observablehq/hello-world.js?v=3";

```

because I want it to be able to import a notebook specified by the user. Here’s what I’m currently attempting:

```javascript
import { Runtime, Inspector } from '@observablehq/runtime'
import { useEffect, useState } from 'react'

export default function NotebookRenderer({ config }) {
    const [notebook, setNotebook] = useState(null)

    useEffect(() => {
        const notebookUrl = `https://api.observablehq.com/${config.notebook}.js?v=3`
        import(notebookUrl).then(notebook => setNotebook(notebook))
    })

    return (
        <>
            {/* code to render notebook will go here */}
        </>
    )
}

```

But I get

```auto
Uncaught (in promise) Error: Cannot find module 'https://api.observablehq.com/NOTEBOOKURL.js?v=3'

```

in the console. I’m using webpack via create-react-app if that is influencing this behavior.

---

<div class="post-metadata">

**Author:** ![harris](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/harris/32/3326_2.png) [@harris](https://talk.observablehq.com/u/harris)\
**Post date:** [April 29, 2022, 3:08am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/2 "2022-04-29T03:08:14Z")

</div>

Ah maybe this actually is a webpack issue after all: [reactjs - Using `dynamic import` to fetch script from another host? - Stack Overflow](https://stackoverflow.com/questions/54781242/using-dynamic-import-to-fetch-script-from-another-host)

---

<div class="post-metadata">

**Author:** ![harris](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/harris/32/3326_2.png) [@harris](https://talk.observablehq.com/u/harris)\
**Post date:** [April 29, 2022, 3:16am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/3 "2022-04-29T03:16:27Z")

</div>

This was indeed a webpack issue, but in case anyone else runs across it, adding this webpack magic comment turned out to be the solution:

```auto
import(/* webpackIgnore: true */notebookUrl).then(notebook => setNotebook(notebook))

```

See for more detail:

> <https://github.com/webpack/webpack/issues/7644#issuecomment-402123392>
>
> \## Feature request
> 
> I need build option to handle dynamic import to load outer… sources or just use native one to optimize.
> 
> https://stackoverflow.com/questions/50960619/can-i-use-browser-native-import-in-webpack-4
> 
> \*\*What is the expected behavior?\*\*
> 
> webpack just ignore dynamic import if options are given.
> 
> \*\*What is motivation or use case for adding/changing the behavior?\*\*
> 
> I want to use webpack for UI but in some case I use dynamic import() with string to load but webpack always handle this.
> 
> I'm trying service-worker side transform. That need this feature https://github.com/mizchi/trans-loader
> 
> \*\*Are you willing to work on this yourself?\*\*
> 
> no. I don't know webpack internal yet.

---

<div class="post-metadata">

**Author:** ![tophtucker](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tophtucker/32/4781_2.png) [@tophtucker](https://talk.observablehq.com/u/tophtucker)\
**Post date:** [April 29, 2022, 5:47am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/4 "2022-04-29T05:47:47Z")

</div>

Nice Harris! We recently started putting together an example of dynamic imports for embedding [in our repo of runtime examples](https://github.com/observablehq/examples/tree/toph/react-dynamic-import/react-dataflow-dynamic). It has a couple outstanding bugs, as you can read about in the [PR comments](https://github.com/observablehq/examples/pull/22#discussion_r819135185); I gotta get back to it and fix those sometime. The example uses Rollup but I should add a note about Webpack. Great find, thanks for working through it.

---

<div class="post-metadata">

**Author:** ![harris](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/harris/32/3326_2.png) [@harris](https://talk.observablehq.com/u/harris)\
**Post date:** [April 29, 2022, 2:49pm UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/5 "2022-04-29T14:49:46Z")

</div>

Oooh, thanks for those added details, @tophtucker. That’s excellent! And yes, those examples and comments are elucidating (and touch on some things I’ve overlooked in my prototyping 😅)

---

<div class="post-metadata">

**Author:** ![harris](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/harris/32/3326_2.png) [@harris](https://talk.observablehq.com/u/harris)\
**Post date:** [April 29, 2022, 4:47pm UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/6 "2022-04-29T16:47:08Z")

</div>

While we’re here @tophtucker do you have any examples or advice for rendering cells in two different React components? For example if I want one component to actually render some cells with the inspector but I want another component to show a list of cell names. Should I instantiate the module twice, once for each component? Or do some complicated ref forwarding?

---

<div class="post-metadata">

**Author:** ![tophtucker](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tophtucker/32/4781_2.png) [@tophtucker](https://talk.observablehq.com/u/tophtucker)\
**Post date:** [April 30, 2022, 6:46am UTC](https://talk.observablehq.com/t/import-a-notebook-dynamically-for-embedding/6494/7 "2022-04-30T06:46:12Z")

</div>

Oh hm that’s a good question. My instinct is some complicated ref forwarding haha but I haven’t tried.
