Skip navigation
All Places > Canvas Developers > Blog > Author: Tyler Clair

Canvas Developers

2 Posts authored by: Tyler Clair
Tyler Clair

Sections to Groups Script

Posted by Tyler Clair Oct 26, 2016

From a related post I was tagged in, the question was asked how to automatically create groups based off of sections in a course, Automatic Group creation for students in same section. I took some time and wrote up a script in Python 3 that based off of a course ID and group category name it will do the following:


  1. Create the Group category with the name you specified
  2. Get a list of sections in a course along with the list of students in each section
  3. Create groups based off of those sections
  4. Create the membership for each student in the group


The one thing that this doesn't do is maintain the groups if a student enrolls in a course or if a student is moved to a different section. Those tasks will have to be done manually but when initially ran it will be fairly easy to make those small changes from within Canvas.


I have posted the code on my github page and it can be access here:

A best practice you should adopt is storing sensitive information used in scripts in config files rather than directly in the scripts. This helps ensure a few important things:

  1. It is easier for you to make changes when you only have to change one file rather than multiple files.
  2. You can reference different configuration items like the production, beta, and test URLs of your Canvas instance. In my configuration file I have tokens for different roles and permissions as well as the different instance URLs.
  3. You are less likely to accidentally leak sensitive information. This is very important when you use a version control system, such as GitHub to share or archive your scripts. Unless you pay for a Github subscription to have private repositories, your code is publicly visible. There are people and groups constantly scanning Github and other publicly available version control systems for API tokens, keys, secrets, usernames, and passwords for exploitation. Luckily most version control systems include a way to ignore or prevent certain files from being uploaded to a repository. So you will want to add any config files containing sensitive information to that ignore list.


I use Python 3 as my go to scripting environment and a few years ago I adopted the use of configuration files using the configparser module One of the greatest strengths that configparser has is the ability to separate key value pairs in sections. In my config file I have an instance and an auth section. The instance section contains the different Canvas instance URLs appropriately named prod, beta, and test.


prod =
beta =
test =
catalog =
token = 1234567890……
teacher_token = 0987654321…..
student_token = 9045387230…..


You can name this anything you want, have any extension you want, and place it anywhere as well, e.g: config.ini, config/secrets.cfg. If you manage multiple instances you could create separate config files for each instance containing the different URLs and any tokens you need access to. In the event that a config file may be leaked or exposed, it will be isolated to that one file.


The code below is how I reference the config file, it makes it very convenient to drop this block of code into a new python file and I am ready to make some API calls.


from configparser import ConfigParser
config = ConfigParser()'config/config.ini')
token = config.get('auth', 'token')
domain = config.get('instance', 'prod')
headers = {'Authorization': 'Bearer {}'.format(token)}