canvasapi
canvasapi copied to clipboard
Documentation Examples
As a stopgap measure until we complete #9, we should provide code examples for common use cases.
Some important examples:
- Create a new user
- Create a new course
- Enroll user(s) in a course
- Create/edit an assignment
- Get all users in a course
There's plenty of other things to add, of course. Add more as needed.
We'll also need to figure out the best way to organize all these examples. For now, a single page in the docs should suffice.
@jessemcbride (and anybody else who is interested), could you take a look at my branch and see if this seems like a good pattern to continue with?
@jessemcbride things to work on:
Accounts:
- [x] Creating a new user
- [x] List all courses
Users
- [x] Getting a user by SIS ID
- [x] Getting a user by their Canvas ID
- [x] Edit a user
- [x] Get page views
Logins
- [x] List user logins
- [ ] Adding a user login
- [ ] Updating a user login
Work continues on this. Will likely be an ever-evolving issue.
Any examples of how to set grades for assignments? If someone can give me a hint I will make an example I see how the assignment class .set_attributes( {x.grade: X} ) works but not how to POST grades I imagine I need to post / upload the students assignment object with changed attributes
@mboldin You should be able to grade an assignment via Submission.edit()
using the submission[posted_grade]
parameter as described in the Canvas docs.
Thanks. The code below works. The trick is you do not use the grade parameter i.e., NOT edit(submission={'grade': new_grade}) And it is easy to iterate over student ids or use .get_submissions() to loop over submissions thanks to other Python code in CANVASAPI
## Pick course, assignment, student, and set new grade
course = canvas.get_course( -- course number --- )
a = course.get_assignment( -- assignment id --)
x = -- student ID --
new_grade = 10
## Modify assignment grade for one student
print (a.name, '--', a.created_at[:10], a.due_at[:10], a.updated_at)
s1 = b.get_submission(x)
print(x, s1.id, 'Current grade:', s1.grade)
s1.edit(submission={'posted_grade': new_grade})
##Check result
s2 = b.get_submission(x)
print(x, s2.id, 'Modified:', s2.grade)
Yeah, per the Canvas Docs, submission[posted_grade]
will update both the score
and grade
attributes.
Also, the object should update in-place, so in your example s1
should already have the modified grade. If not, let us know!
I'd love to help! Is anyone tackling creating and managing assignments? I also found some issues with uploading files.
Hey @elec3647!
We already have examples for creating and editing assignments. If you have suggestions to improve those examples, or have any other examples to add, please feel free to submit a pull request!
Thanks 😄
Add examples in the docs for CalendarEvent.edit() (see #239).