### Install JupyterQuiz using pip Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md Use this command to install the JupyterQuiz package. Ensure you have pip installed and accessible in your environment. ```bash pip install jupyterquiz ``` -------------------------------- ### Install and Display Quiz in JupyterLite Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md Installs the jupyterquiz package using micropip and displays a quiz from a given URL in JupyterLite. ```python import micropip await micropip.install('jupyterquiz') from jupyterquiz import display_quiz git_url='https://raw.githubusercontent.com/jmshea/Foundations-of-Data-Science-with-Python/main/questions/' display_quiz(git_url+'ch1.json') ``` -------------------------------- ### Import display_quiz and set Git Path Source: https://github.com/jmshea/jupyterquiz/blob/main/preserve-responses.ipynb Import the necessary function from jupyterquiz and define the base path for quiz questions stored on GitHub. This setup is required before displaying a quiz. ```python from jupyterquiz import display_quiz git_path="https://raw.githubusercontent.com/jmshea/jupyterquiz/main/examples/" ``` -------------------------------- ### String Question JSON Schema Example Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md Example JSON structure for a string question. Supports multiple correct answers, case sensitivity options, fuzzy matching, and custom feedback. ```json { "question": "Who was the 35th president (1961-63) of the US?", "type": "string", "answers": [ { "answer": "John F. Kennedy", "correct": true, "feedback": "Correct. John F. Kennedy was the 35th president of the U.S.", "match_case": false, "fuzzy_threshold": 0.80 }, { "answer": "JFK", "correct": true, "feedback": "Correct. John F. Kennedy was the 35th president of the U.S." }, { "answer": "Kennedy", "correct": false, "feedback": "Please also provide the first name.", "match_case": false } ] } ``` -------------------------------- ### Define Example Quiz Data Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Defines a sample quiz question in a Python dictionary format. This structure is used as the source for creating quizzes. ```python example=[ { "question": "The variable mylist is a Python list. Choose which code snippet will append the item 3 to mylist.", "type": "multiple_choice", "answers": [ { "code": "mylist+=3", "correct": False }, { "code": "mylist+=[3]", "correct": True }, { "code": "mylist+={3}", "correct": False } ] } ] ``` -------------------------------- ### Display Quiz from URL Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads and displays a quiz from a JSON file hosted on GitHub. This is the preferred method for loading questions. ```python display_quiz(git_path+"questions.json") ``` -------------------------------- ### Import JupyterQuiz Library Source: https://github.com/jmshea/jupyterquiz/blob/main/mve.ipynb Import the necessary function to display quizzes. ```python from jupyterquiz import display_quiz ``` -------------------------------- ### Display Quiz with Custom Colors from Local JSON Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads a quiz from a local JSON file and applies custom colors to specific quiz elements. ```python display_quiz("examples/questions.json", colors={ '--jq-many-choice-bg': '#224dea', '--jq-multiple-choice-bg': '#a45995' } ) ``` -------------------------------- ### Display Quiz from Local JSON File Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads and displays a quiz from a local JSON file. This method is convenient for local testing. ```python display_quiz("examples/questions.json", ) ``` -------------------------------- ### Display Quiz with 'fdsp' Color Scheme Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads and displays a quiz from a JSON file using the 'fdsp' color scheme for a customized appearance. ```python display_quiz(git_path+"questions.json", colors='fdsp') ``` -------------------------------- ### Save Questions to JSON File (for development) Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb This commented-out code snippet shows how to save a Python dictionary of questions to a JSON file, useful during development. ```python # #Leave this here for when doing question development # import json # with open("examples/questions.json", "w") as file: # json.dump(questions, file, indent=4) ``` -------------------------------- ### Import display_quiz Function Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Imports the necessary function for displaying quizzes. This is a prerequisite for all subsequent quiz operations. ```python from jupyterquiz import display_quiz git_path="https://raw.githubusercontent.com/jmshea/jupyterquiz/main/examples/" ``` -------------------------------- ### Display a Manually Selected Subset of Questions Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Displays a quiz using a manually created list of questions. This allows for precise control over which questions are presented. ```python github_preview=[questions[0]]+[questions[2]] ``` ```python display_quiz(github_preview) ``` -------------------------------- ### Load Questions from Python Dict and Customize Appearance Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads quiz questions from a Python dictionary and applies a 'flat' look by setting the border radius to 0. This is useful for rapid question development. ```python import json with open("examples/questions.json", "r") as file: questions=json.load(file) display_quiz(questions, border_radius=0) ``` -------------------------------- ### Load questions from JSON file Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Loads quiz questions from a JSON file into a Python list. Ensure the file path is correct. ```python import json with open("../examples/questions.json", "r") as file: questions=json.load(file) ``` -------------------------------- ### Display Quiz with Response Preservation Source: https://github.com/jmshea/jupyterquiz/blob/main/preserve-responses.ipynb Use the `display_quiz` function to render a quiz from a JSON file. Set `preserve_responses=True` to enable the feature for saving student answers. This requires the quiz questions to be accessible via the provided `git_path`. ```python display_quiz(git_path+"questions.json", preserve_responses = True) ``` -------------------------------- ### Display a Random Subset of Quiz Questions Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Loads and displays a specified number of random questions from a quiz JSON file. The second argument to display_quiz specifies the number of questions to sample. ```python display_quiz(git_path+"questions.json",2) ``` -------------------------------- ### Display String Answer Quiz Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Renders the defined string-based quiz to the Jupyter output. ```python display_quiz(example_string) ``` -------------------------------- ### Display Quiz Source: https://github.com/jmshea/jupyterquiz/blob/main/mve.ipynb Renders the defined quiz questions in a Jupyter environment. This function is used after defining the quiz questions. ```python display_quiz(questions) ``` -------------------------------- ### Display Quiz with Custom Styling Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Use this function to display a quiz from a JSON file with custom styling options like border radius, question alignment, and maximum width. ```python display_quiz(git_path+"questions.json", border_radius=0, question_alignment='center', max_width=1000) ``` -------------------------------- ### Display Quiz Directly Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Displays a quiz to the user by passing the Python dictionary containing the question data directly to the `display_quiz` function. ```python from jupyterquiz import display_quiz display_quiz(example) ``` -------------------------------- ### Define String Answer Quiz Question Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Structure a quiz question where the answer is a string. Supports multiple correct answers and fuzzy matching. ```python example_string = [{ "question": "Who was the 35th president (1961-63) of the US?" , "type": "string", "answers": [ { "answer": "John F. Kennedy", "correct": True, "feedback": "Correct. John F. Kennedy was the 35th president of the U.S.", "match_case": False, "fuzzy_threshold": 0.78 }, { "answer": "JFK", "correct": True, "feedback": "Correct. John F. Kennedy was the 35 preside of the U.S." }, { "answer": "Kennedy", "correct": False, "feedback": "Please also provide the first name.", "match_case": False }, ] }] ``` -------------------------------- ### Display Multiple Choice Quiz Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Renders the defined multiple-choice quiz to the Jupyter output. ```python display_quiz(example_link) ``` -------------------------------- ### Display Quiz from HTML Element Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Call this Python function to display a quiz loaded from a specified HTML element ID. Ensure the element contains Base64-encoded question data. ```python display_quiz("#question2") ``` -------------------------------- ### Import jsonschema validate function Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Imports the necessary validation function from the jsonschema library. ```python from jsonschema import validate ``` -------------------------------- ### Define Many Choice Question in JSON Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md Use this structure to define a many-choice question. Ensure 'question', 'type', and 'answers' are provided. Each answer requires 'answer' text and a 'correct' boolean, with optional 'feedback'. ```json { "question": "Choose all of the following that can be included in Jupyter notebooks?", "type": "many_choice", "answers": [ { "answer": "Text and graphics output from Python", "correct": true, "feedback": "Correct." }, { "answer": "Typeset mathematics", "correct": true, "feedback": "Correct." }, { "answer": "Python executable code", "correct": true, "feedback": "Correct." }, { "answer": "Formatted text", "correct": true, "feedback": "Correct." }, { "answer": "Live snakes via Python", "correct": false, "feedback": "I hope not." } ] } ``` -------------------------------- ### Save Numeric Schema to File Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Saves the defined num_schema to a JSON file named 'num_schema.json' with an indentation of 4 spaces for readability. ```python with open("num_schema.json", "w") as file: json.dump(num_schema, file, indent=4) ``` -------------------------------- ### Save Multiple Choice Schema to File Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Saves the defined mc_schema to a JSON file named 'mc_schema.json' with an indentation of 4 spaces for readability. ```python with open("mc_schema.json", "w") as file: json.dump(mc_schema, file, indent=4) ``` -------------------------------- ### Define Numerical Question in JSON Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md Structure for a numerical question. Includes 'question', 'type', and 'answers'. 'Precision' can be set for input rounding. Answers can be 'value', 'range', or 'default', each with optional 'feedback' and a 'correct' boolean for value/range types. ```json { "question": "Enter the value of pi (will be checked to 2 decimal places):", "type": "numeric", "precision": 2, "answers": [ { "type": "value", "value": 3.14, "correct": true, "feedback": "Correct." }, { "type": "range", "range": [ 3.142857, 3.142858], "correct": true, "feedback": "True to 2 decimal places, but you know pi is not really 22/7, right?" }, { "type": "range", "range": [ -100000000, 0], "correct": false, "feedback": "pi is the AREA of a circle of radius 1. Try again." }, { "type": "default", "feedback": "pi is the area of a circle of radius 1. Try again." } ] } ``` -------------------------------- ### Test Quiz Error Handling Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Use this Python snippet to test the error handling of the quiz display function by targeting a non-existent or malformed question element. ```python # Test new error checking code display_quiz("#question3") ``` -------------------------------- ### Define Schema for Numeric Questions Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Defines the JSON schema for validating numeric questions. It supports single value, range, or default answers, each with correctness and feedback. ```python num_schema={ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://github.com/jmshea/jupyterquiz/num_schema.json", "title": "JupyterQuiz Numeric Question", "description": "Schema for Multiple or Many Choice Questions in JupyterQuiz", "type": "object", "properties": { "question": {"type": "string"}, "type": {"type": "string", "pattern": "numeric"}, "precision": {"type": "integer"}, "answers":{ "type": "array", "items": { "anyOf": [ {"type": "object", "properties": { "value": {"type": "number"}, "correct": {"type": "boolean"}, "feedback": {"type": "string"} }, "required":["value", "correct"] }, {"type": "object", "properties": { "range": { "type": "array", "minItems": 2, "maxItems": 2}, "correct": {"type": "boolean"}, "feedback": {"type": "string"} }, "required":["range", "correct"] }, {"type": "object", "properties": { "type": {"type": "string", "pattern": "default"}, "feedback": {"type": "string"} }, "required":["type", "feedback"] } ] } } } } ``` -------------------------------- ### Define Multiple Choice Quiz Question Source: https://github.com/jmshea/jupyterquiz/blob/main/test.ipynb Structure a multiple-choice question for the quiz. Ensure the 'code' field in answers is correctly formatted for Python list appending. ```python example_link = [{ "question": "The variable mylist is a Python list. " + \ "Choose which code snippet will append the item 3 to mylist. " + \ "See [W3Schools: Python Join Two Lists]" + \ "(https://www.w3schools.com/python/gloss_python_join_lists.asp) for reference.", "type": "multiple_choice", "answers": [ { "code": "mylist+=3", "correct": False }, { "code": "mylist+=[3]", "correct": True }, { "code": "mylist+={3}", "correct": False } ] }] ``` -------------------------------- ### Define Quiz Questions Source: https://github.com/jmshea/jupyterquiz/blob/main/mve.ipynb Define a list of questions for the quiz, including multiple choice and numeric types. Each question has associated answers with feedback. ```python questions = \ [{'question': "Which of these is the ratio of a circle's circumference to its diameter?", 'type': 'multiple_choice', 'answers': [{'answer': 'pi', 'correct': True, 'feedback': 'Correct.'}, {'answer': 'frac227', 'correct': False, 'feedback': 'frac227 is only an approximation to the true value.'}, {'answer': '3', 'correct': False, 'feedback': 'This is a crude approximation to the true value.'}, {'answer': 'tau', 'correct': False, 'feedback': "True for the ratio of the circle's circumference to its radius, not diameter."}]}, {'question': 'Enter the value of pi to 2 decimal places.', 'type': 'numeric', 'answers': [{'type': 'value', 'value': 3.14, 'correct': True, 'feedback': 'Correct.'}, {'type': 'range', 'range': [3.142857, 3.142858], 'correct': True, 'feedback': 'True to 2 decimal places, but you know pi is not really 22/7, right?'}, {'type': 'range', 'range': [-100000000, 0], 'correct': False, 'feedback': 'pi is the AREA of a circle of radius 1. Try again.'}, {'type': 'default', 'feedback': 'pi is the area of a circle of radius 1. Try again.'}]}] ``` -------------------------------- ### Display Quiz from Hidden HTML Element Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Renders a quiz by pulling its data from a hidden HTML element. The element's ID or class is passed to `display_quiz` prefixed with '#'. This method is useful for hiding source data. ```python # May not work first time if using Kernel -> Restart and Run... # I believe this is because of delays in the browser generating the # class lists needed for JL >= 4.2.5. Just rerun this if necessary display_quiz("#question1") ``` -------------------------------- ### Default Color Dictionary for JupyterQuiz Source: https://github.com/jmshea/jupyterquiz/blob/main/README.md This dictionary defines the default color scheme for various elements of the JupyterQuiz interface. It can be customized by passing a modified dictionary to the `colors` keyword argument. ```Python color_dict = { '--jq-multiple-choice-bg': '#6f78ffff', # Background for the question part of multiple-choice questions '--jq-mc-button-bg': '#fafafa', # Background for the buttons when not pressed '--jq-mc-button-border': '#e0e0e0e0', # Border of the buttons '--jq-mc-button-inset-shadow': '#555555', # Color of inset shadow for pressed buttons '--jq-many-choice-bg': '#f75c03ff', # Background for question part of many-choice questions '--jq-numeric-bg': '#392061ff', # Background for question part of numeric questions '--jq-numeric-input-bg': '#c0c0c0', # Background for input area of numeric questions '--jq-numeric-input-label': '#101010', # Color for input of numeric questions '--jq-numeric-input-shadow': '#999999', # Color for shadow of input area of numeric questions when selected '--jq-incorrect-color': '#c80202', # Color for incorrect answers '--jq-correct-color': '#009113', # Color for correct answers '--jq-text-color': '#fafafa' # Color for question text } ``` -------------------------------- ### Encode Python Data to Base64 Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Use this Python snippet to encode a JSON-formatted data structure into Base64. This is useful for preparing quiz data to be hidden. ```python from base64 import b64encode b64encode(bytes(json.dumps(example), 'utf8')) ``` -------------------------------- ### Convert Quiz Data to JSON Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Converts a Python dictionary containing quiz data into a JSON string. This is a preparatory step for embedding the data in HTML. ```python import json ``` ```python json.dumps(example) ``` -------------------------------- ### Validate Numeric Question against Schema Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Validates a specific numeric question object against the defined num_schema. This ensures the numeric question data conforms to the expected structure. ```python validate(numeric, num_schema) ``` -------------------------------- ### Embed Base64 Encoded Quiz Data in HTML Source: https://github.com/jmshea/jupyterquiz/blob/main/HideQuiz.ipynb Embed Base64-encoded quiz data within a hidden HTML span element. This is how JupyterQuiz can load unreadable question data. ```html ``` -------------------------------- ### Define Schema for Multiple Choice Questions Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Defines the JSON schema for validating multiple-choice and many-choice questions. It specifies question text, type, and a list of possible answers with their correctness and feedback. ```python mc_schema={ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://github.com/jmshea/jupyterquiz/mc_schema.json", "title": "JupyterQuiz Multiple or Many Choice Quiz", "description": "Schema for Multiple or Many Choice Questions in JupyterQuiz", "type": "object", "properties": { "question": {"type": "string"}, "type": {"type": "string", "pattern": "multiple_choice|many_choice"}, "answers": {"type": "array", "items":{ "type": "object", "properties": { "answer": {"type": "string"}, "correct": {"type": "boolean"}, "feedback": {"type": "string"}, "answer_cols": {"type": "number"} }, "required": ["answer", "correct"] } }, "code": {"type":"string"} }, "required": ["type", "question", "answers"] } ``` -------------------------------- ### Validate Multiple Choice Question against Schema Source: https://github.com/jmshea/jupyterquiz/blob/main/schema/schema.ipynb Validates a specific multiple-choice question object against the defined mc_schema. This ensures the question data conforms to the expected structure. ```python validate(multiple_choice, mc_schema) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.