PREP 2015 Question Authoring - Archived

Including javascript in problems

Including javascript in problems

by Jan Hlavacek -
Number of replies: 10
I tried to include a d3plus figure (http://d3plus.org/) into my problem, however, I was not able to make it work. This is my file:

# DESCRIPTION
# Test of d3plus plot
# written by Jan Hlavacek (jhlavace@svsu.edu)
# ENDDESCRIPTION

## DBsubject('')
## DBchapter('')
## DBsection('')
## KEYWORDS('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
## Author('Jan Hlavacek')
## Institution('SVSU')

DOCUMENT();
HEADER_TEXT(<<'EOF');
<script src="https://www.d3plus.org/js/d3.js"></script>
<script src="https://www.d3plus.org/js/d3plus.js"></script>
<script language="javascript" type="text/javascript">
 <!-- //
 var data = [
 {"year": 1991, "name":"alpha", "value": 15},
 {"year": 1992, "name":"alpha", "value": 34},
 {"year": 1991, "name":"alpha2", "value": 17},
 {"year": 1992, "name":"alpha2", "value": 65},
 {"year": 1991, "name":"beta", "value": 10},
 {"year": 1992, "name":"beta", "value": 10},
 {"year": 1991, "name":"beta2", "value": 40},
 {"year": 1992, "name":"beta2", "value": 38},
 {"year": 1991, "name":"gamma", "value": 5},
 {"year": 1992, "name":"gamma", "value": 10},
 {"year": 1991, "name":"gamma2", "value": 20},
 {"year": 1992, "name":"gamma2", "value": 34},
 {"year": 1991, "name":"delta", "value": 50},
 {"year": 1992, "name":"delta", "value": 43},
 {"year": 1991, "name":"delta2", "value": 17},
 {"year": 1992, "name":"delta2", "value": 35}
 ]
 $(document).ready(function(){
 var visualization = d3plus.viz()
 .container("#viz")
 .data(data)
 .type("box")
 .id("name")
 .x("year")
 .y("value")
 .time("year")
 .ui([{
 "label": "Visualization Type",
 "method": "type",
 "value": ["scatter","box"]
 }])
 .draw();
});
 // -->
</script>
EOF

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);
Context("Numeric");

# Define variables here:

# Actual problem goes here:
TEXT(beginproblem());
TEXT( MODES(TeX=>'', HTML=><<END_SCRIPT ) );
<div id="viz"></div>

END_SCRIPT
BEGIN_PGML
blah blah
END_PGML

# Solution:
BEGIN_PGML_SOLUTION
*SOLUTION*

hkjhkj
END_PGML_SOLUTION

ENDDOCUMENT();

It is supposed to insert an interactive figure with two box plots, it is just a verbatim copy of an example from the d3plus page. The best I could do was to get a 'd3plus is not defined' in the javascript console at the line where `d3plus.viz` is called. Other things I tried lead either to more serious javascript errors or WeBWorK errors. Is there any way to insert d3, d3plus, jsxgraph, raphael etc visualization into WeBWorK problems?

In reply to Jan Hlavacek

Re: Including javascript in problems

by Gavin LaRose -
Hi Jan,

This is a great question. I have a partial answer for you here, and hopefully we can get a bit further with further thought or consideration.

First, the $(document).ready() call relies on having access to the jQuery library, which I suspect isn't available in WeBWorK problems. That said, it might be worth just trying to see if it works by adding another script call at the top of your inclusion:

    <script type="text/javascript" src="/webwork2_files/js/vendor/jquery/jquery.js"></script>

(jQuery is available in the WeBWorK html tree.) If that doesn't work, we'll need to play some more to see how best to make things work.

Gavin

In reply to Gavin LaRose

Re: Including javascript in problems

by John Travis -
Another note about javascript...the most recent version of WeBWorK uses a lot of javascript for the slicker interface. You may run into conflicts when adding in different jquery library script calls.

Details on what the current version 2.10 uses is located at

https://github.com/openwebwork/webwork2/tree/master/htdocs/js

In reply to Jan Hlavacek

Re: Including javascript in problems

by Jan Hlavacek -
Turns out the problem was caused by loading d3 and d3plus using https. Apparently the default sites do not support that. When loading from a CDN, namely cdnjs.com, that supports https, it works fine. Spacing must be adjusted a bit, it may be tricky to make that work on mobile devices well, but it works.

Ideally the files should be local, but I do not know where to put them. Just placing them directly in the directory where the .pg file is clearly does not work.

The following is a working version (well, it does not yet do anything as a problem, but the javascript works):

# DESCRIPTION
# Test of d3plus plot
# written by Jan Hlavacek (jhlavace@svsu.edu)
# ENDDESCRIPTION

## DBsubject('')
## DBchapter('')
## DBsection('')
## KEYWORDS('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
## Author('Jan Hlavacek')
## Institution('SVSU')

DOCUMENT();
HEADER_TEXT(<<'EOF');
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3plus/1.7.3/d3plus.js"></script>
<script language="javascript" type="text/javascript">
 <!-- //
 var data = [
 {"year": 1991, "name":"alpha", "value": 15},
 {"year": 1992, "name":"alpha", "value": 34},
 {"year": 1991, "name":"alpha2", "value": 17},
 {"year": 1992, "name":"alpha2", "value": 65},
 {"year": 1991, "name":"beta", "value": 10},
 {"year": 1992, "name":"beta", "value": 10},
 {"year": 1991, "name":"beta2", "value": 40},
 {"year": 1992, "name":"beta2", "value": 38},
 {"year": 1991, "name":"gamma", "value": 5},
 {"year": 1992, "name":"gamma", "value": 10},
 {"year": 1991, "name":"gamma2", "value": 20},
 {"year": 1992, "name":"gamma2", "value": 34},
 {"year": 1991, "name":"delta", "value": 50},
 {"year": 1992, "name":"delta", "value": 43},
 {"year": 1991, "name":"delta2", "value": 17},
 {"year": 1992, "name":"delta2", "value": 35}
 ]
 document.addEventListener('DOMContentLoaded', function(){
 var visualization = d3plus.viz()
 .container("#viz")
 .data(data)
 .type("box")
 .id("name")
 .x("year")
 .y("value")
 .time("year")
 .ui([{
 "label": "Visualization Type",
 "method": "type",
 "value": ["scatter","box"]
 }])
 .draw();
});
 // -->
</script>
EOF

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);
Context("Numeric");

# Define variables here:

# Actual problem goes here:
TEXT(beginproblem());
TEXT( MODES(TeX=>'', HTML=><<END_SCRIPT ) );
<div id="viz" style="height:300pt; padding=10pt;"></div>

END_SCRIPT
BEGIN_PGML
blah blah
END_PGML

# Solution:
BEGIN_PGML_SOLUTION
*SOLUTION*

hkjhkj
END_PGML_SOLUTION

ENDDOCUMENT();

In reply to Jan Hlavacek

Re: Including javascript in problems

by Jan Hlavacek -
Another problem is that the javascript generated stuff does not show up when browsing library. Some alternate text must be insertedthat will get deleted by javascript and replaced by the plot.
In reply to Jan Hlavacek

Re: Including javascript in problems

by Paul Pearson -
Hi Jan,

JavaScript that is put into a HEADER_TEXT() block does not get processed when a pg file is rendered in the Library Browser.  If you put it in a TEXT() block instead, it will get processed when the pg file is rendered in the Library Browser.

Best regards,

Paul Pearson
In reply to Jan Hlavacek

Re: Including javascript in problems

by Gavin LaRose -
Hi Jan,

I'm glad this worked---I thought that it should. My guess is that you could put the javascript libraries in the html directory in your course (up one level from the templates directory) to have them be local. The URL for those files is then, I think, /webwork2_course_files/coursename/filename. I don't know enough about how the Library Browser treats included files to know how it will deal with included javascript.

Gavin
In reply to Jan Hlavacek

Re: Including javascript in problems

by Davide Cervone -
One thing to keep in mind as you begin to include your own javascript libraries is that your problem will need to be displayed not only one screen but also in hardcopy. If the data you are presenting using javascript is important to the problem, you might want to provide a means of including it in hardcopy.

WeBWorK does have graphics-creation macros, though they are pretty primitive, and badly need to be modernized. They do work in both screen and hardcopy mode, however, and that is an important advantage.

As WeBWorK moves into a much more javascript-oriented web environment, we may need to look into hooking the webwork back end into javascript tools like node.js that can run the same code on the server as in the browser. But that functionality isn't currently available.
In reply to Davide Cervone

Re: Including javascript in problems

by Jan Hlavacek -
I realized that was a problem. I was envisioning using javascript to not just present graphics, but mainly to let user interact with the graphics in some way (the example d3plus boxplot lets user switch between boxplot and scatter plot, and displays some aditional info on mouseover) or to show animations. None of that would even be possible in a hardcopy. I know that geogebra applets were used in webwork previously. How was that problem handled in that case?
In reply to Jan Hlavacek

Re: Including javascript in problems

by John Travis -
Information regarding geogebra apps inside WW problems is documented on the wiki at http://webwork.maa.org/wiki/GeoGebraApplets .
In reply to John Travis

Re: Including javascript in problems

by Paul Pearson -
Hi folks,

The GeoGebra example that John Travis pointed you to actually uses Java.  I have deprecated that example (because it uses Java, which is no longer universally supported / permitted to run in web browsers) and in the past week have posted a "GeoGebraWeb" example that uses JavaScript instead and should run on all web browsers and on other devices such as tablets that support JavaScript.  See http://webwork.maa.org/wiki/GeoGebraWeb1  There are a number of GeoGebraWeb examples in the OPL directory 


I plan to discuss embedding JavaScript into WeBWorK problems in workshop 4 (or perhaps the end of workshop 3).  I have a few working examples of "GeoGebraWeb" (GeoGebra run by JavaScript) and an example using d3.js to display a histogram rendered in html (but not pdf).

Best regards,

Paul Pearson