CanvasGroup¶
CanvasGroupy.canvas.CanvasGroup
¶
Manage Canvas LMS group operations including roster, grading, and messaging.
Provides methods to authenticate with the Canvas API, manage courses and group categories, create and populate groups, post grades, and send messages to students.
Attributes:
| Name | Type | Description |
|---|---|---|
API_URL |
The Canvas instance base URL. |
|
canvas |
Authenticated Canvas API client. |
|
course |
The currently selected Canvas course. |
|
group_category |
The active group category (group set). |
|
users |
List of enrolled students in the course. |
|
assignment |
The currently linked assignment for grading. |
|
verbosity |
Controls output verbosity (0 = silent, 1 = print all). |
Source code in CanvasGroupy/canvas.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | |
__init__(credentials_fp='', API_URL='https://canvas.ucsd.edu', course_id='', group_category='', verbosity=1)
¶
Initialize a CanvasGroup instance and optionally authenticate and configure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials_fp
|
Path to the credentials JSON file containing API tokens. See the template at the project repository. |
''
|
|
API_URL
|
The Canvas instance base URL (e.g., |
'https://canvas.ucsd.edu'
|
|
course_id
|
The Canvas course ID, found in the course URL. |
''
|
|
group_category
|
Name of the target group category (group set). |
''
|
|
verbosity
|
Controls output verbosity (0 = silent, 1 = print all). |
1
|
Source code in CanvasGroupy/canvas.py
assign_canvas_group(group_name, group_members, in_group_category)
¶
Create a Canvas group and assign members to it.
Sets the group category, creates a new group, and adds the specified students as members.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Display name for the new group on Canvas. |
required |
group_members
|
[str]
|
List of student SIS Login IDs (email prefixes). |
required |
in_group_category
|
str
|
Name of the group category (group set) the new group belongs to. |
required |
Returns:
| Type | Description |
|---|---|
(Group, [str])
|
A tuple of (created group object, list of email prefixes |
(Group, [str])
|
for students who could not be added). |
Source code in CanvasGroupy/canvas.py
auth_canvas(credentials_fp)
¶
Authenticate with the Canvas API using a credentials file.
Reads the Canvas API token from the JSON credentials file and initializes the Canvas API client. Verifies the token by fetching the activity stream summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials_fp
|
str
|
Path to the JSON file containing a
|
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the credentials file does not exist. |
InvalidAccessToken
|
If the token is invalid. |
Source code in CanvasGroupy/canvas.py
check_github_usernames(github_usernames, send_canvas_email=False, send_undone_reminder=False, quiz_url='')
¶
Batch validate GitHub usernames and optionally notify students.
Checks each GitHub username against the GitHub API. Optionally sends Canvas messages to students with invalid usernames and to students who have not yet submitted the quiz.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
github_usernames
|
dict
|
A dict mapping email prefixes to GitHub
usernames, typically from |
required |
send_canvas_email
|
If True, send a Canvas notification to students with invalid GitHub usernames. |
False
|
|
send_undone_reminder
|
If True, send a Canvas reminder to students who have not submitted the quiz. |
False
|
|
quiz_url
|
URL of the quiz to include in reminder messages. |
''
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict of email prefixes to GitHub usernames that could not |
dict
|
be validated on GitHub. |
Source code in CanvasGroupy/canvas.py
create_conversation(recipients, subject, body)
¶
Send a Canvas message (conversation) to a student.
Creates a new conversation in the context of the current course.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipients
|
int
|
Recipient Canvas user ID, or a course/group ID
prefixed with |
required |
subject
|
str
|
Subject line of the conversation. |
required |
body
|
str
|
The message body text. |
required |
Returns:
| Type | Description |
|---|---|
Conversation
|
The created Canvas conversation object. |
Source code in CanvasGroupy/canvas.py
create_group(params)
¶
Create a group under the currently active group category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict
|
A dictionary of parameters for the Canvas group
creation API, which must include a |
required |
Returns:
| Type | Description |
|---|---|
Group
|
The newly created group object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no group category has been set or created. |
Source code in CanvasGroupy/canvas.py
create_group_category(params)
¶
Create a new group category (group set) in the current course.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict
|
A dictionary of parameters for the Canvas group
category API. See the Canvas API documentation for
|
required |
Returns:
| Type | Description |
|---|---|
GroupCategory
|
The newly created group category object. |
Source code in CanvasGroupy/canvas.py
fetch_username_from_quiz(quiz_id, col_index=7)
¶
Extract GitHub usernames from a Canvas quiz student analysis.
Downloads the student analysis report for the specified quiz, parses the CSV, and builds a mapping from student email prefixes to their submitted GitHub usernames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quiz_id
|
int
|
The Canvas quiz ID containing GitHub username submissions. |
required |
col_index
|
The zero-based column index in the generated CSV that contains the GitHub username question response. |
7
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict mapping student email prefixes (SIS Login IDs) to |
dict
|
their submitted GitHub usernames. |
Source code in CanvasGroupy/canvas.py
get_course()
¶
Get the current course object.
Returns:
| Type | Description |
|---|---|
|
The currently selected Canvas course object, or None if no |
|
|
course has been set. |
get_email_by_name(name_fussy)
¶
Look up a student's email prefix by a partial name match.
Performs a case-insensitive substring search against all student names in the roster and returns the first matching email prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name_fussy
|
str
|
A partial or full student name to search for (first name, last name, or substring). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The email prefix (SIS Login ID) of the first matching student. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no student name contains the search string. |
Source code in CanvasGroupy/canvas.py
get_group_categories()
¶
List all group categories (group sets) in the current course.
Fetches every group category from the Canvas course and caches
them in self.group_categories.
Returns:
| Type | Description |
|---|---|
dict
|
A dict mapping category names to their GroupCategory objects. |
Source code in CanvasGroupy/canvas.py
get_groups(category_name='')
¶
Get groups and their members in the current or specified category.
Returns a dictionary mapping group names to lists of member email
prefixes. If category_name is provided, the category is set
first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
|
Optional group category name. If empty, uses the currently active group category. |
''
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict mapping group names to lists of student email prefixes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no group category is set and |
Source code in CanvasGroupy/canvas.py
join_canvas_group(group, group_members)
¶
Add students to a Canvas group by their email prefixes.
Iterates over the provided member list and creates a group membership for each student. Students whose email prefixes are not found in the roster are collected and returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
Group
|
The Canvas group object to add members to. |
required |
group_members
|
[str]
|
List of student SIS Login IDs (email prefixes,
the part before the |
required |
Returns:
| Type | Description |
|---|---|
[str]
|
A list of email prefixes for students who could not be added. |
Source code in CanvasGroupy/canvas.py
link_assignment(assignment_id)
¶
Link a Canvas assignment for grading.
Fetches the assignment by ID and stores it for subsequent grading
operations such as post_grade.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assignment_id
|
int
|
The Canvas assignment ID, found in the assignment URL. |
required |
Returns:
| Type | Description |
|---|---|
Assignment
|
The linked Canvas assignment object. |
Raises:
| Type | Description |
|---|---|
ResourceDoesNotExist
|
If the assignment ID is invalid. |
Source code in CanvasGroupy/canvas.py
post_grade(student_id, grade, text_comment='', force=False)
¶
Post a grade and optional comment to the linked Canvas assignment.
Submits a grade for a specific student on the currently linked
assignment. If force is False and the existing score matches
the new grade, the submission is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student_id
|
int
|
The Canvas user ID of the student, obtainable
from |
required |
grade
|
float
|
The numeric grade to post. |
required |
text_comment
|
An optional text comment to attach to the submission. |
''
|
|
force
|
If False (default), skip posting when the existing
score already matches |
False
|
Returns:
| Type | Description |
|---|---|
Submission
|
The updated submission object, or None if the post was |
Submission
|
skipped. |
Source code in CanvasGroupy/canvas.py
set_course(course_id)
¶
Set the target course and fetch the student roster.
Retrieves the course by ID, fetches all enrolled students, and builds lookup dictionaries mapping emails to Canvas IDs and names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
course_id
|
int
|
The numeric Canvas course ID. |
required |
Raises:
| Type | Description |
|---|---|
ResourceDoesNotExist
|
If the course ID is invalid. |
Source code in CanvasGroupy/canvas.py
set_group_category(category_name)
¶
Set the active group category and fetch its groups.
Selects a group category (group set) by name and retrieves all groups and their member email lists within that category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
|
str
|
The exact name of the group category to activate. |
required |
Returns:
| Type | Description |
|---|---|
GroupCategory
|
The selected group category object. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |