Group Abstract Group Abstract

Message Boards Message Boards

Wolfram Notebook Embedder released

Posted 7 years ago
POSTED BY: Jan Poeschko
17 Replies

Hi Jan, I’m excited about this direction! I got your examples going and wanted to take a next step – but, I’m stumbling. I use the ReactJS library extensively. When I saw you are using document.getElementById('container') light bulbs started going off somewhere above my head. As you may know React has the primary node into HTML ReactDOM.render(<App/>, document.getElementById("root")), where <App/> is the first component of the react component tree. What I’m interested in doing with import WolframNotebookEmbedder from 'wolfram-notebook-embedder' is add it lower/higher (at any rate exactly where I want) in the React component tree so that a specific React component is ‘responsible’ for container created/managed by WolframNotebookEmbedder. First, I started a project with a create-react-app (CRA) then tried npm install –save 'wolfram-notebook-embedder but this failed. I have the logs if your interested. Second, in index.html, I tried the

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <script
      crossorigin
      src="https://unpkg.com/wolfram-notebook-embedder@0.1/dist/wolfram-notebook-embedder.min.js"
    ></script>

But this is not working out. Unfortunately, I don’t have much time to start a research and testing process so looking for suggestions/help before proceeding.

Cheers, Mitch

POSTED BY: Jan Poeschko

Hi Jan, this is great!

I've been struggling to integrate WolframNotebookEmbedder with React. From what I understand did you mean something like this:

import React from 'react';
import WolframNotebookEmbedder from 'wolfram-notebook-embedder';

class NotebookEmbed extends React.Component {
  constructor(props) {
    super(props);
    this.embeddingElem = React.createRef();
  }

  componentDidMount() {
    const embeddingElem = this.embeddingElem.current;
    this.embedding=WolframNotebookEmbedder.embed('https://www.wolframcloud.com/obj/jpoeschko/Public/BasicExample.nb', embeddingElem);
  }

  componentWillUnmount() {
    this.embedding.then(nb => nb.detach());
  }

  render() {
    return <div className="NotebookEmbed" ref={this.embeddingElem} />;
  }
}

export default NotebookEmbed;

I keep getting Uncaught TypeError: Cannot read property 'embed' of undefined at NotebookEmbed.componentDidMount errors with this..

Which I believe suggests it's a context issue? Perhaps related to this?

Not entirely sure why I had to import like this (new to React), but if anyone stumbles upon this - I indeed got this working using a CRA with react:17.0.1, react-dom:17.0.1, and wolfram-notebook-embedder:0.2.1 :

import React from 'react';
import * as WolframNotebookEmbedder from 'wolfram-notebook-embedder';

class NotebookEmbed extends React.Component {

  componentDidMount() {
    this.embedding=WolframNotebookEmbedder.embed('https://www.wolframcloud.com/obj/jpoeschko/Public/BasicExample.nb', this.el);
  }

  componentWillUnmount() {
    this.embedding.then(nb => nb.detach());
  }

  render() {
    return <div className="NotebookEmbed" ref={el => this.el = el} />;
  }
}

export default NotebookEmbed;

This has the potential of being a really big deal for users, and therefore for Wolfram. GREAT JOB Jan!!!

The documentation appears to be missing a closing > on the embed script. And the examples are overly simplistic. They should include an actual running example with the Astronomy calculator for example! Also all those images for the code that needs to be used cannot be copied and pasted!!! need the copyable code in those documentation pages...

I am trying to get this to work in Google Site. So far no luck. Could someone paste here the code needed to make this work?

I tried:

<script crossorigin src="https://unpkg.com/wolfram-notebook-embedder@0.1/dist/wolfram-notebook-embedder.min.js"></script>

<div id="notebookContainer"></div>

<script WolframNotebookEmbedder.Embed('https://www.wolframcloud.com/obj/luc.barthelet/mynotebook.nb', document.getElementById('notebookContainer')
);></script>

Thanks again for doing this, really want to use it ASAP,

Luc

POSTED BY: Luc Barthelet

Thanks for your feedback, Luc!

Spot-on regarding the closing >. We'll fix that.

The point of the image on the first page is really just a "teaser" and overview of how the library operates; it's (deliberately) not even the complete HTML code for what you see on the right. More examples are on https://reference.wolfram.com/language/WolframNotebookEmbedder/examples/ and it should be easy to copy and paste code from there. The first example is intentionally simplistic to focus on the very basics; the following ones add a bit more complexity.

I agree that using a more sophisticated notebook (such as the Astronomy Clock shown on the landing page) would perhaps be more impressive, but at the same time, it wouldn't change the actual HTML or JS code, so there's a trade-off with distracting from what's essential for embedding... So I think we'll add another "neat" example.

In general, I agree we could and should add many more examples. Of course, since it's open source, anyone's welcome to help: Just open a pull request with changes to https://github.com/WolframResearch/wolfram-notebook-embedder/blob/master/website/pages/en/examples.js

Regarding your code: When I try to open https://www.wolframcloud.com/obj/luc.barthelet/mynotebook.nb I only get a 404 page. Maybe the notebook is not public? (Being public is a prerequesite for a notebook to be embeddable.) Also, instead of

<script                         
WolframNotebookEmbedder.Embed('https://www.wolframcloud.com/obj/luc.barthelet/mynotebook.nb',
document.getElementById('notebookContainer')
);></script>

you'll need

<script>
WolframNotebookEmbedder.embed(
    'https://www.wolframcloud.com/obj/luc.barthelet/mynotebook.nb',
    document.getElementById('notebookContainer')
);
</script>

(note the closing > and .embed instead of .Embed). Please let us know if you still run into problems after these changes.

POSTED BY: Jan Poeschko

Awesome, it works now. I just need to learn how to just get the last manipulate to show up rather than the full notebook!

POSTED BY: Luc Barthelet

It would be good if the Wolfram Notebook Embedder was installed on the Wolfram Community website and automatically worked for the Mathematica Stack Exchange.

And what would be really nice if it worked with Facebook and LinkedIn so as to make embedding Notebooks as easy as possible.

POSTED BY: Paul Abbott

Embedding in Facebook or Linkedin for you would be risky, in terms of cost, since anyone seeing it would end up charging your account...

POSTED BY: Luc Barthelet

Yes, integrating notebooks in Wolfram Community using this embedder library is already in progress. We're also working on an oEmbed endpoint, which will help e.g. with WordPress and maybe even Stack Exchange (?).

POSTED BY: Jan Poeschko
Posted 7 years ago

Hi Jan,

This is great news. Thank you.

Could this also be used to render static HTML of notebooks in Github repositories. Similar to Jupyter notebook rendering.?

POSTED BY: Rohit Namjoshi

Could this also be used to render static HTML of notebooks in Github repositories.

Good point, and yes, potentially – although the library itself is mostly about interactive rendering (on the client side). To retrieve static HTML for a (cloud) notebook, we have the /statichtml API described here: https://reference.wolfram.com/language/WolframNotebookEmbedder/docs/ServerSideRendering/

The primary purpose of this API is to speed up loading of embedded notebooks and to help with SEO, though. E.g. it currently doesn't include the content of all cells (only up to a certain vertical threshold, plus simple textual cells). We'll expand this in the future.

POSTED BY: Jan Poeschko
Posted 7 years ago

Glad to see this is finally out. One big question: I'd like to render arbitrary chunks of Mathematica code, but don't necessarily want to make a cloud notebook for each one. Anyway to get around this? Interactivity in terms of things like Manipulate doesn't matter to me, but I'd like Graphics3D to work.

POSTED BY: b3m2a1 ​ 

I'd like to render arbitrary chunks of Mathematica code, but don't necessarily want to make a cloud notebook for each one.

This should be addressed by the on-the-fly notebook rendering API (maybe of the form https://www.wolframcloud.com/nb?url=...) that I mentioned above. It could also take literal notebook content (a notebook/cell/box expression) as an argument, e.g. https://www.wolframcloud.com/nb?content=.... I won't make any promises when it's going to be there, but it's definitely something we want as well.

In the meanwhile, you could build something a bit like this API yourself, generating a cloud object based on the content expression passed in. You could use a hash of the expression as an identifier, avoiding unnecessary duplication. The API would return the final cloud object URL, which you could then feed into the embedder library. You could enable rather long (browser) caching for the API so it wouldn't have to be executed on every visit. Just be aware that there are some security challenges with this (e.g. you'd have to make sure there are no dangerous Dynamics in the notebook created under your account).

POSTED BY: Jan Poeschko
Posted 7 years ago

Very nice! My understanding of "cloud credits" is near zero. Do we get "charged" for each time someone uses an embedded notebook?

POSTED BY: Jim Baldwin

Currently, you get charged cloud credits for any kernel evaluations the notebook needs, e.g. to resolve a Dynamic or to update a Manipulate after moving a slider. Just viewing a notebook without any dynamic content (e.g. a notebook with only text in it) would not consume any credits.

This page has some relevant numbers: https://www.wolfram.com/cloud-credits/

You can see your currently available number of credits by evaluating $CloudCreditsAvailable (either in the cloud or on desktop).

That being said, the way we charge credits in deployed notebooks will change rather soon (to the better!): Dynamics and interactivity up to a certain (rather generous, per-viewer) limit will be free, and beyond that you'll be able to choose whether you actually want to pay credits or offer the viewer different options (e.g. they could sign in and make their own copy of the notebook, thus not consuming your credits). More details coming when we're closer to making this change.

POSTED BY: Jan Poeschko

Your remark about how credits will be charged in deployed notebooks caught my eye. Has there been any development in this direction since your post from last year? So far those few sentences are the only information I've been able to find in that vein.

POSTED BY: Michael Janas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard