首页
/ Carto-python 使用与技术文档

Carto-python 使用与技术文档

2024-12-26 09:18:02作者:侯霆垣

1. 安装指南

carto-python 可以通过克隆此仓库或使用 Pip 进行安装:

pip install carto

如果您想使用开发版本,可以直接从 github 安装:

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

使用开发版本时,您可能还需要安装 Carto 的依赖项:

pip install -r requirements.txt

2. 项目的使用说明

在开始使用 carto-python 之前,需要定义 API 调用的认证方式。目前支持两种认证方式:未认证和基于 API 密钥的认证。因此,首先需要创建一个认证客户端,用于实例化处理 API 请求的 Python 类。

  • 未认证请求

创建一个 NoAuthClient 对象:

from carto.auth import NoAuthClient

USERNAME = "type here your username"
USR_BASE_URL = "https://{user}.carto.com/".format(user=USERNAME)
auth_client = NoAuthClient(base_url=USR_BASE_URL)
  • 基于 API 密钥的认证

创建一个 APIKeyAuthClient 实例:

from carto.auth import APIKeyAuthClient

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

API 密钥 对于所有 API 请求都是必需的,除了向公共数据集发送 SQL 查询。

base_url 参数必须包含 user 和/或 organization

3. 项目API使用文档

以下是 carto-python 中几个关键 API 的使用示例:

SQL API

使用 SQL API 进行请求非常直接:

from carto.sql import SQLClient

sql = SQLClient(auth_client)

try:
    data = sql.send('select * from mytable')
except CartoException as e:
    print("some error occurred", e)

print(data['rows'])

请参考源代码文档以了解构造函数和 send 方法接受的其余参数。特别是,send 方法允许您控制结果的格式。

Import API

您可以将本地或远程数据集导入 CARTO:

from carto.datasets import DatasetManager

# 在此处写入本地文件或远程 URL 的路径
LOCAL_FILE_OR_URL = ""

dataset_manager = DatasetManager(auth_client)
dataset = dataset_manager.create(LOCAL_FILE_OR_URL)

Import API 是异步的,但 DatasetManager 会等待最多 150 秒以便数据集上传完成,一旦完成,数据集就会在 CARTO 中创建。

Maps API

Maps API 允许创建和实例化命名和匿名地图:

from carto.maps import NamedMapManager, NamedMap
import json

# 在此处写入包含 JSON 命名地图模板的本地文件的路径
JSON_TEMPLATE = ""

named_map_manager = NamedMapManager(auth_client)
named_map = NamedMap(named_map_manager.client)

4. 项目安装方式

除了使用 Pip 安装外,还可以通过以下方式安装 carto-python:

# 克隆仓库
git clone https://github.com/CartoDB/carto-python.git

# 进入目录
cd carto-python

# 安装依赖
pip install -r requirements.txt

# 安装 carto-python
pip install .

请确保在安装前已经安装了 Pip 和 Git。

项目优选

收起
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
417
317
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
90
158
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
46
115
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
50
13
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
268
402
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TSX
310
28
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
87
239
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
342
213
MateChatMateChat
前端智能化场景解决方案UI库,轻松构建你的AI应用,我们将持续完善更新,欢迎你的使用与建议。 官网地址:https://matechat.gitcode.com
625
73
RuoYi-Cloud-Vue3RuoYi-Cloud-Vue3
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
85
61