Register for InstructureCon25 • Passes include access to all sessions, the expo hall, entertainment and networking events, meals, and extraterrestrial encounters.
Found this content helpful? Log in or sign up to leave a like!
Hi,
I'm using the canvasapi Python module, and I'm trying to create quizzes from a file. I am able to create the entire quiz this way except for adding answers. I've looked everywhere I can think of for information on what I'm doing wrong with the answer objects, but I'm unable to find even the basic format of an answer object. I've attached some sample code below from one of my hard-coded quizzes that should illustrate the issue I'm having:
quiz.create_question(question={
'question_name': 'Gender',
'question_type': 'multiple_choice_question',
'question_text': 'Which gender do you identify as?',
'points_possible': '0.0',
'correct_comments': '',
'incorrect_comments': '',
'neutral_comments': '',
'correct_comments_html': '',
'incorrect_comments_html': '',
'neutral_comments_html': '',
'answers': [{'answer_text':'Male', 'weight':100.0}
,{'answer_text':'Female', 'weight':0.0}
,{'answer_text':'Other', 'weight':0.0}
,{'answer_text':'Prefer not to say', 'weight':0.0}
]
})
I had been under the impression that to add an answer, I should only need to specify the answer_text and the weight (or answer_weight, I've tried both and neither's worked). However, when I run that (in terminal), I get this:
Traceback (most recent call last):
File "<stdin>", line 15, in <module>
File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/quiz.py", line 136, in create_question
_kwargs=combine_kwargs(**kwargs)
File "/home/jeff/anaconda3/lib/python3.6/site-packages/canvasapi/requester.py", line 111, in request
raise CanvasException("API encountered an error processing your request")
canvasapi.exceptions.CanvasException: API encountered an error processing your request
Obviously that error message isn't particularly helpful.
Out of curiosity, I went onto the website and created an answer via the web interface, just to see what the structure of an answer is. This is what I got (just the answers section, because this question is already getting pretty long):
QuizQuestion(_requester=<canvasapi.requester.Requester object at 0x7f5ac4bd47b8>, id=1337341, quiz_id=122827, quiz_group_id=None, assessment_question_id=1851679, position=None, question_name=Question1, question_type=multiple_choice_question, question_text=<p>It worked!</p>, points_possible=1.0, correct_comments=, incorrect_comments=, neutral_comments=, correct_comments_html=, incorrect_comments_html=, neutral_comments_html=, answers=[{'id': 505, 'text': '1', 'html': '', 'comments': '', 'comments_html': '', 'weight': 100.0}], variables=None, formulas=None, answer_tolerance=None, formula_decimal_places=None, matches=None, matching_answer_incorrect_matches=None, course_id=31084)
I've tried specifying each of those fields within the python code to create my own answers, but again this didn't work. Even if it had, the 'id' field itself already presents a problem, since it's going to be very difficult for me to manually ensure I'm always getting the next unique id.
That said, the structure of the Answer object kind of indicates to me that there's probably a way to actually make this, I just haven't been able to find it in the documentation at all. Has anyone encountered this problem before, and how did you deal with it?
Solved! Go to Solution.
Hey @bulmer_jeffk !
I'm one of the primary maintainers of the CanvasAPI library. Let's see if we can get you up and running!
The code you provided appears to be correctly formatted. In fact, I copy-pasted it without alterations and was able to successfully create a new question with all the parameters you provided.
The error message you're seeing is as a result of Canvas returning an HTTP 500 error to the API request. The latest version of CanvasAPI actually makes this error slightly more useful by also providing the status code. This error means Canvas is breaking when trying to process your request, often due to bad input that is then mishandled by the server.
Since I don't see anything immediately wrong with the code you posted, I have a few questions:
pip show canvasapi
)python --version
)My apologies for seeing this so late. I don't frequent the Canvas Community as often as I probably should, so the best place to get CanvasAPI-specific help is the UCF Open Source Slack in the #canvasapi channel. Creating an issue on our GitHub page is also a good way.
Thanks,
- Matt Emond
Hello @bulmer_jeffk Welcome to the Canvas Community. Thank you for posting your question. Your question is beyond my expertise, but I wanted to let you know that I have gone ahead and shared your question with the Canvas Developers group here in the Community in hopes that your question will get some additional exposure. If you are not following this group yet, please use the link that I have provided above, and then you will see two buttons at the top right corner of that page: "Follow" and "Actions". Click on both of these. (When you click on "Actions", you'll want to further select "Join group".) I hope these steps will be of help to you. Good luck in finding an answer, Jeff!
Hey @bulmer_jeffk !
I'm one of the primary maintainers of the CanvasAPI library. Let's see if we can get you up and running!
The code you provided appears to be correctly formatted. In fact, I copy-pasted it without alterations and was able to successfully create a new question with all the parameters you provided.
The error message you're seeing is as a result of Canvas returning an HTTP 500 error to the API request. The latest version of CanvasAPI actually makes this error slightly more useful by also providing the status code. This error means Canvas is breaking when trying to process your request, often due to bad input that is then mishandled by the server.
Since I don't see anything immediately wrong with the code you posted, I have a few questions:
pip show canvasapi
)python --version
)My apologies for seeing this so late. I don't frequent the Canvas Community as often as I probably should, so the best place to get CanvasAPI-specific help is the UCF Open Source Slack in the #canvasapi channel. Creating an issue on our GitHub page is also a good way.
Thanks,
- Matt Emond
Hi @matthew_emond
Thanks for your reply. I was actually able to fix this issue just by updating my canvasapi. I still want to address your response as much as possible though, in the interest of maybe saving someone else the headache this caused in the future.
1. I am currently using canvasapi version 0.13.0 (I believe the previous version was 0.12.0). In this version, that code is fully correct, and ran without any changes at all.
2. I am running Python 3.6.5
3. I am currently making several questions at once. I actually never had a problem generating any number of questions. I tried generating only one question, or no questions, but I have pretty consistently been able to create as many or as few questions as i want without issue.
4. The main problem came from answers. As soon as I put any answers into a question, I would get the error. I tried one answer, as well as several, but neither of those options worked. When I did not provide answers, I was able to create questions, but these didn't have answers, and so weren't particularly useful.
As I said, in the most recent version of the API, I am able to add questions with answers using the code provided, so I was able to fix it just by updating. I've taken note of the slack and the GitHub as well, so I'll direct further questions there as well as here.
Thank you for taking the time to look into this, and hopefully this discussion helps people to avoid having this issue in the future!
To interact with Panda Bot in the Instructure Community, you need to sign up or log in:
Sign In