cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
at1032
Community Member

LTI grade passback to Canvas using Python

Jump to solution

Hello,

I am having trouble understanding how to pass a grade back to Canvas once a student finishes an assignment in an external tool. I am trying to do this in Python and Flask with the PyLTI library, but have not found a good example or explanation on how this should be done.  Any help, advice, or guidance would be much appreciated. 

Thanks,

Austin

Tags (3)
1 Solution

Accepted Solutions
bob_elliott
Community Member

I an see you want to use LTI instead of the CanvasAPI (or pyCanvas library)  because that means you can run using the credentials of the person who launched the app, rather than a hard coded key.

Are any of these code snippets handy?

https://programtalk.com/python-examples-amp/pylti.flask.lti.post_grade2/
https://programtalk.com/python-examples/pylti.flask.lti.post_grade/

Example 0

@app.route("/post_grade2/<float:grade>")
@lti_flask(error=error, request='session', app=app)
def post_grade2(grade, lti):
"""
Access route with 'session' request.

:param lti: `lti` object
:return: string "grade={}"
"""
ret = lti.post_grade2(grade)
return "grade={}".format(ret)

Example 1

@app.route("/post_grade2/<float:grade>")
@lti_flask(error=error, request='session', app=app)
def post_grade2(grade, lti):
"""
Access route with 'session' request.

:param lti: `lti` object
:return: string "grade={}"
"""
ret = lti.post_grade2(grade)
return "grade={}".format(ret)

https://canvas.instructure.com/doc/api/all_resources.html

{
"timestamp": "2017-04-16T18:54:36.736+00:00",
"scoreGiven": 83,
"scoreMaximum": 100,
"comment": "This is exceptional work.",
"activityProgress": "Completed",
"gradingProgress": "FullyGraded",
"userId": "5323497",
"https://canvas.instructure.com/lti/submission": {
"new_submission": true,
"submission_type": "online_url",
"submission_data": "https://instructure.com"
}
}

View solution in original post

5 Replies
m_brett
Community Member

If you solved this, I would love to hear how ...

Cheers,

Matthew

klundstrum
Community Coach
Community Coach

at1032 or  @m_brett   --  Were either of you able to find a solution? If yes, what was your process? If not, would you like this discussion to remain open in the Canvas Community?

m_brett
Community Member

No - sorry - I did not find a solution.   Yes, it would be good to leave this open in case someone has, and can explain how they did it ...

bob_elliott
Community Member

Without more detail, the question is difficult to answer directly. Would any of these suggestions be similar to what you want to do?

# Set `score` to a Int value
submission = assignment.get_submission(student_id)
submission.edit(submission={'posted_grade':score})
curl 'https://<canvas>/api/v1/courses/1/assignments/2/submissions/update_grades' \
     -X POST \
     -F 'grade_data[3][posted_grade]=88' \
     -F 'grade_data[4][posted_grade]=95' \
     -H "Authorization: Bearer <token>"
        'submission[posted_grade]': 8.0
    }
userGrade = requests.put(GRADE_URL, headers = self.headers, data = data)
bob_elliott
Community Member

I an see you want to use LTI instead of the CanvasAPI (or pyCanvas library)  because that means you can run using the credentials of the person who launched the app, rather than a hard coded key.

Are any of these code snippets handy?

https://programtalk.com/python-examples-amp/pylti.flask.lti.post_grade2/
https://programtalk.com/python-examples/pylti.flask.lti.post_grade/

Example 0

@app.route("/post_grade2/<float:grade>")
@lti_flask(error=error, request='session', app=app)
def post_grade2(grade, lti):
"""
Access route with 'session' request.

:param lti: `lti` object
:return: string "grade={}"
"""
ret = lti.post_grade2(grade)
return "grade={}".format(ret)

Example 1

@app.route("/post_grade2/<float:grade>")
@lti_flask(error=error, request='session', app=app)
def post_grade2(grade, lti):
"""
Access route with 'session' request.

:param lti: `lti` object
:return: string "grade={}"
"""
ret = lti.post_grade2(grade)
return "grade={}".format(ret)

https://canvas.instructure.com/doc/api/all_resources.html

{
"timestamp": "2017-04-16T18:54:36.736+00:00",
"scoreGiven": 83,
"scoreMaximum": 100,
"comment": "This is exceptional work.",
"activityProgress": "Completed",
"gradingProgress": "FullyGraded",
"userId": "5323497",
"https://canvas.instructure.com/lti/submission": {
"new_submission": true,
"submission_type": "online_url",
"submission_data": "https://instructure.com"
}
}

View solution in original post