Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Howdy folks,
I'm looking for some more Python code samples for using the API to automate some of the mundane tasks I have been assigned. I have been using some of the scripts located here: kajigga/canvas-contrib · GitHub but not all of them seem to be working.
Right now I'm specifically looking for a python script that will allow me to import quizzes. I have a list of 101 course ID numbers and I have a different quiz (QTI zip formatted) that needs to be uploaded into different course. So it would be really awesome if the script read the course ID, and file name from a CSV file so I could reuse the script the next time I have to do this. Any help is greatly appreciated!
P.S. I know as much Python as I do Spanish, which is not much. I can ask "where is the bathroom" and that's about it.
Python is a great language! I'm also sort'of limited in my knowledge of Python and I'm sure if I get some time I can look into automating tasks in python to help you out. Or ruby if you're more interested in that? Mostly, I have been using bash scripts to do mundane tasks because - for whatever reason - that's what I am most comfortable with.
I made a blog post about working with canvas from the command line https://community.canvaslms.com/groups/canvas-developers/blog/2015/07/02/canbash-canvas-bash-scripti... ... I find it pretty simple to do, but my method pretty much relies on you running BASH as your shell of choice and works from the command line.
Then you can use https://canvas.instructure.com/doc/api/quizzes.html as your documentation resource for helping to develop a script for your purpose that can be re-usable, customized, ect. And if you choose to do it the BASH scripting way I can defiantly be a resource to help you with that. Or maybe you can sudo-reverse engineer my bash script workflow into a python script of your own design.
Also, working with CSV files from the command line isn't that bad from my experience if I know what I am talking about -- who knows.
If I could just get the script here to work, I think it does exactly what I need. I just keep getting errors. Here is a couple of errors that I fixed:
But now I get this error:
copies [{'source_id': u'AdvancedBandI(Grades9-12).zip', 'destination_id': u'53.X
381'}]
local config file not found. That's okay, it just means you will have modified the variables at the top of this file.
local config file not found. That's okay, it just means you will have modified the variables at the top of this file.
local config file not found. That's okay, it just means you will have modified the variables at the top of this file.
local config file not found. That's okay, it just means you will have modified the variables at the top of this file.
doing copy: {'source_id': u'AdvancedBandI(Grades9-12).zip', 'destination_id': u'53.X381'}
Traceback (most recent call last):
File "F:\Dropbox\Work\Canvas\canvas-contrib-master\API_Examples\bulk_upload_migration\python\upload_course_migrations.py", line 351, in <module>
runMigrations(course_copy_queue)
File "F:\Dropbox\Work\Canvas\canvas-contrib-master\API_Examples\bulk_upload_migration\python\upload_course_migrations.py", line 281, in runMigrations
stats.append(res.get(0xFFFF))
File "C:\Python27\lib\multiprocessing\pool.py", line 567, in get raise self._value
NameError: global name 'rootLogger' is not defined
I'm using Windows 8.1 as my OS and I do not have access to any machines that run OSX.
If you wanted to do the BASH way you could always install a VM and run a free linux distro like Ubuntu or something and should (pretty much) be able to BASH the way I do it... However, I totally get you not wanting to mess around with it. So, lemme take a look at the python.
But, looking at the traceback I see the "NameError: global name 'rootLogger' is not defined", so I would assume "rootLogger = logging.getLogger('upload_course_migrations')" is misplaced on line 326 ... Because (I could be totally wrong) it is being defined after it is being used. So, common python practice would be to define it before you use it. So, I would guess that would be causing you some problems starting on line 322 where it says "There is probably a better place to put this stuff" ...
I could be totally wrong here. But, that seems to make sense to me.
Hi,
We have been developing a Canvas sdk in python, it's up on github here: penzance/canvas_python_sdk · GitHub
Please feel free to check it out.
We also have other python samples in the same repo on github that you might find useful.
The sdk modules map directly to the the Canvas REST api calls.
Here's are some sample calls:
# get all enrollments for a course
canvas_enrollments = get_all_list_data(SDK_CONTEXT, enrollments.list_enrollments_courses, canvas_course_id)
# list all sections
results = sections.list_course_sections(request_context, course_id)
# get section info
response = sections.get_section_information_sections(SDK_CONTEXT, section_id)
# delete a section
result = sections.delete_section(SDK_CONTEXT, section_id)
There also a small example python script here:
Full Example · penzance/canvas_python_sdk Wiki · GitHub
I hope this is helpful!
Eric
Saving
Hi @MattHanes ,
My group and I have done some extensive automation with Canvas in Python specifically. Since this post is about 10 months old, are you still looking for examples for the scenario you described above, or have you moved on?
I have moved on but I would still love to see your examples!
a simple script we wrote to mass publish courses looks like this:
# Canvas Course Publishing Script, written by Luke Levesque - Liberty University
# Written for use with python 2.7 and 'requests' package - http://docs.python-requests.org/en/
import requests
token = '<token goes here>'
url = 'https://canvas.instructure.com'
account_id = '<canvas account number>'
header = {'Authorization': 'Bearer ' + '%s' % token}
def publishCourses(courselist):
for id in courselist:
payload = {'course_ids[]': id, 'event': 'offer' }
r = requests.put(url + '/api/v1/accounts/' + account_id + '/courses/' + str(id), params=payload, headers = header )
data = r.json() #json response of the PUT request
return data
if __name__ == "__main__":
user_input = raw_input("Please enter the Course IDs to publish: ") #accepts a space separated list of canvas course ids
courselist = map(int, s.split()) #splits the values at each space and appends it to this list
publishCourses(courselist)
Let me know if you have questions or a specific function in mind, we've done quite a lot in python.
Yes, I have a very similar python script that reads the course IDs from a CSV file for publishing.
I would be very interested in any examples you have for using the content-migrations API to do QTI quiz imports. Thanks for sharing!
Hello @lalevesque ,
I realize this is very old, but my team and I are looking to automate several processes with Python as well. However, we are using Python 3 and it does not support the Requests module. Right now we are running into issues with pagination and I wanted to see if you had any insights into this? Any help you can provide would be great. Thanks again.
Jason
Thank you @lalevesque - this was a huge help as I wrote my first python API script for Canvas. I'll post a link to some code samples when I finalize.
I'm trying to use Python to upload an outcomes file (csv) to Canvas but the script gives me errors. Do you have a script you would share?
@gbibb ,
I have not tried this before but would iterating through each row of the csv file work for an upload? For instance:
a = open(some_file.csv)
csv_a = csv.reader(a)
for row in csv_a:
## upload outcomes or something else ##
Jason Schaffer
This is something to add to the outcome_importer.py script?
@gbibb ,
I am not familiar with that script and I do not see it posted here. Is this something you wrote on your own, or did you find it posted somewhere?
Regards,
Jason Schaffer
He's probably talking about this one: unsupported-canvas/api/import_outcomes/python at master · kajigga/unsupported-canvas · GitHub
Hey jschaffer,
I looked in to it and the Python Package Index (PYPI) lists the requests module as compatible with Python 3 here: requests 2.10.0 : Python Package Index
I'd be glad to help out with pagination issues, my team created some clever solutions to get around pagination. Do you have any specific examples of issues you're experiencing?
Thanks @lalevesque ,
The issue I am running to is that I am using the http.client import for Python 3. Our goal is to be able to pull all courses in a term (which could add up to several hundred courses) with a single API. Right now I have a way to pull the first page, or the last page, but when I try to loop through the responses I am getting errors. I cannot find a way to review the links in the responses to see if I have reached the last page or not. I am just getting my feet wet with both Python and coding so I am sure there is something simple I am missing, but so far I have had no luck.
Jason
Here is the function that I use to return a list of our courses, maybe it will be helpful:
Let me know if this doesn't make sense.
@lalevesque ,
Sorry for the late reply, but this is fantastic. I could not have asked for better help. This should get me started on my project. Thanks again.
Jason
To participate in the Instructure Community, you need to sign up or log in:
Sign In