The Instructure Community will enter a read-only state on November 22, 2025 as we prepare to migrate to our new Community platform in early December.
Read our blog post for more info about this change.
Found this content helpful? Log in or sign up to leave a like!
i'm trying to pull grades for students using the python canvasapi. using get_enrollments, i can get the course grade, but i'd like to get the students' grades for each assignment group. for example, i have assignment groups for midterm1, midterm2, final, and programming assignments. is that available through Enrollment, or should i be looking somewhere else?
i kind of figured out how to do it with graphql:
Grade = namedtuple('Grade', ['category', 'grade'])
# canvas is a Canvas object and course is a Course object
results = canvas.graphql('query { course(id: "' + str(course.id) + '''") {
assignmentGroupsConnection {
nodes { name
gradesConnection {
edges { node { currentScore
enrollment { user { name } }
} } } } } } }''')
grades_by_student = defaultdict(list)
for assignment_group in results['data']['course']['assignmentGroupsConnection']['nodes']:
category = assignment_group['name']
for grade in assignment_group['gradesConnection']['edges']:
score = grade['node']['currentScore']
if score:
name = grade['node']['enrollment']['user']['name']
grades_by_student[name].append(Grade(category, score))
for i in grades_by_student.items():
print(f'{i[0]}: {",".join([g.category+"="+str(g.grade) for g in i[1]])}')
i'm curious if there is a better way to do it.
If you're getting the results you want, then graphql is the way to go -- they announced the other day that moving forward the api is going to be implemented in graphql so we're all going to learn it, I think.
One way you can make things easier on yourself is to alias the result keys so that the code you write to handle the results is easier to read (and the raw results are easier to read as well). All you have to do is put a valid new name in front of whatever key you want to clarify, e.g. instead of name, you call it course_name, i.e. course_name: name
So for your query:
query GradesByAssignment($CourseCanvasID: ID) {
course(id: $CourseCanvasID) {
assignment_groups: assignmentGroupsConnection {
assignments: nodes {
assignment_name: name
grade_list: gradesConnection {
grades: nodes {
currentScore
enrollment {
user {
user_canvas_id: _id
user_display_name: name
} } } } } } } }
I'm not saying it buys you much, but sometimes graphql queries end up having a bunch of keys that are name or _id and it gets tedious to read.
I've never seen any way to get the assignment group scores before, so good find. I haven't used graphQL much, but that information isn't available through the API without using graphQL.
Extracting information from graphQL is a pain without some kind of library to merge records. You'll basically have to go through and re-assemble the data like you are. There are various ways to get the information out, but it appears you cannot get it by starting with the user, which would make it easier for you.
You might want to include the id and state properties. GraphQL gives you all of the data, even deleted entries and for items that haven't been submitted. The ID would allow you to handle the case where two student have the same name.
Community helpTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in