// Numbas version: finer_feedback_settings {"name": "Fetch data from an external source before variable generation", "extensions": [], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Fetch data from an external source before variable generation", "tags": [], "metadata": {"description": "
This question shows how to use the question's JavaScript preamble to request data from an external source, and use that data in question variables.
\nNote that this means the question only works when the external source is available. Use this very carefully, and avoid it if you possibly can!
", "licence": "Creative Commons Attribution 4.0 International"}, "statement": "The preamble of this question calls https://fakerapi.it/api/v1/books?_quantity=1 to get information about a randomly generated fake book. This particular task could be done entirely in Numbas, but it demonstrates the principle of requesting data from an external source.
\nIf the request succeeds, then the variable book
is set with the returned data. The variables title
and author
are both defined to extract information from book.
The data about the book is stored in this variable once it's received from the external API. A default value is given so that the editor doesn't complain about an undefined variable, and to document the variable's structure.
", "templateType": "json", "can_override": false}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["book", "author", "title"], "variable_groups": [], "functions": {}, "preamble": {"js": "const url = 'https://fakerapi.it/api/v1/books?_quantity=1';\nreturn fetch(url, {headers: {'Accept': 'application/json'}}).then(response => {\n return response.json().then(({data}) => {\n const [book] = data;\n question.scope.setVariable('book', Numbas.jme.wrapValue(book));\n })\n});", "css": ""}, "parts": [{"type": "information", "useCustomName": false, "customName": "", "marks": 0, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "exploreObjective": null, "prompt": "This question is about the book \"{title}\" by {author}.
"}], "partsMode": "all", "maxMarks": 0, "objectives": [], "penalties": [], "objectiveVisibility": "always", "penaltyVisibility": "always", "type": "question", "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}