A simple grader script
This document will walk through the implementation of a grader script for a simple problem. All this grader will do is test whether the input function is of a straight line.
Each grader script at its base is composed of two components
1. The problem configuration
2. The grader function
Imports
There are two SketchResponse python modules that must be imported for this simple example. All grader scripts must import the sketchresponse
module. There are two other modules that provide different grading helper functions. In this case, we only need to input the GradeableFunction
module from grader_lib
.
from sketchresponse import sketchresponse
from sketchresponse.grader_lib import GradeableFunction
Problem configuration
The problem configuration is passed to the javascript front end to define the
size and scale of the drawing space and to define which drawing tools are
available for the problem. The sketchresponse.config()
function takes a dict of
configuration options.
In the example configuration below, the first seven key/value pairs are required:
'width': 750
sets the pixel width of the drawing space as 750 pixels'height': 420
sets the pixel height of the drawing space to 420 pixels'xrange': [-2.35, 2.35]
sets the numerical range of the x axis'yrange': [-1.15, 1.15]
sets the numerical range of the y axis'xscale': 'linear'
sets the scale of the x axis to linear (only option currently implemented)'yscale': 'linear'
sets the scale of the y axis to linear (only option currently implemented)'coordinates': 'cartesian' or 'polar'
sets the coordinate system used by the axes plugin to either cartesian or polar
The last entry 'plugins'
takes a list of dicts that enable the specific javascript plugins that are available to the user. All plugins are declared by 'name'.
The 'axes' plugin entry is the simplest plugin to enable. It has no mandatory options so all that must be set is the 'name'. This plugin enables the axes in the drawing space. It should probably be on by default no?
The 'freeform' plugin entry enables the freeform drawing tool. It has three configuration options to set:
'id'
sets the name of the argument of the grader callback function (described in the next section) to which the data generated by this plugin is passed.'label'
is the name of the tool displayed to the user.'color'
is the color used to render the drawn function.
A listing of all the built-in plugins can be found at SketchResponse Plugins.
problemconfig = sketchresponse.config({
'width': 750,
'height': 420,
'xrange': [-2.35, 2.35],
'yrange': [-1.15, 1.15],
'xscale': 'linear',
'yscale': 'linear',
'coordinates': 'cartesian',
'plugins': [
{'name': 'axes'},
{'name': 'freeform', 'id': 'f', 'label': 'Function f(x)', 'color':'blue'},
]
})
The above problem configuration settings will create a javascript tool that looks something like the image below.
Define the grader callback function
@sketchresponse.grader
def grader(f):
gf = GradeableFunction.GradeableFunction(f)
if not gf.is_straight():
return False, 'Not straight'
return True, 'Good Job'
The grader callback function implements the function passed to the sketchinput grader to evaluate the data sent from the javascript tool.
The arguments of the grader function are the 'id'
values as defined in the
problem configuration above. E.g. in our problem configuration we enabled the
freeform drawing tool with id 'f' and we have a corresponding argument f in
the signature of the function that will be automatically unpacked.
Before we can execute any grading helper functions on the data, we must
instantiate the data as a GradeableFunction
.
In this simple example all we are checking is that the submitted function
defines a straight line over its entire domain. We are not checking for slope
of the line. To do this we call the grader helper function gf.is_straight()
.
is_straight()
returns a boolean value. The full API documentation for the
grader helper functions can be found at SketchResponse API.
And that's it! Those two simple blocks of code complete our first grader script. Admittedly this particular script doesn't do much. Check out the Complex Grader example for a more realistic grader tutorial on an example math problem.
Testing the script
Once the script is written, you can run the script in the local testing server. See the Test a Grading Script on a Local Server tutorial for details on installing and running the testing server.
There is already a copy of this grader script in the grader_scripts
directory so all you need to do is start the server and point your browser of choice at the url:
http://localhost:5000/simple_grader
You should see the configured Sketch Tool. If you draw a straight(ish) line and press the check button you will get accept message. If the line is not straight enough, you will get a reject message.