Sometimes, I want to visualize some of my experimental results, or share them with others. One of the easiest ways to do this is using notion: which is capable of handling a variety of data types, and can be published to a relatively nice looking website, with minimal effort.
Step 1: Creating a notion integration
To do this, you first have to create a notion integration, which you can do here. Creating an integration will give you access to an API token, which we can use in the python script to interact with our notion database.
Step 2: Create a database using Notion’s web interface
Next, you’ll want to create a database using Notion’s web interface. This is pretty straightforward, and you can find
instructions on how to do this here. Next, we’ll need the ID
of the database, which you can find by navigating to the database, and copying the URL. The ID is the last part of the
URL, and will look something like this: https://dmchan.notion.site/January-23-January-26-Paper-Review-24cb0a2d2c74438495321a85a8080331
.
At the same time, we’ll need to give our integration access to the database. You can do this by navigating to the database, clicking on the “…” button, scrolling to “Connections” and clicking “Add connection”. Then, you can select the integration you created in step 1, and give it access to the database.
Step 3: Writing the python script
Now that we have our integration set up, and our database created, we can write a python script to append content to the database. Here’s an example of a script that appends a new row to the database:
# pip install notion-client
from notion_client import Client
import os
client = Client(auth=os.environ['NOTION_TOKEN'])
database_id = "24cb0a2d2c74438495321a85a8080331"
new_row = {
"parent": {"database_id": database_id},
"properties": {
"Name": {"title": [{"text": {"content": "New row"}}]},
"Tags": {"multi_select": [{"name": "New tag"}]},
"Description": {"rich_text": [{"text": {"content": "This is a new row"}}]}
},
# The children field is used to add content to the page, you can add a lot of different types of content here.
# For example, you can add a paragraph, a heading, a list, or an image.
# Unfortunately, the API doesn't support adding images directly, instead, you'll have to upload them to S3 or another
# service, and then add a link to the image in the page content.
"children": {
"object": "block",
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": "This is some page content!"
}
}
]
}
}
}
client.pages.create(**new_row)
In this script, we first import the Client
class from the notion_client
package, and then create a new Client
object
using our API token. We then define a dictionary new_row
that contains the properties of the new row we want to append
to the database. In this case, we’re adding a new row with a “Name” and “Description” field. Finally, we call client.pages.create
with the new_row
dictionary as an argument, which appends the new row to the database.
Conclusion
In this post, we went over how to create a notion integration, create a database using Notion’s web interface, and write a python script to append content to the database. This is a simple example, but you can use the notion API to do more complex operations, such as updating existing rows, or querying the database. You can find more information about the notion API here.