Difference between revisions of "Sage.pl"

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with "<code> # sage.pl # # This macro provides functionality for calling a Sage cell server. # sub _sage_init { PG_restricted_eval('sub Sage {new sage(@_) }'); } package sage;...")
 
m (added tag)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<code>
+
<pre>
 
# sage.pl
 
# sage.pl
 
#
 
#
Line 20: Line 20:
 
SageCode => 'print 1+2',
 
SageCode => 'print 1+2',
 
ButtonText => 'Start/Restart the Interactive Cell',
 
ButtonText => 'Start/Restart the Interactive Cell',
CellServer => 'http://sagecell.sagemath.org',
+
CellServer => 'https://sagecell.sagemath.org',
 
SageAnswerName => 'sageAnswer', # not used yet
 
SageAnswerName => 'sageAnswer', # not used yet
 
SageAnswerValue => 'ansList', # not used yet
 
SageAnswerValue => 'ansList', # not used yet
 
AutoEvaluateCell => 'true',
 
AutoEvaluateCell => 'true',
  +
ShowAnswerBlank => 'hidden',
  +
accepted_tos =>'false', # force author to accept terms of service explicitly
 
@_
 
@_
 
);
 
);
Line 47: Line 49:
   
 
def record_answer($self->{SageAnswerValue}):
 
def record_answer($self->{SageAnswerValue}):
html('<input type=input size=15 name="$self->{SageAnswerName}" id="$self->{SageAnswerName}" value="%s">'%($self->{SageAnswerValue},) )
+
html('<input type=$self->{ShowAnswerBlank} size=15 name="$self->{SageAnswerName}" id="$self->{SageAnswerName}" value="%s">'%($self->{SageAnswerValue},) )
   
 
$self->{SageCode}
 
$self->{SageCode}
Line 77: Line 79:
   
   
  +
#### Experimental stuff.
  +
  +
package main;
  +
sub sageCalculatorHeader {
  +
$CellServer = 'https://sagecell.sagemath.org';
  +
main::HEADER_TEXT(main::MODES(TeX=>"", HTML=><<"END_OF_FILE"));
  +
<script src="$CellServer}/static/jquery.min.js"></script>
  +
<script src="$CellServer/static/embedded_sagecell.js"></script>
  +
<script>\$(function () {
  +
// Make the div with id 'mycell' a Sage cell
  +
sagecell.makeSagecell({inputLocation: '#mycell',
  +
template: sagecell.templates.minimal,
  +
evalButtonText: 'Activate'});
  +
// Make *any* div with class 'compute' a Sage cell
  +
sagecell.makeSagecell({inputLocation: 'div.compute',
  +
autoeval: 1,
  +
evalButtonText: 'Evaluate'});
  +
});
  +
</script>
  +
END_OF_FILE
  +
  +
}
  +
  +
sub sageCalculatorPad {
  +
my $top_test = shift;
  +
my $initial_contents = shift;
  +
main::TEXT(main::MODES(TeX=>"", HTML=><<"EOF"));
  +
  +
<p>
  +
$top_text
  +
<p>
  +
<div class="compute">
  +
<script type="text/x-sage">
  +
  +
$initial_contents
  +
  +
</script></div>
  +
  +
EOF
  +
  +
return $out;
  +
  +
  +
}
 
1; #required at end of file - a perl thing
 
1; #required at end of file - a perl thing
</code>
+
</pre>
  +
  +
  +
[[Category:Files]]

Latest revision as of 14:56, 21 June 2021

# sage.pl
#
#  This macro provides functionality for calling a Sage cell server.
#

sub _sage_init {
   PG_restricted_eval('sub Sage {new sage(@_) }');
}

package sage;

##  Options:
##    sage( SageCode (not yet), ButtonText, CellServerAddress)

sub new {
   my $self = shift; my $class = ref($self) || $self;

   my %options = ( 
      SageCode => 'print 1+2',
      ButtonText => 'Start/Restart the Interactive Cell',
      CellServer => 'https://sagecell.sagemath.org',
      SageAnswerName => 'sageAnswer',   #  not used yet
      SageAnswerValue => 'ansList',             #  not used yet
      AutoEvaluateCell => 'true',
      ShowAnswerBlank => 'hidden',
      accepted_tos =>'false',  # force author to accept terms of service explicitly
     @_
   );

   $self = bless {
     %options
   }, $class;

    main::RECORD_ANS_NAME($self->{SageAnswerName}, 345);

   $self->sageCode();
   $self->sagePrint();
   return $self;
}


sub sageCode{
  my $self = shift;
  main::TEXT(main::MODES(TeX=>"", HTML=><<"SAGE_CODE"));

<div id="sagecell">
<script type="text/code">

def record_answer($self->{SageAnswerValue}):
    html('<input type=$self->{ShowAnswerBlank} size=15 name="$self->{SageAnswerName}" id="$self->{SageAnswerName}" value="%s">'%($self->{SageAnswerValue},)   )

$self->{SageCode}

    </script>
</div>

SAGE_CODE
}

sub sagePrint{ 
  my $self = shift;
  main::TEXT(main::MODES(TeX=>"", HTML=><<"SAGE_PRINT"));
    <script src="$self->{CellServer}/static/jquery.min.js"></script>
    <script src="$self->{CellServer}/embedded_sagecell.js"> </script>
    <script>
      \$(function () {
        sagecell.makeSagecell({
           inputLocation:     '#sagecell',
           template:              sagecell.templates.minimal,
           autoeval:               $self->{AutoEvaluateCell}, 
           linked:                   true,        
           evalButtonText:    '$self->{ButtonText}'
         });
       });
    </script>
SAGE_PRINT
}


#### Experimental stuff.

package main;
sub sageCalculatorHeader {
$CellServer = 'https://sagecell.sagemath.org';
main::HEADER_TEXT(main::MODES(TeX=>"", HTML=><<"END_OF_FILE"));
<script src="$CellServer}/static/jquery.min.js"></script>
    <script src="$CellServer/static/embedded_sagecell.js"></script>
    <script>\$(function () {
    // Make the div with id 'mycell' a Sage cell
    sagecell.makeSagecell({inputLocation:  '#mycell',
                           template:       sagecell.templates.minimal,
                           evalButtonText: 'Activate'});
    // Make *any* div with class 'compute' a Sage cell
    sagecell.makeSagecell({inputLocation: 'div.compute',
                           autoeval: 1,
                           evalButtonText: 'Evaluate'});
    });
    </script>
END_OF_FILE

}

sub sageCalculatorPad {
	my $top_test = shift;
	my $initial_contents = shift;
main::TEXT(main::MODES(TeX=>"", HTML=><<"EOF"));

<p>
$top_text
<p>
<div class="compute">
<script type="text/x-sage">

$initial_contents

</script></div>

EOF
	
return $out;	
	
	
}
1;    #required at end of file - a perl thing