To get all of the pages you can use my python script:
cgetall.py canvas_course_page_url|course_id [destination_directory]
available from https://github.com/gqmaguirejr/Canvas-tools
I have another program: cput.py canvas_course_page_url
to put pages back, it is basically just calling a function:
def put_course_page(canvas_course_page_url):
# Use the Canvas API to GET the page
#GET /api/v1/courses/:course_id/pages/:url
#extract course_id from URL
course_id=canvas_course_page_url[canvas_course_page_url.find("courses/")+8:canvas_course_page_url.find("pages/")-1]
if Verbose_Flag:
print("course_id: {}".format(course_id))
#extract the file name portion of the URL
page_url=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]
if Verbose_Flag:
print("page_url: {}".format(page_url))
new_file_name=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]+'.html'
if Verbose_Flag:
print("new_file_name: {}".format(new_file_name))
# read .html page
with open(new_file_name, 'rb') as f:
file_input=f.read()
url = baseUrl + '%s/pages/%s' % (course_id, page_url)
if Verbose_Flag:
print(url)
payload={"wiki_page[body]": file_input}
r = requests.put(url, headers = header, data=payload)
if Verbose_Flag:
print("r.status_code: {}".format(r.status_code))
if r.status_code == requests.codes.ok:
page_response = r.json()
pprint(page_response)
return True
else:
print("No such page: {}".format(canvas_course_page_url))
return False
return False
To create pages - one can use a function such as:
# canvas_course_page_url will be of the form: https://kth.instructure.com/courses/11/pages/notes-20160716
def create_course_page(canvas_course_page_url, page_title):
# Use the Canvas API to GET the page
#GET /api/v1/courses/:course_id/pages/:url
#extract course_id from URL
course_id=canvas_course_page_url[canvas_course_page_url.find("courses/")+8:canvas_course_page_url.find("pages/")-1]
if Verbose_Flag:
print("course_id: {}".format(course_id))
#extract the file name portion of the URL
page_url=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]
if Verbose_Flag:
print("page_url: {}".format(page_url))
new_file_name=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]+'.html'
if Verbose_Flag:
print("new_file_name: {}".format(new_file_name))
# read .html page
with open(new_file_name, 'rb') as f:
file_input=f.read()
# note that you cannot provide the page_url for the page - as this page_url does not yet exist
url = baseUrl + '%s/pages' % (course_id)
if Verbose_Flag:
print(url)
payload={'wiki_page[title]': page_title, 'wiki_page[published]': False, "wiki_page[body]": file_input}
r = requests.post(url, headers = header, data=payload)
if Verbose_Flag:
write_to_log(r.text)
if Verbose_Flag:
print("r.status_code: {}".format(r.status_code))
if r.status_code == requests.codes.ok:
page_response = r.json()
if Verbose_Flag:
print("Created page")
pprint(page_response)
return True
elif r.status_code == requests.codes['unprocessable_entity']:
# a list of all the status codes is at https://github.com/kennethreitz/requests/blob/master/requests/status_codes.py
print("unprocessable_entity - probably because you specified a page_url, but this page does not yet exist")
print("Error when creating page: {}".format(canvas_course_page_url))
return False
else:
print("Error when creating page: {}".format(canvas_course_page_url))
return False
return False
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.