How to talk to Canvas, programmatically
Canvas is a popular commercial LMS our university recently migrated to. Canvas offers an API1 to implement typical LMS activities.
An API is a way to programmatically implement actions (like creating and populating a quiz) instead of using the buttons and text inputs at the GUI. Once set up, this can make time-consuming tasks (e.g., setting up quizzes) extremely fast. So, whatever time you invest in getting familiar with the API will save you countless hours in the future. More importantly, freeing you from the mundane actions of setting up quizzes will allow you to use these quizzes in more creative ways to realises cool things like interleaving and spaced recall (for example, see Weinstein, Nunes, and Karpicke (2016) or Weinstein, Madan, and Sumeracki (2018)).
In this post, I will share some code I wrote to create and populate MCQ quizzes. The code does not have the Apple-like polish. However, I have laid out the code so anyone new to programming can still use it.
The code
- The code lives on the Github repository: github.com/chammika-udalagama/Canvas-API.
If you are new to Github you can just download the repository as a zip file and then unzip, and you are ready to go. - Requirements:
- You will need Python installed on your machine. Alternatively, you can use a service like Google’s Colab, but be careful about keeping your API-KEY secure (see below).
How to use the code
- Create an API key to access your Canvas account (You need to do this only once).
- Create an Excel file with your questions and quiz information using the template provided.
- Run the Python script.
How to generate an API Key
Here is how you can create an API key
- Log in to Canvas.
- Click on your profile picture/name and select “Settings.”
- Navigate to “Approved Integrations” in the sidebar.
- Generate a new API key by providing the necessary details.
- Copy the generated key.
- Treat the key as a password and keep it securely. You can regenerate if compromised.
How to use the key
To use the key, create a new file called constants.py
with the following content:
= "COPY_YOUR_API_TOKEN_HERE" API_TOKEN
Please look at the example Excel file (Canvas-Question-Bank.xlsx
) provided to see how the details of quizzes and questions have been declared. It should be self-explanatory so I won’t spend time on the details. However, it is important to note the following:
My Quiz Label
should be unique and contain no spaces.- Use
My Quiz Label
to link the questions to the quizzes. - You can include the same question in more than one quiz.
- Always place the correct answer as option ‘A.’
Once you have prepared your Excel file, you can just run the script in the terminal (or Anaconda prompt) by invoking:
python main.py
References
Footnotes
API: Application Programme Interface↩︎