Giter Site home page Giter Site logo

basil1987 / canvasapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ucfopen/canvasapi

0.0 1.0 0.0 3.67 MB

Python API wrapper for Instructure's Canvas LMS. Easily manage courses, users, gradebooks, and more.

Home Page: https://pypi.python.org/pypi/canvasapi

License: MIT License

Python 99.93% Ruby 0.02% Shell 0.05%

canvasapi's Introduction

CanvasAPI on PyPI License Python Versions Documentation Status Build Status Coverage Status Join UCF Open Slack Discussions

CanvasAPI

CanvasAPI is a Python library for accessing Instructure’s Canvas LMS API. The library enables developers to programmatically manage Canvas courses, users, gradebooks, and more.

Table of Contents

Installation

You can install CanvasAPI with pip:

pip install canvasapi

Documentation

Full documentation is available at Read the Docs.

Quickstart

Getting started with CanvasAPI is easy.

Like most API clients, CanvasAPI exposes a single class that provides access to the rest of the API: Canvas.

The first thing to do is instantiate a new Canvas object by providing your Canvas instance’s root API URL and a valid API key:

# 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)

You can now use canvas to begin making API calls.

Working with Canvas Objects

CanvasAPI converts the JSON responses from the Canvas API into Python objects. These objects provide further access to the Canvas API. You can find a full breakdown of the methods these classes provide in our class documentation. Below, you’ll find a few examples of common CanvasAPI use cases.

Course objects

Courses can be retrieved from the API:

# Grab course 123456
>>> course = canvas.get_course(123456)

# Access the course's name
>>> course.name
'Test Course'

# Update the course's name
>>> course.update(course={'name': 'New Course Name'})

See our documentation on keyword arguments for more information about how course.update() handles the name argument.

User objects

Individual users can be pulled from the API as well:

# Grab user 123
>>> user = canvas.get_user(123)

# Access the user's name
>>> user.name
'Test User'

# Retrieve a list of courses the user is enrolled in
>>> courses = user.get_courses()

# Grab a different user by their SIS ID
>>> login_id_user = canvas.get_user('some_user', 'sis_login_id')

Paginated Lists

Some calls, like the user.get_courses() call above, will request multiple objects from Canvas’s API. CanvasAPI collects these objects in a PaginatedList object. PaginatedList generally acts like a regular Python list. You can grab an element by index, iterate over it, and take a slice of it.

Warning: PaginatedList lazily loads its elements. Unfortunately, there’s no way to determine the exact number of records Canvas will return without traversing the list fully. This means that PaginatedList isn’t aware of its own length and negative indexing is not currently supported.

Let’s look at how we can use the PaginatedList returned by our get_courses() call:

# Retrieve a list of courses the user is enrolled in
>>> courses = user.get_courses()

>>> print(courses)
<PaginatedList of type Course>

# Access the first element in our list.
#
# You'll notice the first call takes a moment, but the next N-1
# elements (where N = the per_page argument supplied; the default is 10)
# will be instantly accessible.
>>> print(courses[0])
TST101 Test Course (1234567)

# Iterate over our course list
>>> for course in courses:
         print(course)

TST101 Test Course 1 (1234567)
TST102 Test Course 2 (1234568)
TST103 Test Course 3 (1234569)

# Take a slice of our course list
>>> courses[:2]
[TST101 Test Course 1 (1234567), TST102 Test Course 2 (1234568)]

Keyword arguments

Most of Canvas’s API endpoints accept a variety of arguments. CanvasAPI allows developers to insert keyword arguments when making calls to endpoints that accept arguments.

# Get all of the active courses a user is currently enrolled in
>>> courses = user.get_courses(enrollment_status='active')


# Fetch 50 objects per page when making calls that return a PaginatedList
>>> courses = user.get_courses(per_page=50)

For a more detailed description of how CanvasAPI handles more complex keyword arguments, check out the Keyword Argument Documentation.

Contact Us

Need help? Have an idea? Just want to say hi? Come join us on the UCF Open Slack Channel and join the #canvasapi channel!

canvasapi's People

Contributors

thetwam avatar jessemcbride avatar a-goetz avatar jrsilveti avatar devints47 avatar qwertynerd97 avatar nathaned avatar liblit avatar anthonyrodriguez726 avatar tuanvpham avatar atarisafari avatar danbrink91 avatar katherinecrose avatar weining-li avatar nottheswimmer avatar altgilbers avatar iturgeon avatar rowdyrotifer avatar matthewf-ucsd avatar allygator avatar andrew-gardener avatar rebelaide avatar phaustin avatar sigurdurb avatar tobiaqs avatar kensler avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.