[ARCHIVED] CanvasAPI Python - Beginner's Question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi All,
I am just starting working with CanvasAPI. Using Pyhtnon and editor PyCharm CE.
I am not a developer by any means by I am trying.
I have this code which works. ( I have remove token and url). I can make a connection and my token works.
# Import the Canvas class from canvasapi import Canvas # Canvas API URL API_URL = "https://example.com" # Canvas API key API_KEY = "p@$$w0rd" # Initialize a new Canvas object canvas = Canvas(API_URL, API_KEY)
However, I am trying to use the code below to retrieve my user details.
# Grab user 123 >>> user = canvas.get_user(123) # Access the user's name >>> user.name 'Test User'
My question is , how do I put both code snippets together to make it work?
Or is this is like a developer sort of question ?
Thank you in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would join the Canvas Developers group to start because you'll find a lot of good user scripts and coaching there.
I don't use PyCharm, so I'm sure there's a way to run a script from the IDE, but here's how you can do it with a file and a command line. The general file structure should be something like this:
from canvasapi import Canvas
canvas = Canvas(your_url, your_key)
# This a function that will run when you call
# the script from your terminal.
def main():
user = canvas.get_user(123)
print(user.name)
# prints 'Some User'
# Listen for the script from the terminal
if __name__ == "__main__":
main()
Save this in a script with the ".py" extension and then you can execute it from the command line with python user.py. When you run it, the user's name will print in your command line.
You can define helper functions to get more information outside of main. That's just the main worker (and the name "main" is just a python convention...you can call it whatever you want). Here's a slightly more complex script you can use to get missing work. Put an active course ID in the main function to test.
from canvasapi import Canvas
# Retrieve a <Course> object from Canvas.
def get_course(canvas, id):
course = canvas.get_course(id)
return course
def get_my_missing(submissions):
# Take all submissions and interpolate a list of missing work
missing = [item.assignment['name'] for item in submissions if
item.workflow_state == "unsubmitted" and
item.excused is not True and
item.submitted_at is None
]
return missing
def main():
# set up the Canvas object
canvas = Canvas(your_url, your_key)
# Use the get_course function above
course = get_course(canvas, 123)
# The endpoint looks for assignment IDs. If you don't supply any, it gets
# submissions for every assignment in the class.
#
# Include the "assignment" param to get the assignment name easily.
# Include the "submission_history"
submissions = course.get_multiple_submissions(include=["assignment"])
# Create a list of missing assignments
missing = get_my_missing(submissions)
# Display the assignment name
for assignment in missing:
print(assignment)
if __name__ == "__main__":
main()
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.