子图 > 查询
2 分钟
使用 Python 和 Subground查询The Graph
Subgrounds是一个用于查询子图的直观的 Python 库,由 Playgrounds构建。 它允许您直接将子图数据连接到 Python 数据环境。 让您使用像 pandas这样的库来进行数据分析!
Subgrounds提供了一个简单的 Pythonic API,用于构建GraphQL 查询,实现分页等冗余工作流自动化,并通过受控模式转换增强高级用户的能力。
开始
Subgrounds需要 Python 3.10或更高版本,可在 pypi上获取。
1pip install --upgrade subgrounds2# or3python -m pip install --upgrade subgrounds
安装完毕后,您可以通过以下查询测试Subgrounds。 下面的示例为 Aave v2 协议拍摄了子图并查询了TVL 订购的前 5 个市场(总值锁定), 选择他们的名字和他们的 TVL (美元) 并返回数据为pandas DataFrame。
1from subgrounds import Subgrounds23sg = Subgrounds()45# Load the Subgraph6aave_v2 = sg.load_subgraph(7 "https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum")89# Construct the query10latest_markets = aave_v2.Query.markets(11 orderBy=aave_v2.Market.totalValueLockedUSD,12 orderDirection='desc',13 first=5,14)15# Return query to a dataframe16sg.query_df([17 latest_markets.name,18 latest_markets.totalValueLockedUSD,19])
文档
Subgrounds是由 Playgrounds团队构建和维护的,可以在 Playplace文档上访问。
由于Subgrounds有大的功能可以探索,在这里有一些有用的起始地点: