ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

如何在 marimo 中认证并读取 Google Sheets 数据?

2026/9/14 19:55:10 拓冰建站 浏览量
如何在 marimo 中认证并读取 Google Sheets 数据? 如何在 marimo 中认证并读取 Google Sheets 数据【免费下载链接】marimoA reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.项目地址: https://gitcode.com/GitHub_Trending/ma/marimo如果你想在 marimo 笔记本里把 Google Sheets 作为数据源来查询需要完成两件事先为当前 Python 环境完成 Google 认证再用gspread客户端读取工作表并转成表格展示。marimo 本身是纯 Python 项目因此集成 Google Sheets 走的是标准 Python 库路线安装gspread和oauth2client认证方式与 Google Cloud 生态一致Application Default Credentials 或服务账号密钥文件。安装依赖如果还没安装 marimo先安装它并运行marimo tutorial intro确认浏览器中打开了教程笔记本说明安装成功安装方式见 安装文档pip install marimo再安装读取 Google Sheets 所需的两个包官方集成文档给出的命令pip install gspread oauth2client下面的读取代码还会用到pandas来构造 DataFrame仓库中的示例笔记本通过 PEP 723 内联依赖声明固定了版本gspread6.1.2、oauth2client4.1.3、pandas2.2.3requires-python 3.9见 google_sheets.py。认证 Google Sheets官方文档docs/integrations/google_sheets.md给出两条认证路径按你的运行环境二选一。方式一Application Default Credentials文档推荐如果你的 marimo 运行在 Google Cloud 上、且所在资源挂载了服务账号Application Default CredentialsADC会被自动使用无需额外操作。如果 marimo 在本地运行先在终端执行 gcloud 登录命令完成 ADC 认证gcloud auth application-default login方式二服务账号密钥文件需要先在 Google Cloud 中创建一个服务账号并下载服务账号密钥文件Google Cloud 的密钥管理文档有创建步骤。下载好密钥文件后把GOOGLE_APPLICATION_CREDENTIALS环境变量指向该文件路径export GOOGLE_APPLICATION_CREDENTIALS/path/to/key/file.json其中/path/to/key/file.json是文档使用的占位符请替换为你自己下载的密钥文件的实际路径。注意该变量只对当前 shell 会话生效每个新开终端都需要重新export或者写入 shell 配置。在笔记本中读取数据认证完成后在 marimo 笔记本中用gspread建立客户端并读取工作表。以下是文档给出的代码分为两个 cell# Cell 1 - Load libraries import marimo as mo import pandas as pd import os import gspread from oauth2client.service_account import ServiceAccountCredentials # Authenticate with Google Sheets scope [ https://spreadsheets.google.com/feeds, https://www.googleapis.com/auth/drive, ] credentials ServiceAccountCredentials.from_json_keyfile_name( os.environ[GOOGLE_APPLICATION_CREDENTIALS], scope ) gc gspread.authorize(credentials)# Cell 2 - Load the sheet wks gc.open(marimo).sheet1 mo.ui.table(pd.DataFrame(wks.get_all_records()))使用这段代码时注意三个条件os.environ[GOOGLE_APPLICATION_CREDENTIALS]这一行要求环境变量已经设置即走的是服务账号密钥文件这条认证路径如果走的是 ADC 方式需要改用 ADC 凭据来构造客户端示例笔记本的写法见下一节。gc.open(marimo)中的marimo是文档示例用的电子表格名称替换成你自己的表格名称sheet1指第一个工作表换成你实际的工作表名即可。scope里的两个 URL 是文档要求的授权范围spreadsheets feeds 与 drive 权限保持原样。可选运行仓库自带的示例应用仓库里提供了完整的 Google Sheets 示例 google_sheets.py它把认证和表格定位都做成了应用内配置一个文本输入框接受服务账号密钥文件路径文档说明其 placeholder 为path/to/creds.json。填了路径就用ServiceAccountCredentials.from_json_keyfile_name认证留空则回退到GoogleCredentials.get_application_default().create_scoped(_scopes)走 ADC。一个 Spreadsheet URL 输入框填入表格 URL 后通过gc.open_by_url(...).sheet1读取再用mo.ui.table(pd.DataFrame(wks.get_all_records()), selectionNone)展示。没有填写表格 URL 时代码执行mo.stop(...)停止运行不会发起读取。本地运行时marimo run examples/cloud/gcp/google_sheets.py浏览器中打开后在应用界面填入密钥文件路径或表格 URL 即可。文档也提示该应用需要 Google Cloud Platform 账号。验证结果与边界成功的标志是 notebook 单元格输出中出现由mo.ui.table渲染的数据表内容来自wks.get_all_records()读到的记录文档未给出固定示例输出实际行数和内容取决于你的表格。文档明确或隐含的边界两条认证路径互斥于同一段代码密钥文件路径依赖GOOGLE_APPLICATION_CREDENTIALS环境变量ADC 路径依赖gcloud auth application-default login登录状态或云环境的服务账号。文档只演示了读取第一个工作表sheet1并整体转成 DataFrame 这一条链路没有涉及多工作表遍历、增量同步等内容如需对接其他 Google 数据源可参考同目录的 Google Cloud BigQuery 与 Google Cloud Storage 集成文档。【免费下载链接】marimoA reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.项目地址: https://gitcode.com/GitHub_Trending/ma/marimo创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考