WeBWorK Problems

Problem embedding sage

Problem embedding sage

by Ken Levasseur -
Number of replies: 7

I'm trying to author a problem with an embedded SageMath graph.  I take two random integer values generated by Webwork and pass them to the Sage cell, which generates the graphic correctly.  The answer is computed in terms of the random numbers from Webwork, so I'm only using Sage to display the graph.  My problem is that Sage insists that there is a sageAnswer, which I think I've not created:

No answer evaluator for the question labeled: sageAnswer 

The answer checker I intend correctly checks the answer I intend, but it is preceded by the checking on a nonexistent sage answer.  

I'm not sure what is wrong.  I'm attaching the pg file

Ken Levasseur

PS:  The radius formula is not correct but has no bearing on the issue.


Tags:
In reply to Ken Levasseur

Re: Problem embedding sage

by Larry Riddle -
I'm not a Sage user in WeBWorK, but some experimentation showed that if I added the lines
$ansList = List("$r"); (after the definition of $r)
and
NAMED_ANS( sageAnswer => $ansList->cmp ); (before your answer checker)
then WeBWorK no longer gives a warning message. For reference, see https://webwork.maa.org/wiki/Sage_Embedding.
In reply to Larry Riddle

Re: Problem embedding sage

by Ken Levasseur -

Larry:

Thanks!  WeBWorK likes the adjustment - no "pg-errors" in the question; but another problem emerges.  In addition to checking the submitted answer for the radius correctly, WeBWorK expects an answer corresponding to the value of $ansList.   I've experimented with making further changes, but keep reverting to the original problem.  

I'm thinking that there must be a simpler version of sage embedding  than the template you pointed me to, where an answer doesn't depend on the Sage computation. All  I'm doing in this problem is using Sage to display a graph with the answer already predetermined. 

I'm attaching my updated version of the problem.

Ken

In reply to Ken Levasseur

Re: Problem embedding sage

by Malcolm Harper -

I think adding `AnswerReturn => 0,` to your `new sage` call will suppress sage from returning an answer to WeBWorK so that WeBWorK won't complain about the lack of an answer checker.  The call would then be:

new sage(
SageCode=>$SageCode,
AutoEvaluateCell=>'true',
AnswerReturn => 0,
 );

Malcolm

In reply to Malcolm Harper

Re: Problem embedding sage

by Ken Levasseur -

Malcolm:

Your suggestion made perfect sense, but didn't solve the problem.  I still get this warning:

No answer evaluator for the question labeled: sageAnswer

This occurs when I remove the line 

$ansList = List("$r"); 

in the variables section, and  also remove

NAMED_ANS( sageAnswer => $ansList->cmp );

in the answer checking portion of the question.  At that point, there is nothing called sageAnswer in my .pg code, so I assume this is automatically created as a result of the Sage call. 

Thanks for the suggestion though.

Ken


In reply to Ken Levasseur

Re: Problem embedding sage

by Glenn Rice -
Your code should look like this:

DOCUMENT();

loadMacros(
    "PGstandard.pl",
    "MathObjects.pl",
    "sage.pl"
);

TEXT(beginproblem());

$m = random(2,5,1);
$n = random(2,5,1);
$r=$m+$n-4;
####################################################
$SageCode = <<SAGE_CODE;

g=graphs.GridGraph([$m,$n]).plot()
g

SAGE_CODE

Sage(
    SageCode=>$SageCode,
    AutoEvaluateCell=>'true',
    ShowAnswerBlank => 'none'
);

Context()->texStrings;

BEGIN_TEXT
$PAR
What is the radius of the graph above?

\(r =\) \{ans_rule(15) \}
$BR
END_TEXT
Context()->normalStrings;
ANS(num_cmp($r));

ENDDOCUMENT();

I have removed some of the other things you have added to show the basics of what are needed.  You can add the other stuff back.

However, this won't work.  This is because there are two bugs in the sage.pl macro.  The first is a bug that prevents the ShowAnswerBlank => "none" option from working.  You can work around this by adding AnswerReturn => 0 as was suggested.  AnswerReturn is supposed to be a deprecated option, but works because of the bug.

That still won't work because of the other bug.  That is that the code in the macro uses the python2 print syntax, but sage now uses python3 which requires parentheses around the print functions arguments.

I have attached a fixed macro that you can use.  Place this in your courses templates/macros directory to override the built in macro.

There is still something odd going on with the page styles.  Something that Sage does is messing with things.  I haven't figured out what is causing that.
In reply to Glenn Rice

Re: Problem embedding sage

by Glenn Rice -

I have attached another version of sage.pl that is even better.  It fixes the other things that were going on in the page.  It seems that the sagecell server still doesn't have the jQuery noConflict issue fixed correctly.