Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
I'm trying my hand at a python script that will reset a batch of courses using the canvasapi, and I want to attempt it with the following:
import csv
import sys
from canvasapi import Canvas
API_URL = "https://my.test.instructure.com"
API_KEY = <your api key here>
canvas = Canvas(API_URL, API_KEY)
with open('H:\TESTING RESULTS\cnvs_200227_050011_courses.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
courseId = row[0]
partedCourse = courseId.rpartition("-")
term = partedCourse[2]
print(courseId, term)
if term == '21/SP' or term == '21/IN' or term == '20/FA':
course = canvas.get_course(courseId)
course.reset()
I've confirmed that I can successfully read the records from the csv file, and accurately parse out the course ID and term. But can someone confirm that the last 2 lines will perform the desired task?
I'm a total noob at both python and working with the CanvasAPI, so any other suggestions are also welcome.
TIA!
Greetings @swaldie ,
Yes, that command will perform the same course reset that is in the Settings of a course. You may want to change that last line to something like this so you can grab the new course ID:
new_course = course.reset()
print(new_course.id)
Thanks, Matt!
I appreciate your feedback and the quick response.
Quick clarification...is the highlighted variable on line 1 actually spose to be the same as that on line 2:
@swaldie ,
So when you perform a course reset using that first line, you will get a response object back. The new_course variable will store that response object and then you can see the new course ID using that second line. A course reset actually creates a new empty course with a different course ID and deletes the old course so you'll need that new course ID if you want to do anything like enroll users through the API (instead of using SIS imports). Hopefully I'm explaining that correctly as well as interpreting your question correctly.
Reading your question over again, I would also like to say that you could do this:
print(new_course)
And that would print the entire object that was returned by the reset. Usually you only need the new course ID so the new_course.id would only show the ID value instead of the whole object.
Ah yes, I get it now. Specifying the ".Id" singles out the course ID part of new_course.
Thanks again, Matt! This has been very helpful.
Hey @swaldie ,
This doesn't answer your question, but I highly recommend trying out your developmental scripts on the test instance of Canvas - which is designed exactly for this type of thing.
Thanks, Danny. I'll definitely be testing this out in the test env first.
To participate in the Instructure Community, you need to sign up or log in:
Sign In
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.