CartoDB / carto-python

CARTO Python client
https://carto.com
BSD 3-Clause "New" or "Revised" License
154 stars 62 forks source link

Import API overwrite and table_name #64

Closed Eh2406 closed 7 years ago

Eh2406 commented 7 years ago

Hi,

I have a csv that I want to upload to my account. I am using the overwrite arg in cartoframes.

cc.write(pd.read_csv('table_ouput.csv'), 'table', overwrite=True, temp_dir="")

How can I do the equivalent using the carto-python library directly?

alrocar commented 7 years ago

Hi,

I guess you have to get the dataset, delete it and reupload it. It should be something like this:

from carto.datasets import DatasetManager
from carto.auth import APIKeyAuthClient

USERNAME="type here your username"
ORGANIZATION="type here your organization"
BASE_URL = "https://{organization}.carto.com/user/{user}/". \
    format(organization=ORGANIZATION,
           user=USERNAME)
auth_client = APIKeyAuthClient(api_key="myapikey", base_url=BASE_URL, organization=ORGANIZATION)

# write here the name of the dataset to overwrite
DATASET_ID = ""

dataset_manager = DatasetManager(auth_client)
dataset = dataset_manager.get(DATASET_ID)
dataset.delete()

# write here the path to a local file or remote URL
LOCAL_FILE_OR_URL = ""
dataset = dataset_manager.create(LOCAL_FILE_OR_URL)
Eh2406 commented 7 years ago

Thank you for that workaround. That really gets us moving on this project. cartoframes lets me upload it with a different name than the file, but I can just rename the file before uploading.

I spent some time trying to find how carto frames accomplishes it, but I don't see the call to delete.

alrocar commented 7 years ago

AFAIK CARTOFrames uploads the file via the import API, and then drops the old table and rename the new one with the name of the old one.

See this

:)