Sunday 9 October 2011

Embedding Mathematica Content in Websites

I'd like to mention a new functionality provided by Wolfram (and one I have been waiting for for quite some time). The Computable Document Format (.cdf) is available in Mathematica 8, and makes it possible to view (and interact with) the saved content using a free Mathematica Player. Most importantly, the content may then be embedded in websites in a relatively straightforward manner, allowing the author to distribute it to a wider audience. This is achieved in the following steps (based on Wolfram Guidelines):
  1. The first thing to do is to prepare the content for embedding. To this end, one needs to select the relevant cells in Mathematica, choose Format > Style > Other from the program menu, type  PluginEmbeddedContent and click OK.
  2. In addition, it is useful to lock the content against editing, by choosing Format > Option Inspector, looking up the option Deployed and setting its value to False.
  3. The next step is to make the resulting .cdf file available on-line, by uploading it to a hosting service such as Google Sites.
  4. Finally, we paste the following html code in the relevant section of the website:
    <script type="text/javascript" src="http://www.wolfram.com/cdf-player/plugin/v2.1/cdfplugin.js"></script>
    <script type="text/javascript">
     var cdf = new cdfplugin();
     cdf.embed('/path/to/filename.cdf', width, height);</script> 
    where in the 2nd block of code we substitute the path to the file uploaded in step 3 above, as well as the width and height of the embedded object.
The result could look like in the example below, which is based on one of my previous posts. It retains all features of the original demonstration - observe that the entire phase portrait is still clickable, not just the controls. This requires a browser plugin included in the Mathematica Player installation. If it is not present, the script should display a link to the Player in place of the embedded content.

This is nice, as one no longer needs to load the content into Mathematica, and can instead interact with it directly from the web browser level. Well done Wolfram!