# Loading vtk.js dynamically

**URL:** <https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755>\
**Category:** Web\
**Created:** [May 10, 2021, 3:30pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755 "2021-05-10T15:30:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 10, 2021, 3:30pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/1 "2021-05-10T15:30:41Z")

</div>

I’m trying to use vtk.js in a jupyter notebook with mixed success. When it works, it works great:

 ![obraz](https://discourse.vtk.org/uploads/default/original/2X/7/773c8fc9f23e13077ef7c04f6929266d0eaa1acc.png)  
But I’m struggling a lot with dynamically loading vtk.js. Most often it does not work (it does work sometimes - which makes me even more confused). I tried this:

```auto
new Promise(function(resolve, reject) {
	var script = document.createElement("script");
	script.onload = resolve;
	script.onerror = reject;
	script.src = "https://unpkg.com/vtk.js@14.16.4/dist/vtk.js";
	document.head.appendChild(script);
}).then(() => {
console.log(vtk)
});

```

and this

```auto
require.config({
        "paths": {"vtk": "https://unpkg.com/vtk"},
        "exports": {"vtk": null},
        "shim": {
            "vtk": {"exports": "vtk"},
        }
    }
);

require(["vtk"], function(vtk) {console.log(vtk);});

```

To sum up, the question is: what would be the conanical way of loading vtk.js?

Thanks,  
Adam

---

<div class="post-metadata">

**Author:** ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)\
**Post date:** [May 10, 2021, 4:34pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/2 "2021-05-10T16:34:12Z")

</div>

Do you have a typo on your second approach? Should it be `vtk.js` rather than vtk?

The first one should work except if Jupyter is using some iframe and therefore reduce the scope of that script loading to some extent.

I don’t know enough about Jupyter to comment on that though.

---

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 10, 2021, 8:24pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/3 "2021-05-10T20:24:52Z")

</div>

requirejs does add `.js` on its own, so there is no typo in the location. Is there anything special about vtk.js that could cause such intermittent failures? I tested both approaches with e.g. d3.js and they seem to always work.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)\
**Post date:** [May 10, 2021, 10:57pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/4 "2021-05-10T22:57:05Z")

</div>

Nothing that I think of. I’m wondering if you import twice what happen? Maybe that is the issue? Hard to tell.

---

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 18, 2021, 5:43pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/5 "2021-05-18T17:43:47Z")

</div>

Thanks for the help. I guess it is something jupyter specific - I asked the question here: [Trying to load vtk.js dynamically - Notebook - Jupyter Community Forum](https://discourse.jupyter.org/t/trying-to-load-vtk-js-dynamically/9244) .

---

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 20, 2021, 6:32pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/6 "2021-05-20T18:32:21Z")

</div>

This seems to work. I’d really would like to understand why…

```auto
new Promise(function(resolve, reject) {
        require.config({
        "paths": {"vtk": "https://unpkg.com/vtk"},
    }
);
    require(["vtk"], resolve);
}).then(() => {
    console.log(vtk);
    console.log("Loaded vtk.js library");
});

```

---

<div class="post-metadata">

**Author:** ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)\
**Post date:** [May 20, 2021, 8:23pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/7 "2021-05-20T20:23:02Z")

</div>

It looks like the `require.config` add the script tag. The `require([vtk]..)` wait for the script to be downloaded and interpreted.

---

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 20, 2021, 9:40pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/8 "2021-05-20T21:40:02Z")

</div>

Yes it does. I used this approach without wrapping it in a promise (see the original post) and it did not _always_ work.

It also does not really explain why the promise+adding a script element by hand did not work.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)\
**Post date:** [May 20, 2021, 9:44pm UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/9 "2021-05-20T21:44:51Z")

</div>

Hum indeed, that is intriguing… I’m not sure why, but it is nice to see it work now and have a path forward.

---

<div class="post-metadata">

**Author:** ![a-u](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-u/32/3325_2.png) [@a-u](https://discourse.vtk.org/u/a-u)\
**Post date:** [May 21, 2021, 6:41am UTC](https://discourse.vtk.org/t/loading-vtk-js-dynamically/5755/10 "2021-05-21T06:41:18Z")

</div>

Yes, thanks for your help! If anyone is interested the PR is here: [Implement importBrep and vtkPolyData export by adam-urbanczyk · Pull Request #735 · CadQuery/cadquery · GitHub](https://github.com/CadQuery/cadquery/pull/735)
