23
$\begingroup$

I have the following program at home, which draws on a batch of pre-processed files (that are word frequency text files, compiled using ToLowerCase, Tally and Sort. Here is an example of the file that it draws on:

{{"i", 3073}, {"you", 2860}, {"the", 1741}, {"and", 1518}, {"a", 1218}, {"me", 1209}, {"to", 1153}, {"my", 913}, {"t", 855}, {"that", 843}... and so on. Each file is about ~ 50KB, and it's been generated using Put and is thus grabbed using Get.

I use them for the following program, which essentially plots word frequency for a given year (each year has its own file). It's along a similar lines as the Google ngram viewer, but on a very specific historical dataset.

Manipulate[viewerCount1 = {};
 viewerCount2 = {};
 SetDirectory["/users/myNAME/desktop/DB/Put/"];
 filenames = FileNames["*.txt"];
 Do[
  input = Get[file];
  yearLength = Length[input];
  AppendTo[viewerCount1, 
   If[Length[Flatten[Cases[input, {word1, _}]]] == 0, 0, 
     Flatten[Cases[input, {word1, _}]][[2]]]/yearLength];
  AppendTo[viewerCount2, 
   If[Length[Flatten[Cases[input, {word2, _}]]] == 0, 0, 
     Flatten[Cases[input, {word2, _}]][[2]]]/yearLength];
  , {file, filenames}];
 DateListPlot[{Tooltip[viewerCount1, word1], 
   Tooltip[viewerCount2, word2]}, {1964}, Joined -> True, 
  PlotLabel -> "Number of Total Word Appearances by % of All Words", 
  PlotStyle -> {{Red}, {Blue}}], {{word1, "war", 
   "First Word (Red)"}}, {{word2, "peace", "Second Word (Blue)"}}]

enter image description here

Here's the crux of the question, however. Is there a good way to deploy this online so other people can access it? I assume this is too big for the CDF format? Alternatively, WebMathematica sounds promising, but before I upgrade my Mathematica purchase I would love to hear if the community thinks that would be useful.

I know that there have been discussions elsewhere about sharing dynamic content with non-Mathematica users, but these seem to not rely on specific external files.

Or, am I using the wrong language to deploy the final version of this?

$\endgroup$

3 Answers 3

14
$\begingroup$

You cannot use CDF for this because you have textual import fields and these are not supported when you embed online. You will just get a big grey box. InputField cannot accept strings (non-numeric) for online CDFs unless you are Wolfram Research and can override it. (I'm guessing you don't want to pay many thousands for CDF Pro.)

You also cannot import and export into an online embedded CDF, not even a Pro CDF (got that first hand from Wolfram). This is for security reasons.

So to create something like you have described using Mathematica and have online, the only possible way to do it, it would seem, is to use webMathematica.

$\endgroup$
10
  • 2
    $\begingroup$ I am wondering if the license permits using Mathematica on a webpage (i.e. writing the minimal functionality required for this instead of getting webMathematica) $\endgroup$
    – Szabolcs
    Commented Feb 15, 2012 at 22:44
  • $\begingroup$ +1. I guess one can create a sort of a virtual keyboard (buttons) to enter word letters in this way, with the mouse - if so, CDF may still be used, if I get it right. $\endgroup$ Commented Feb 15, 2012 at 23:06
  • $\begingroup$ Without going into a lot of detail I've explored a lot of ways to get things "into" a CDF with Wolfram. Also you ability to import "allowed" stuff (paclet data) into CDF is constrained when the CDF is embedded as well unless you embedd in "full screen" mode and press the button to override the security warning. $\endgroup$ Commented Feb 15, 2012 at 23:44
  • 1
    $\begingroup$ @Szabolcs: that's not a viable option because you can't guarantee how many web site users will concurrently try to run a MMA process). Regarding the claim that it doesn't work: could someone please test the CDF I posted here: pages.uoregon.edu/jenscls/readData/Untitled-2.cdf (if you confirm that it works, I think it will point the OP toward a solution to his problem). The lack of a text input field is not the central issue in this question, I would say. $\endgroup$
    – Jens
    Commented Feb 16, 2012 at 0:48
  • $\begingroup$ Sorry, I'm in a hurry but it does look like CDF player without Mathematica can't do what I thought... That is so sad. $\endgroup$
    – Jens
    Commented Feb 16, 2012 at 1:04
14
$\begingroup$

Depending on the total size of the files, CDF may or may not be a good option. A file of a few MB is probably still ok, 10Mb or more is probably not. That said, this looks like a case for WebMathematica to me. It is fairly easy to set up, and looking at your description, you won't need an awful lot of interactivity (which, had you needed it, would be one reason to prefer the CDF). The interface like the one you described can be easily done with WebMathematica, you can even adopt one of the existing examples that come with it.

However, keep in mind the following. With WebMathematica, you will have to maintain a server, meaning setting up some domain, getting a server running, watching it, caring about security of your code, maintaining everything, etc. With CDF, things are significantly simpler for you, since it runs on the client. However, with WebMathematica, people don't have to install anything, not even a CDF plugin, which generally is a plus, since lots of people are hesitant to install things on their computers (which, I think, is understandable).

So, in summary: if you want to do a one-time thing, and share just some particular piece of dynamic content, and you manage to squeeze it into not too large CDF, this may be what you need. If you plan on expanding things, and don't want to limit yourself to what can be done within the limitations imposed by CDF format, and you don't need sophisticated dynamic functionality, then WebMathematica may serve you better.

$\endgroup$
6
  • $\begingroup$ This is very good to hear! I'm trying to figure out what software is worth investing in. $\endgroup$ Commented Feb 15, 2012 at 15:12
  • 1
    $\begingroup$ Thanks for this. Quick question wrt CDF - since that format does not seem to support Import or Get from non-official sources, would I then write include all of that information in the Mathematica code itself (i.e. paste the MB or so of info into the notebook itself)? $\endgroup$ Commented Feb 15, 2012 at 15:19
  • 2
    $\begingroup$ +1 There are webMathematica hosting services. $\endgroup$
    – Mr.Wizard
    Commented Feb 15, 2012 at 15:20
  • 1
    $\begingroup$ @ian.milligan Yes, you'd have to keep it there. I would use Compress and store Compress-ed data. You can make them self-uncompressing upon the first call to the data. The technique can be similar to what I used to answer your other question here: stackoverflow.com/questions/8247005/… $\endgroup$ Commented Feb 15, 2012 at 15:21
  • $\begingroup$ @LeonidShifrin Fantastic, thank you. This is good to have a sense of the pros/cons of both options. $\endgroup$ Commented Feb 15, 2012 at 15:23
5
$\begingroup$

In this case, I assume you want both, dynamic interactivity and pull large datasets from a database in real time. This can be done with a CDF notebook, and it doesn't require WebMathematica unless your notebook really needs to do much more computationally intensive work than your example code indicates.

So here are the steps:

Server

Save your dynamic notebook as a CDF in a directory on your web server where you have enabled serving CDF - i.e., by adding the following to your .htaccess file:

AddType application/vnd.wolfram.player nbp
AddType application/vnd.wolfram.mathematica nb
AddType application/vnd.wolfram.cdf cdf
AddType application/vnd.wolfram.cdf.text cdf

Then save your data file (let's call it data.txt) somewhere on your server, too. I'll assume the URL of the data file is http://website.com/data.txt.

Your notebook

In your CDF file, read the contents of the required data file(s) using a statement like fileContents = Import["http://website.com/data.txt"];

That's all there is to it. Now you can manipulate the contents of data.txt on the client side, and update its contents on the server side if necessary.

Edit

After trying with a computer that doesn't have Mathematica installed, only the CDF player in the Chrome browser, it seems that Import doesn't work after all.

To solve the original problem of deploying an interactive representation of your data, the best approach therefore seems to be:

  1. In Mathematica on your local machine, read all the data into your notebook
  2. With these data, create your interactive element, e.g. a Manipulate.
  3. Save the resulting notebook with the dynamic content as a CDF file.
  4. Upload this file to your web server, thereby distributing the entire data content together with the interactive interface. This makes it unnecessary to read any external files into the deployed CDF.

An example for this is online at this URL.

This is of course a compromise, and it implies that you'll have to re-evaluate the notebook and save it as CDF every time the data needs to be updated. So, unfortunately, vanilla CDF isn't able to do exactly what you want, both regarding the data file handling and regarding the free-form text input fields.

Edit 2

The reason why I was initially confused about the limitations of the CDF format was actually very simple: the file that you get via Save As... CDF behaves very differently from the file that you get by using the menu Deploy > Standalone (a.k.a. Free CDF). It is the free version that should be used in order to make content available on the web if your target audience doesn't have Mathematica installed.

However, the "non-free" version from Save As... CDF is what I tried first. It is much more powerful, but when served as full screen it opens in the browser in the same way that the free CDF does. The non-free CDF then lets you work in your browser window almost as if you were in a Notebook. That's nice, but also confusing when you develop CDF files and lose track of which one was exported in what way. Perhaps it would have been better if Wolfram had reserved the CDF suffix purely for the free deployed format.

$\endgroup$
5
  • $\begingroup$ If you look at the comments in Leonid's answer, it's clear that CDF viewers do not support Import. $\endgroup$
    – Eli Lansey
    Commented Feb 15, 2012 at 17:00
  • 1
    $\begingroup$ So why does it work for me? Please don't tell me that CDF player behaves differently for users that have Mathematica installed! That would be incredibly bad design. $\endgroup$
    – Jens
    Commented Feb 15, 2012 at 17:04
  • $\begingroup$ Wait, scratch that. Checking the faq, it seems they can Import. $\endgroup$
    – Eli Lansey
    Commented Feb 15, 2012 at 17:08
  • $\begingroup$ I'm not sure. The FAQ says: "However, in files saved straight out of Mathematica 8 for the free CDF Player, some functionality is not available: non-numeric input fields, dialog windows, and data import and export (except from Wolfram-curated data sources, e.g. ChemicalData, CountryData, and WordData)." But then again, it later says that you can import text. So maybe I'm getting data/text/etc. confused in my head... since it is working for Jens. Let me test it out. $\endgroup$ Commented Feb 15, 2012 at 17:16
  • 1
    $\begingroup$ @Jens This is a Q where I wish I could accept all of the questions. I agree that this is extremely frustrating: the CDF player operates one way when Mathematica's installed, and another way on a box w/o it. I think webMathematica's probably the way forward. $\endgroup$ Commented Feb 16, 2012 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.