concept.so的非官方python api客户端
notion的Python项目详细描述
概念py
Concept.so API v3的非官方Python3客户端。
- 面向对象接口(将数据库表映射到python类/属性)
- 内部概念格式和适当的python对象之间的自动转换
- 统一数据存储中的数据本地缓存
- 实时反应式双向数据绑定(更改python对象->;概念ui的实时更新,反之亦然)
- 用于响应概念变化的回调系统(例如,用于触发操作、更新另一个api等)
在Jamie的博客上阅读有关concept和concept py的更多信息
注意:最新版本的concept py需要python 3.5或更高版本。 我们保留所有通过的数据的本地缓存。当您引用用法
快速启动
PIP安装概念
fromnotion.clientimportNotionClient# Obtain the `token_v2` value by inspecting your browser cookies on a logged-in session on Notion.soclient=NotionClient(token_v2="<token_v2>")# Replace this URL with the URL of the page you want to editpage=client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")print("The old title is:",page.title)# Note: You can use Markdown! We convert on-the-fly to Notion's internal formatted text data structure.page.title="The title has now changed, and has *live-updated* in the browser!"
概念和注释
record
),类的每个实例表示一个特定的记录。记录中的某些字段(如上例中的title
)已映射到模型属性,允许轻松、即时地读取/写入记录。其他字段可以用get
方法读取,也可以用set
方法写入,但是您需要确保与内部结构完全匹配。block
类及其子类,对应不同的块类型,空格(通过空格
类),集合(通过集合
类),集合视图(通过集合视图
和子类),以及concept用户(通过用户
类)。record
实例中的数据有时可能在没有显式请求的情况下更新。您还可以调用记录上的
刷新
方法来触发更新,或将
强制更新
传递给
获取
等方法。
时,可以传入id或页面的url。注意,页面本身只是myrecord.get()
来查看记录的完整内部结构。块e页面上的所有内容块。您可以通过单击页面中某个块的上下文菜单中的"复制链接"来获取该块的URL,并将该URL传递到
get block
中。更新记录
记录
上的属性时,我们首先查找该缓存以检索该值。如果找不到它,它将从服务器检索它。您还可以通过调用refresh
方法手动刷新记录的数据。默认情况下(除非我们用
monitor=false
实例化nomensionclient
,否则我们还为任何实例化的记录订阅长轮询更新
记录的本地缓存数据应该在服务器上的任何数据更改后不久自动实时更新。长轮询发生在后台后台后台进程线程中。
示例:遍历块树
forchildinpage.children:print(child.title)print("Parent of {} is {}".format(page.id,page.parent.id))
示例:添加新节点
fromnotion.blockimportTodoBlocknewchild=page.children.add_new(TodoBlock,title="Something to get done")newchild.checked=True
示例:删除节点
# soft-deletepage.remove()# hard-deletepage.remove(permanently=True)
示例:创建嵌入式内容类型(iframe、video等)
fromnotion.blockimportVideoBlockvideo=page.children.add_new(VideoBlock,width=200)# sets "property.source" to the URL, and "format.display_source" to the embedly-converted URLvideo.set_source_url("https://www.youtube.com/watch?v=oHg5SJYRHA0")
示例:创建新的嵌入集合视图块
collection=client.get_collection(COLLECTION_ID)# get an existing collectioncvb=page.children.add_new(CollectionViewBlock,collection=collection)view=cvb.views.add_new(view_type="table")# now the filters and format options on the view can bet set as desired.# # for example:# view.set("query", ...)# view.set("format.board_groups", ...)# view.set("format.board_properties", ...)
示例:移动块
# move my block to after the videomy_block.move_to(video,"after")# move my block to the end of otherblock's childrenmy_block.move_to(otherblock,"last-child")# (you can also use "before" and "first-child")
示例:订阅更新
我们可以"监视"a记录
以便在它发生更改时获得a回调。结合基于长轮询的记录实时更新,这允许一种"反应式"设计,在这种设计中,可以触发本地应用程序中的操作,以响应与概念接口的交互。
# define a callback (note: all arguments are optional, just include the ones you care about)defmy_callback(record,difference):print("The record's title is now:"record.title)print("Here's what was changed:")print(difference)# move my block to after the videomy_block.add_callback(my_callback)
示例:使用数据库,也称为"集合"(表、板等)
以下是事情的组合方式:
- 主容器块:
collectionviewblock
(内联)/collectionviewpageblock
(整页)集合
(包含架构,并且是数据库行本身的父级)集合行块
集合行块
(更多数据库记录)
集合视图
(保存每个特定视图的筛选器/排序等)
注意:为了方便起见,我们基于集合
中定义的模式,自动将数据库"columns"(aka properties)映射到集合行块
实例的getter/setter属性中。属性名是列名称的"slugified"版本。因此,如果您有一个名为"Estimated Value"的列,那么可以通过myrowblock.Estimated_value
读写它。可进行一些基本验证,并将其转换为适当的内部格式。对于"person"类型的列,我们期望有一个用户
实例或它们的列表;对于"relation",我们期望有一个块
的子类的单个/列表实例
# Access a database using the URL of the database page or the inline blockcv=client.get_collection_view("https://www.notion.so/myorg/8511b9fc522249f79b90768b832599cc?v=8dee2a54f6b64cb296c83328adba78e1")# List all the records with "Bob" in themforrowincv.collection.get_rows(search="Bob"):print("We estimate the value of '{}' at {}".format(row.name,row.estimated_value))# Add a new recordrow=cv.collection.add_row()row.name="Just some data"row.is_confirmed=Truerow.estimated_value=399row.files=["https://www.birdlife.org/sites/default/files/styles/1600/public/slide.jpg"]row.person=client.current_userrow.tags=["A","C"]row.where_to="https://learningequality.org"# Run a filtered/sorted query using a view's default parametersresult=cv.default_query().execute()forrowinresult:print(row)# Run an "aggregation" queryaggregate_params=[{"property":"estimated_value","aggregation_type":"sum","id":"total_value",}]result=cv.build_query(aggregate=aggregate_params).execute()print("Total estimated value:",result.get_aggregate("total_value"))# Run a "filtered" queryfilter_params=[{"property":"assigned_to","comparator":"enum_contains","value":client.current_user,}]result=cv.build_query(filter=filter_params).execute()print("Things assigned to me:",result)# Run a "sorted" querysort_params=[{"direction":"descending","property":"estimated_value",}]result=cv.build_query(sort=sort_params).execute()print("Sorted results, showing most valuable first:",result)
注意:您可以组合筛选器
、聚合
和排序
。在concept中设置复杂视图,然后检查cv.get("query")
您还可以在Smoke Test Runner中看到更多的示例。使用:
fromnotion.clientimportNotionClient# Obtain the `token_v2` value by inspecting your browser cookies on a logged-in session on Notion.soclient=NotionClient(token_v2="<token_v2>")# Replace this URL with the URL of the page you want to editpage=client.get_block("https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821")print("The old title is:",page.title)# Note: You can use Markdown! We convert on-the-fly to Notion's internal formatted text data structure.page.title="The title has now changed, and has *live-updated* in the browser!"0
快速插入:学习平等就是雇佣!
我们是一家小型非营利机构,具有全球影响力,正在建设令人兴奋的技术!我们目前正在招聘工程师和其他角色--加入我们吧!
待办事项
- 按层次结构克隆页
- 去缓冲缓存?
- 在降价转换中支持内联"用户"和"页面"链接以及提醒
- 支持更新/创建集合架构的实用程序
- 支持更新/创建集合视图查询的实用程序
- 支持轻松管理页面权限
- WebSocket支持实时块缓存更新
- "将整页呈现为降价"模式
- "从HTML导入页面"模式