Here are some examples of common tasks that can be performed easily and quickly with CanvasAPI.
All other examples assume you have already created a canvas
object like so:
# Import the Canvas class
from canvasapi import Canvas
# Canvas API URL
API_URL = "https://example.com"
# Canvas API key
API_KEY = "p@$$w0rd"
# Initialize a new Canvas object
canvas = Canvas(API_URL, API_KEY)
# Grab the account to create the user under
account = canvas.get_account(1)
user = account.create_user(
user={
'name': 'New User'
},
pseudonym={
'password': 'secure123',
'sis_user_id': 'new_user'
}
)
courses = account.get_courses()
for course in courses:
print(course)
# Grab a user with the SIS ID of 'some_id'
user = canvas.get_user('some_id', 'sis_login_id')
# Grab user with ID of 1
user = canvas.get_user(1)
# Grab the user
user = canvas.get_user(1)
user.edit(
user={'name': 'New Name'}
)
page_views = user.get_page_views()
logins = user.get_user_logins()
for login in logins:
print(login)
# Grab course with ID of 123456
course = canvas.get_course(123456)
Using :func:`canvasapi.course.Course.get_users`:
users = course.get_users()
for user in users:
print(user)
users = course.get_users(enrollment_type=['student'])
for user in users:
print(user)
type_list = ['teacher', 'ta', 'designer']
users = course.get_users(enrollment_type=type_list)
for user in users:
print(user)
users = course.get_users(
enrollment_type=['teacher', 'student']
enrollment_state=['active', 'invited']
)
for user in users:
print(user)
Using :func:`canvasapi.course.Course.update`:
print(course.name) # prints 'Old Name'
course.update(course={'name': 'New Name'})
print(course.name) # prints 'New Name'
Either an ISO8601 format date string:
course.update(
course={
'start_at': '2018-01-01T00:01Z',
'end_at': '2018-12-31T11:59Z'
}
)
Or a Python DateTime object:
from datetime import datetime
start_date = datetime(2018, 1, 1, 0, 1)
end_date = datetime(2018, 12, 31, 11, 59)
course.update(
course={
'start_at': start_date,
'end_at': end_date
}
)
Using :func:`canvasapi.course.Course.conclude`:
course.conclude()
Using :func:`canvasapi.course.Course.delete`:
course.delete()
Using :func:`canvasapi.course.Course.get_assignment`:
# Grab assignment with ID of 1234
assignment = course.get_assignment(1234)
print(assignment)
Using :func:`canvasapi.course.Course.get_assignments`:
assignments = course.get_assignments()
for assignment in assignments:
print(assignment)
assignments = course.get_assignments(bucket='ungraded')
for assignment in assignments:
print(assignment)
Using :func:`canvasapi.course.Course.create_assignment`:
new_assignment = course.create_assignment({
'name': 'Assignment 1'
})
print(new_assignment)
new_assignment = course.create_assignment({
'name': 'Assignment 2',
'submission_types': ['online_upload', 'online_text_entry', 'online_url']
})
print(new_assignment)
from datetime import datetime
new_assignment = course.create_assignment({
'name': 'Assignment 3',
'submission_types': ['online_upload'],
'allowed_extensions': ['docx', 'doc', 'pdf'],
'notify_of_update': True,
'points_possible': 100,
'due_at': datetime(2018, 12, 31, 11, 59),
'description': 'Please upload your homework as a Word document or PDF.',
'published': True
})
print(new_assignment)
Using :func:`canvasapi.assignment.Assignment.edit`:
updated_assignment = assignment.edit(assignment={'name': 'New Name'})
print(updated_assignment)
updated_assignment = assignment.edit(
assignment={'submission_types': ['on_paper']}
)
print(updated_assignment)
Using :func:`canvasapi.assignment.Assignment.delete`:
assignment.delete()
Using :func:`canvasapi.submission.Submission.edit`:
# Set `score` to a Int value
submission = assignment.get_submission(student_id)
submission.edit(submission={'posted_grade':score})
# Set added points as an Int variable
added_points = 2
submissions = assignment.get_submissions()
for submission in submissions:
# Handle an unscored assignment by checking the `score` value
if submission.score is not None:
score = submission.score + added_points
else:
# Treat no submission as 0 points
score = 0 + added_points
submission.edit(submission={'posted_grade': score})