concept.so的非官方python api客户端

notion的Python项目详细描述


概念py

Concept.so API v3的非官方Python3客户端。

  • 面向对象接口(将数据库表映射到python类/属性)
  • 内部概念格式和适当的python对象之间的自动转换
  • 统一数据存储中的数据本地缓存
  • 实时反应式双向数据绑定(更改python对象->;概念ui的实时更新,反之亦然)
  • 用于响应概念变化的回调系统(例如,用于触发操作、更新另一个api等)

 src=

在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!"

概念和注释

更新记录

我们保留所有通过的数据的本地缓存。当您引用记录上的属性时,我们首先查找该缓存以检索该值。如果找不到它,它将从服务器检索它。您还可以通过调用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导入页面"模式

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java检查整数是0还是检查变量是null更好?   java Android Kotlin(初学者)使用File(),并从ACTION\u GET\u内容返回Uri   java JavaFx在“内部场景”和根场景之间切换   spring将XMLBean配置转换为java配置   java JPA HIBERNATE映射列两次(embeddedID和POJO)   c#单态模式模型在什么情况下适用?   java请求。getRemoteUser在特定时间后返回null?   spring boot中PUT api控制器的java my单元测试用例失败   java在字符串中互换地解析和替换值   java Android JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起异常   JavaSpringDataMongo:使用非简单键持久化映射   爪哇玻璃鱼连接被拒绝   java如何在用户注册时发送特定电子邮件id的自动回复?   Java列表:实例化时和之后的赋值之间的差异