MATHEMATICA HOW TO

How to | Create a Computable Document Format (CDF) File

Computable Document Format (CDF) files supply a rich deployment method leveraging the power and flexibility of the Mathematica language with the wide distribution provided by a public format. As a built-in feature of Mathematica 8, it's easy to save .cdf files straight from your working notebooks, custom-formatted papers and articles, or dedicated application development workflows. Anything you compute in Mathematica can be made into a user-interactive object offering maximum clarity in the presentation of your concepts, and there are no special considerations when creating documents just for viewing in Wolfram CDF Player; all notebook features can be displayed and printed.

Computable Document Format files can be created from existing notebooks or from scratch. They can contain absolutely anything you can put into a Mathematica notebook.

The easiest way to create a new .cdf file is to select File ► New ► FreeCDF (.cdf) from the menu:

A new, empty window will appear, just as if you had opened a new notebook. You can add content to this window just as you would a Mathematica notebook.

The main difference between a Mathematica notebook and a .cdf file is the allowance of interaction when viewing your document in CDF Player. Notebook files (files with the extension .nb) are viewable as static documents in CDF Player, but any Manipulate objects present in a .cdf file are fully interactive in CDF Player.

For example, if you add the following output to a .cdf file, users viewing your document using CDF Player will be able to move the sliders interactively:

Interactive and static content can be mixed freely in a .cdf file. For instance, you could provide a header to your interface:

Not all content in a .cdf file created this way is interactive, however. Please see "Interactivity in .cdf Files" for further information.

    

An open notebook can also be made into a .cdf file in one of two ways. The first method uses the Save As menu, while the second method uses the CDF Export menu item. For more information on the CDF Export menu item or how to embed a .cdf file into existing HTML, see "Deploying a .cdf File".

To use the Save As menu, select your open notebook in Mathematica and then select File ► Save As...:

Then select Computable Document (*.cdf) from the drop-down menu labeled Save as type: in the file browser window that appears:

That's it! Your notebook is now an interactive .cdf file, ready for viewing in CDF Player.

    

The third option for creating a .cdf file is by using Export. Both Import and Export fully support the "CDF" file format.

Here is an example Notebook expression containing a Manipulate output:

In[1]:=
Click for copyable input

The newly opened notebook should look like this:

To create a .cdf file from this notebook expression, use Export just as you would with any other file format:

In[2]:=
Click for copyable input
Out[2]=

Deploying a .cdf File

Using the File ► CDF Export menu item, you can either save a .cdf file as a standalone document or easily embed either the entire document or a specific selection into existing HTML code.

To create a standalone .cdf file, select the menu item File ► CDF Export ► Standalone...:

Upon clicking the menu item, the following window will appear:

Follow the instructions in the wizard to save a standalone .cdf file. Note that if you want to save a selection from a notebook instead of the entire window, you must select the portion to be deployed before opening the wizard.

    

If you want to create a .cdf file to be embedded in a web page, select the menu item File ► CDF Export ► Web Embeddable...:

Upon clicking the menu item, the following window will appear:

The wizard will walk you through the steps of saving the .cdf file from an entire document or a selected portion, specifying where on your web server the .cdf file will be located, and then it will provide an HTML code segment to be copied and pasted into an existing HTML file. Note that, as with the standalone file, if you want to save a selection from a notebook instead of the entire window, you must select the portion to be deployed before opening the wizard.

Interactivity in .cdf Files

Almost all of the functions available in Mathematica can be used to build applications for CDF Player, but there are a few programming restrictions to keep in mind.

  • Dialog windows are not supported.
  • MathLink operations, including J/Link and .NET/Link, are not supported.
    • Data import and export are not supported from within CDF Player with the exception of Wolfram-curated data sources (ChemicalData, CountryData, WordData, etc.).

    Because CDF Player cannot load custom data at runtime, you must ensure that all necessary information is embedded within the interactive elements of your .cdf file. This can be done with either Initialization or SaveDefinitions; both are options to Manipulate.

    The Initialization option can be used to include small blocks of code and data in the interactive output, as in this simple example:

    

Alternatively, for larger blocks of code, packages, or larger datasets, it may be more convenient to define them before creating the Manipulate output, then store their state within the output using the SaveDefinitions option. The simple example below recreates the above example without using Initialization:

In[2]:=
Click for copyable input
Out[4]=

For activation of higher-level application content in .cdf files, such as arbitrary input, dialog windows, and loading external data, please contact Wolfram Research.

New to Mathematica? Find your learning path »
Have a question? Ask support »