Giter VIP home page Giter VIP logo

cartodb-python's Introduction

What is cartodb-python?

The cartodb-python project is a Python client for:

Installation

You can install cartodb-python by cloning this repository or by using Pip, a Python package installer similar to easy_install:

pip install cartodb

Or if you want to use the development version (currently the only one that supports the Import API):

pip install -e git+git://github.com/CartoDB/cartodb-python.git#egg=cartodb

You might need to install cartodb's dependencies as well:

pip install -r requirements.txt

Usage example: SQL API

The following example requires your CartoDB API consumer key and consumer secret or the API key. Refer to the CartoDB documentation for details.

Using oAuth

from cartodb import CartoDBOAuth, CartoDBException

user =  '[email protected]'
password =  'secret'
CONSUMER_KEY='YOUR_CARTODB_CONSUMER_KEY'
CONSUMER_SECRET='YOUR_CARTODB_CONSUMER_SECRET'
cartodb_domain = 'YOUR_CARTODB_DOMAIN'
cl = CartoDBOAuth(CONSUMER_KEY, CONSUMER_SECRET, user, password, cartodb_domain)
try:
    print(cl.sql('select * from mytable'))
except CartoDBException as e:
    print("some error ocurred", e)

Using API KEY

You can get you API key in https://YOUR_USER.cartodb.com/your_apps

from cartodb import CartoDBAPIKey, CartoDBException

API_KEY ='YOUR_CARTODB_API_KEY'
cartodb_domain = 'YOUR_CARTODB_DOMAIN'
cl = CartoDBAPIKey(API_KEY, cartodb_domain)
try:
   print(cl.sql('select * from mytable'))
except CartoDBException as e:
   print("some error ocurred", e)

Usage example: Import API

The following example requires your CartoDB API key. Refer to the CartoDB documentation for details.

You can import a file into CartoDB like this:

from cartodb import CartoDBAPIKey, CartoDBException, FileImport

API_KEY ='YOUR_CARTODB_API_KEY'
cartodb_domain = 'YOUR_CARTODB_DOMAIN'
cl = CartoDBAPIKey(API_KEY, cartodb_domain)

# Import csv file, set privacy as 'link' and create a default viz
fi = FileImport("test.csv", cl, create_vis='true', privacy='link')
fi.run()

You can also import a dataset from a remote URL:

from cartodb import URLImport

fi = URLImport(MY_URL, cl)
fi.run()

If you specify a refresh interval (>=3600s) for a remote URL, your import job becomes a sync table, and CartoDB will refresh the datasets based on the contents of the URL at that interval:

from cartodb import URLImport

fi = URLImport(MY_URL, cl, interval=3600)
fi.run()

At this point, fi.success indicates whether the initial upload was successful or not.

You can get all the pending imports:

from cartodb import ImportManager

im = ImportManager(cl)
import_list = im.all(ids_only=False)

Or just one:

im = ImportManager(cl)
single_import = im.get("afaab071-dc95-4bda-a772-ea37f8729157")

You can update the attributes of an import job any time (like for checking if an import has finished):

single_import.update()

Object attributes correspond to those defined by the API. In particular, state is useful to know when the import is finished:

while im.state != "complete" and im.state != "failure":
    time.sleep(10)
    im.update()

API

Please refer to the source code for API reference documentation.

Note for people using version 0.4

With the new API key auth now you have two options to authenticate so the class CartoDB has been replaced with CartoDBOAuth and CartoDBAPIKey.

In order to migrate your code to this version you have to replace

from cartodb import CartoDB

by

from cartodb import CartoDBOAuth as CartoDB

Running tests

Clone the repo, create a secret.py from secret.py.example, fill the variables and execute:

python setup.py test

cartodb-python's People

Contributors

agonzalezro avatar atarantini avatar danicarrion avatar dersteppenwolf avatar eightysteele avatar idsoftware avatar javisantana avatar jgsogo avatar sanderpick avatar

Watchers

 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.