Skip to content
Jaime Bermudez edited this page Jul 10, 2014 · 3 revisions

The Sections module has functions that correspond to what's described in the [Canvas API Doc] (https://canvas.instructure.com/doc/api/sections.html). Below, we'll see how to make some calls within this module.

NOTE: Refer to the RequestContext doc for details on how to set up a RequestContext, which is required for all method calls.

List course sections

To obtain and print a list of the sections for a given course:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
course_id = 1234  # Replace with a real course id
results = sections.list_course_sections(request_context, course_id)
for idx, section in enumerate(results.json()):
    print "section %d has data %s" % (idx, section)

To include the set of students within a list of sections:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
course_id = 1234  # Replace with a real course id
results = sections.list_course_sections(request_context, course_id, include='students')
# Collect all sections with students
sections_with_students = [section for sections in results.json() if len(section['students']) > 0]
print "sections with student data are %s" % sections_with_students

Create a course section

To create a course section with optional SIS section id:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
course_id = 1234  # Replace with a real course id
section_name = "My Test Section"
sis_id = "999-ABC-1"
results = sections.create_course_section(request_context, course_id, section_name, sis_section_id=sis_id)
print "new section was created as: %s" % results.json()

Update a section

To update the name of a section:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
section_id = 4321  # Replace with a real section id
new_section_name = "Updated section name"
results = sections.edit_section(request_context, section_id, new_section_name)
print "updated section details: %s" % results.json()

Get section details

To get detailed information on a specific section:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
section_id = 4321
results = sections.get_section_information_sections(request_context, section_id)
print "section details: %s" % results.json()

Delete a section

Deleting a section returns the former Section:

from canvas_sdk.methods import sections

request_context = ...  # Create or retrieve a stored one
section_id = 999  # Replace with a real section id
results = sections.delete_section(request_context, course_id)
print "The section that was just deleted is: %s" % results.json()

Clone this wiki locally