Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Hello,
I am trying to update the end dates for my Canvas courses. I am using the Canvas API module imported to Python.
Looking at this, I assumed that I would be able to just use the update method. Based on this documentation, I thought that I could do so by passing course{end_at] = [date] as a parameter. So, my code looks like this:
course = canvas.get_course(course_id)
course.update(course[end_at] = [date])
This gives me the error: SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
So, then I thought to try the following:
course.update(end_at = date)
This works without any errors, but doesn't actually change the course end date. I tried passing the date as both a string and DateTime object, but had no success.
I could really appreciate some help here. I found posts like this. However, I don't understand how to translate the JSON into the appropriate formatting for this update method in the Python module that I am using. I have no previous experience with coding; I am a History teacher and just started learning to be able to work more quickly in Canvas. So, if you are able to help, please just assume that I don't know anything.
Solved! Go to Solution.
Success!
Here's my code which updates a course titled 'Canvas Example' to have an end date field set to Dec 1, 2024 in my time zone (your time zone date string may vary))
course_id = <your_course_id>
test_course = canvas.get_course(course_id)
result = test_course.update(course={
'conclude_at': '2024-12-1T07:00:00.000Z'
}
)
print(result)
# Prints out Course Title and not a bool as the UCF CanvasAPI documentation has listed
Note that the documentation for .update(**kwargs) lists that the return type is bool, but my result variable came out as the course title field which is Truthy so you could use that in some sort of logic statement to determine success of the code.
Seems like this should be pretty straightforward. Looking at the API documentation page first because the CanvasAPI Python library is a wrapper for the official API.
https://canvas.instructure.com/doc/api/courses.html#method.courses.update_settings
Unless my eyes are deceiving me, I'm not able to find an endpoint that allows for the end_date to be manipulated via the API. My understanding is that the Canvas UI is built upon the official API. Maybe someone else who knows how the Canvas UI transmits that information from the browser to the back end can assist. I'm going to tag @James since I have received several good responses in this community group from him before.
I don't use the Python library, but the code doesn't look right. You would likely need to create an object (maybe dictionary). I don't speak Python very well) so I won't be able to help convert the JSON into Python.
It looks like you may have been close with the course.update(end_at=date) attempt, but it looks like there is one piece missing.
The Update a Course endpoint is the one to use to set the date for a course. The end_at parameter should be an ISO8601 date, but there is a note in the documentation that says "This value is ignored unless ‘restrict_enrollments_to_course_dates’ is set to true." That's the piece that seems to be missing.
Perhaps. A possible reading of that is "We'll change the date, but it won't have any impact unless the enrollments are set to use the course dates".
It's also possible that your date is not in the proper format. I don't know how the Canvas Python library deals with datetime objects.
I haven't tested this, but I think this should work. Again, I cannot help with the Python.
The PUT statement should look something like this (change the course ID and date to what you need them to be):
PUT <instance>/api/v1/courses/1234?course[end_at]=2024-06-20T05:00:00Z&course[restrict_enrollments_to_course_dates]=1
As a JSON object, it would look like this:
{
"course" : {
"end_at" : "2024-06-20T05:00:00Z",
"restrict_enrollments_to_course_dates": true
}
}
The Canvas Mobile apps are built on the API. There are some internal commands from the web UI that do not use it, but if the apps support it, then it's likely in the API somewhere.
Thanks @James
I need to retract my earlier statement. I was able to find where the update endpoint is supposed to be in the API docs and successfully recreate a PUT request to Canvas to update a course to end on Dec 25:
Strangely enough, the info that sent over my dev tools panel in Chrome there appears to have been in a conclude_at field sent over in a POST request rather than an end_at field sent in a PUT request:
Here is my Headers panel in Chrome Dev that shows the request as a POST request. This comes when clicking the 'Update Course Details' button on the settings nav panel in my test course called 'Canvas Example'
Here is the Payload tab in Chrome that shows the course details object fields and the conclude_at field in the payload:
I did get it to work in Postman as a PUT request so that made the request look like this:
https://school.instructure.com/api/v1/courses/id?course[conclude_at]=2024-12-25T07:00:00.000Z
The DateTime string is for Dec 25 in my current time zone. How that translates into an update request via the UCF Python CanvasAPI library, I will continue to play with and respond when I have success. Someone else is welcome to beat me to the finish line. 😉
Success!
Here's my code which updates a course titled 'Canvas Example' to have an end date field set to Dec 1, 2024 in my time zone (your time zone date string may vary))
course_id = <your_course_id>
test_course = canvas.get_course(course_id)
result = test_course.update(course={
'conclude_at': '2024-12-1T07:00:00.000Z'
}
)
print(result)
# Prints out Course Title and not a bool as the UCF CanvasAPI documentation has listed
Note that the documentation for .update(**kwargs) lists that the return type is bool, but my result variable came out as the course title field which is Truthy so you could use that in some sort of logic statement to determine success of the code.
Wow! Thank you so much. That works perfectly. Thank you as well for including screenshots in your earlier replies. It makes it very easy to see where you found this, so hopefully I can do the same in the future.
To participate in the Instructure Community, you need to sign up or log in:
Sign In