Don't feel like reading the doc π ? Get started with this 3min video.
If you are not familiar with Notion, check out their website. It's pretty amazing all-in-workspace.
Find below is their websiteπ
Before anything, you have to connect to your account in notion and get your session token
First, click right on the notion page and select inspect
Then select the Application tab (shown in the red box)
Finally, look for the token_v2
and copy the value on the right, this is the way to login to your Notion workspace.
import naas_driversβtoken = "*********"url = "https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821"collection = naas_drivers.notion.connect(token=token).get_collection(url)collection
import naas_driversβtoken = "*********"url = "https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821"n = naas_drivers.notion.connect(token=token)cv = n.get_collection(url, raw=True)β# 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"
Get the notion page content
import naas_driversβtoken = "*********"url = "https://www.notion.so/myorg/Test-c0d20a71c0944985ae96e661ccc99821"page = naas_drivers.notion.connect(token=token).get(url)β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!"
for child in page.children:print(child.title)βprint("Parent of {} is {}".format(page.id, page.parent.id))
from notion.block import TodoBlockβnewchild = page.children.add_new(TodoBlock, title="Something to get done")newchild.checked = True
# soft-deletepage.remove()β# hard-deletepage.remove(permanently=True)
from notion.block import VideoBlockβvideo = 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")β# Before the view can be browsed in Notion,# the filters and format options on the view should be 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")
# The "locked" property is available on PageBlock and CollectionViewBlock objects# Set it to True to lock the page/databasepage.locked = True# and False to unlock it againpage.locked = False
You can also get it in raw format to be able to edit it :
Discover more usage with the documentation of the original notion python package we are using.