Automating Notion with Python

Automata Learning Lab · Beginner ·🔧 Backend Engineering ·3y ago

Key Takeaways

The video demonstrates how to automate Notion with Python using the Notion API, covering setting up integrations, creating databases, and interacting with the API to read, write, and manage tasks. Tools like Notion API, Python, and CLI are utilized to achieve this automation.

Full Transcript

in this video we're going to learn how to automate notion databases using python okay so these are going to be the steps to automate to-do lists with python and the notion API I'm doing an example where we're creating a database that's supposed to be our to-do list and then we're going to automate checking and unchecking tasks as well as creating new tasks in that database notion using python to do that we'll have to do the following steps first we're going to set up a new integration in notion and generate an API key then we're going to create a database for the to-do list and get the corresponding ID finally we're going to initialize the variables holding the API keys and the relevant database urls then we're going to write a function to read the database we're going to write a function to list all the tasks present in that database we're going to write another one to check and uncheck the tasks in the to-do list and then we're going to write one to add tasks to the database and the last step is we're going to wrap everything into a command line tool so let's go to each of these in detail so for setting up an integration in notion all you need to do is head up to line integration so the notion that I saw forward slash my Integrations you click on new integration and then you copy the generated API token which we're going to use inside our python script then we're going to create a database for that to-do list and get the corresponding ID so to create a database in notion we just open a page in our notion workspace and we type in forward slash database so something like this so to create a database notion all you need to do is you come here in notion and you write database inline or through Page we're going to choose full page and then we have our database I'm going to call my tasks and that's what you got to do now when okay so after we've done that we're gonna initialize the variables that are holding the API keys and the relevant database urls so to do this we get a first export the corresponding keys to our environment so we're just to avoid stuff like you know you putting API Keys uh publicly available on GitHub or you know you don't want to have API keys in your code base so you're gonna update your dot basharc file and you're going to write two lines which are going to be explored notion token as and then you give your notion integration key that you got when you went to the my Integrations part and the notion you're going to export the notion test database ID variable that you got when you extracted the database ID when you create it and then we're going to replace these with the value the values that are relevant for your project now we can access those from inside Jupiter notebook so this is what I'm doing here I'm importing the necessary dependencies for this project we're going to go through what am I importing here and then I'm setting three variables the token variable which is the notion token that you just exported the database ID that you just created and you got from the uh from your notion page and the in the URL that the URL you're going to use to make updates to that database when you're doing post requests to that notion page finally you're going to set up your headers so that you can authenticate your requests when you're interacting with the notion API and then yeah so this is what we're doing here uh we have the token the database ID the update database URL and the headers which is just a dictionary to set up the authentication for sending the requests to the notion API finally we're going to write a function to read that database so this is the function to read the database in the ocean basically what we're doing is we have a read URL which is URL we're going to use to send a post request to the database querying for information so this is the post request then we're going to store that response as a Json file we're going to print the status of our request so that we know if it worked or not in here honestly I'm doing something that actually regret doing which is just saving that Json file I should be just returning the data but you know when I did this I was saving as a Json file I don't recommend saving Json file locally in your folder because this file is going to have a bunch of information about um Pages inside that page and it's going to have like the IDS for those things so you know it's not the best idea to have this uh saved in your folder but at the same time for organization it's useful okay so now that we did this um okay so now that we did this we're going to write a function to list all the tasks present in that database so the function to list all the tasks is going to be uh the get tasks from the database and we're gonna get the length of our return results this is going to be the length of our table or the database that we created with our tasks then we're going to look through that we're going to Loop through that database and we're gonna get the tasks from the database which is what we're doing here we're accessing the results then we're accessing the properties of the database uh I'm assuming that your database has a column called task but you could easily change that to whatever you want you could just replace this as a variable put it here and you're good to go uh and then I'm printing out the task with the corresponding ID number with the corresponding index number of that task so that you can access later for checking and unchecking and creating Test new tests perfect so this is what a gitask function does it calculates the number of tests in the database which basically is just the length of the result of that request we created them to list the store that's not true that's not happening anymore we look through each task in the database checking if it has a title and then we extract the name of each task and print the index and the name of that task uh now we're going to write a function to check and uncheck the tasks in the to-do list which is our emotional database so this is the check test function we have a new an update URL and basically that update URL is the URL we use from the page corresponding to that database so that we can make changes to the property of that page in this case we want to add we want to change the property of the checkbox then we're going to say um that the property of the checkbox is going to be equal to a check status which in this case is true but this function checks and unchecks the task so uh and we're going to change that when we wrap everything into a CLI too but right now that's how it looks and we're going to get the test name we're going to update the task and dump that into a Json a Json file and then we're going to send a patch request to the update URL of that page with the headers and the updated task page that now has the check box checked and then we're going to print task was checked and that's it so that's what we're doing we're checking the property of the test page and then updating that data converted to a Json string and sending that to the notion API through an HTTP patch request and then we print that to the console okay perfect so now we write a function to add tasks to that database so this is the function to create a task it has three arguments it has the test title the test database ID and the headers so we set up our update database URL we set up an update page URL and then we create a dictionary that will contain the contents for our new page so that's statistic dictionary right here it has to have the name of the column matching the name of the tasks in that in the original database so in this case task and then I give the title and inside of it I say text content and then then the task title or the test description this is the argument that holds the contents of your test description then I set up the parent dictionary which is the this is the original task database and you're gonna understand that in a second then we're going to combine these two properties so we're going to have the data as so the parent is apparent and then the properties on your page so basically we have our task list and then inside of it we have the page matching the new task you're creating and then we create a post request to the page URL which is the page of your task database and we print the response so that's pretty much it and now we're gonna wrap everything into a cli2 and we're going to test it out so that you can see this thing working and the CLI 2 is I'm going to show it in ejector notebook just a bit more it's a bit easier to understand so let's head over there um so this is the notion CLI so the notion CLI is here to contain so we're importing our dependencies here in the Autos module I put the all the functions that we just discussed so three database read page there's some other functions here that I'm going to discuss in a new video and we get the get the asks from database we have all those functions here and they were importing to this uh to the script we're setting up our tokens our test database ID and the URL to update the database and the headers dictionary and now I'm using the r Parts module from python to create a simple CLI tool I'm adding a few arguments so get which is going to get all the tasks from the test database an argument called check task to check a task when it was done a unchecked task argument to check uncheck a task in case you know I thought I was done but then I wasn't and then a net desk argument where I create a task from scratch and I just add it to the database and then I parse these arguments my parsleys are just right here and then I set up my uh uh I set up the actions so if I call the get argument with the name to Do's right now it just has this functionality but I'll write something later uh and then I just called the database function I read the database and then I print the outputs of the get asked from database which is the function we discussed just now and so I'm calling the get tasks from database which is right here so it is I'm gonna erase this because I'm not using this list I'm just going to look through all the tasks in the database and print that out okay so to Showcase what that looks like let's just do a little demo so here on my left I'm gonna have the command line two and on my right I'm gonna have my notion page with the last database head over to my notion page this is the original database with the um uh with the desks that I was uh that I was creating and now we're gonna say okay uh I'm gonna run motion by CLI I'm going to say get and we're going to say to deuce and as you can see here on the left from the zero to eight I have eight tasks from uh bottom to top here on the notion database with my task so run code server added a YouTube some random tasks that I put here and it works super well and it works super fast which is great um okay so now for the second argument the check test argument I'm setting up the argument so that it takes in a new integer and then this integer matches the index of the task so zero one two three four five six seven eight in the case of this test database and then I just say okay uh get a page in the database and I give the task database ID which is the database ID for the task list table thing that you're seeing here on the right and then I say okay uh uncheck that task or a check sorry check that task or uncheck it uh and what does that look like in practice well I can say biotin version CLI and then I can say check desk and then let's say I'm gonna check the Run code server as a background service because I already did that so I'm going to say zero and we'll run and as you can see in real time or whatever you just check the task and now let's say uncheck desk which is the other argument and say uncheck the task and it just uncheck the test like you're seeing here so yeah it's fun to play with apis and finally let's look at the end desk argument so I can say python notion CLI and then I say add a task and I just give a description for the task so finish YouTube video about the python notion API integration okay integration I can say that I can run this and now as you can see here on my right the function the task was added to my database and that's pretty much it uh thanks for watching don't forget to like And subscribe and see you next time cheers

Original Description

#automation #python #notion #productivity #datascience In this video, we’ll go through how to automate Notion with Python. We'll learn how you can update a Notion database directly from Python code using the Notion API. Chapters 00:00 - Introduction 00:30 - Steps 01:06 - Set up a new integration in Notion and generate an API key 01:26 - Create a database for the todo list and get its corresponding id 02:13 - Initialize the variables holding the API keys and relevant database URLs 04:11 - Write a function to read the database 05:22 - Write a function to list all the tasks present in that database 06:52 - Write a function to check and uncheck tasks in the todo list 08:32 - Write a function to add tasks to that database 10:08 - Wrap everything into a cli tool 13:01 - Testing the automation Ok, now let's get started! Let’s go! B). - Subscribe!: https://www.youtube.com/channel/UCu8WF59Scx9f3H1N_FgZUwQ - Join Medium: https://lucas-soares.medium.com/membership - Tiktok: https://www.tiktok.com/@enkrateialucca?lang=en - Twitter: https://twitter.com/LucasEnkrateia - LinkedIn: https://www.linkedin.com/in/lucas-soares-969044167/ Some affiliate links (if you use them you support the channel! :)) - Kindle Oasis: https://amzn.to/3IUtaOh - Seagate Portable 2TB External Hard Drive HDD: https://amzn.to/3QSZ8wd - Sony WH-1000XM5 Wireless with Noise Cancelling: https://amzn.to/3HfJvM8 Music ”Before Chill” by Yomoti from www.epidemicsound.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Automata Learning Lab · Automata Learning Lab · 31 of 60

1 A Quick Tutorial on NLP Basics
A Quick Tutorial on NLP Basics
Automata Learning Lab
2 Automating your Digital Morning Routine with Python
Automating your Digital Morning Routine with Python
Automata Learning Lab
3 Exploring Problem Solving with Python and Jupyter Notebook #1
Exploring Problem Solving with Python and Jupyter Notebook #1
Automata Learning Lab
4 Summarize Papers with Python and GPT-3
Summarize Papers with Python and GPT-3
Automata Learning Lab
5 An Experiment Tracking Tutorial with Mlflow and Keras
An Experiment Tracking Tutorial with Mlflow and Keras
Automata Learning Lab
6 Automating Google Forms Submissions with Python
Automating Google Forms Submissions with Python
Automata Learning Lab
7 Productivity Tracking With Python and the Notion API
Productivity Tracking With Python and the Notion API
Automata Learning Lab
8 When your Machine Learning Model Fails Do This ;p
When your Machine Learning Model Fails Do This ;p
Automata Learning Lab
9 Machine Learning Tip#1 Practical Deep Learning Course
Machine Learning Tip#1 Practical Deep Learning Course
Automata Learning Lab
10 Machine Learning Tips: Deep Learning Monitor
Machine Learning Tips: Deep Learning Monitor
Automata Learning Lab
11 Machine Learning Tips#5 MLOPs specialization in Coursera #machinelearning
Machine Learning Tips#5 MLOPs specialization in Coursera #machinelearning
Automata Learning Lab
12 Automatically Changing Desktop Wallpaper with Python and the Nasa Image API
Automatically Changing Desktop Wallpaper with Python and the Nasa Image API
Automata Learning Lab
13 Building an Image Classifier to Filter Out Unused Images From Your Photo Album with Machine Learning
Building an Image Classifier to Filter Out Unused Images From Your Photo Album with Machine Learning
Automata Learning Lab
14 Automating VS Code Snippets with Python
Automating VS Code Snippets with Python
Automata Learning Lab
15 How to Set Up a Machine Learning Environment with Conda and Pip-Tools
How to Set Up a Machine Learning Environment with Conda and Pip-Tools
Automata Learning Lab
16 9 Google Search Tips for Machine Learning
9 Google Search Tips for Machine Learning
Automata Learning Lab
17 Thinking Tools
Thinking Tools
Automata Learning Lab
18 Automating Car Search with Python and Data Science
Automating Car Search with Python and Data Science
Automata Learning Lab
19 Generating Images from Text with Stable Diffusion and Hugging Face
Generating Images from Text with Stable Diffusion and Hugging Face
Automata Learning Lab
20 A Practical Introduction to Data Science using the Spaceship Titanic Dataset from Kaggle
A Practical Introduction to Data Science using the Spaceship Titanic Dataset from Kaggle
Automata Learning Lab
21 Jiu Jitsu App with Python and Streamlit
Jiu Jitsu App with Python and Streamlit
Automata Learning Lab
22 2 Apps for Coding In The Ipad Pro
2 Apps for Coding In The Ipad Pro
Automata Learning Lab
23 From Tensorflow to Pytorch?
From Tensorflow to Pytorch?
Automata Learning Lab
24 Building an Audio Transcription App with OpenAI Whisper and Streamlit
Building an Audio Transcription App with OpenAI Whisper and Streamlit
Automata Learning Lab
25 Productivity Tracking with Python Short Summary
Productivity Tracking with Python Short Summary
Automata Learning Lab
26 Automating Expense Reports with Python
Automating Expense Reports with Python
Automata Learning Lab
27 ChatGPT, Angry Pandas and AI Code
ChatGPT, Angry Pandas and AI Code
Automata Learning Lab
28 7 Strategies To Learn Anything Using ChatGPT
7 Strategies To Learn Anything Using ChatGPT
Automata Learning Lab
29 Building a Thought Summarization App with Whisper and GPT3
Building a Thought Summarization App with Whisper and GPT3
Automata Learning Lab
30 Visualize a Neural Net Learning Polynomial Functions
Visualize a Neural Net Learning Polynomial Functions
Automata Learning Lab
Automating Notion with Python
Automating Notion with Python
Automata Learning Lab
32 Pose Tracking for Jiu Jitsu - Update #jiujitsu #machinelearning
Pose Tracking for Jiu Jitsu - Update #jiujitsu #machinelearning
Automata Learning Lab
33 Update to my Pose Tracking for Jiu Jitsu Project #machinelearning #jiujitsu #ai #deeplearning
Update to my Pose Tracking for Jiu Jitsu Project #machinelearning #jiujitsu #ai #deeplearning
Automata Learning Lab
34 ChatGPT API Released by OpenAI
ChatGPT API Released by OpenAI
Automata Learning Lab
35 ChatGPT API Response Format #machinelearning #ai #datascience
ChatGPT API Response Format #machinelearning #ai #datascience
Automata Learning Lab
36 Beyond Stable Diffusion with Composer | Automata Learning Lab Paper Series #1
Beyond Stable Diffusion with Composer | Automata Learning Lab Paper Series #1
Automata Learning Lab
37 Beyond Diffusion Models with Composer #machinelearning #ai
Beyond Diffusion Models with Composer #machinelearning #ai
Automata Learning Lab
38 Machine Learning for Jiu Jitsu
Machine Learning for Jiu Jitsu
Automata Learning Lab
39 Prompt Engineering Basics #machinelearning #gpt4 #chatgpt
Prompt Engineering Basics #machinelearning #gpt4 #chatgpt
Automata Learning Lab
40 Visual ChatGPT: Integrating Images with ChatGPT Paper Series#2
Visual ChatGPT: Integrating Images with ChatGPT Paper Series#2
Automata Learning Lab
41 Visual ChatGPT #machinelearning #ai #artificialintelligence
Visual ChatGPT #machinelearning #ai #artificialintelligence
Automata Learning Lab
42 LERF - Language Embeddings + NERF for Querying 3D Spaces #machinelearning #ai
LERF - Language Embeddings + NERF for Querying 3D Spaces #machinelearning #ai
Automata Learning Lab
43 Summarize Papers with Python and ChatGPT
Summarize Papers with Python and ChatGPT
Automata Learning Lab
44 Large Language Models can use Tools Now! #artificialintelligence #machinelearning #ai
Large Language Models can use Tools Now! #artificialintelligence #machinelearning #ai
Automata Learning Lab
45 Sparks of AGI in GPT4? #machinelearning #ai #agi #artificialintelligence
Sparks of AGI in GPT4? #machinelearning #ai #agi #artificialintelligence
Automata Learning Lab
46 Toolformer: LLMs can use Tools! #chatgpt #llms #gpt4 #gpt3 #artificialintelligence
Toolformer: LLMs can use Tools! #chatgpt #llms #gpt4 #gpt3 #artificialintelligence
Automata Learning Lab
47 Talking to Your Notes with LangChain #artificialintelligence #llms #gpt4 #chatgpt
Talking to Your Notes with LangChain #artificialintelligence #llms #gpt4 #chatgpt
Automata Learning Lab
48 How to Talk to a PDF using LangChain and ChatGPT
How to Talk to a PDF using LangChain and ChatGPT
Automata Learning Lab
49 Query Your Own Notes With LangChain
Query Your Own Notes With LangChain
Automata Learning Lab
50 HuggingGPT #machinelearning #artificialintelligence #huggingface #gpt4 #chatgpt
HuggingGPT #machinelearning #artificialintelligence #huggingface #gpt4 #chatgpt
Automata Learning Lab
51 Do as I Can Not as I Say Paper #artificialintelligence #llms #reinforcementlearning
Do as I Can Not as I Say Paper #artificialintelligence #llms #reinforcementlearning
Automata Learning Lab
52 Automating Anki Flashcards with OpenAI and GPT-4
Automating Anki Flashcards with OpenAI and GPT-4
Automata Learning Lab
53 Building A PDF Summarization App with  Gradio and LangChain
Building A PDF Summarization App with Gradio and LangChain
Automata Learning Lab
54 Auto-GPT #artificialintelligence #gpt4 #llms #autogpt
Auto-GPT #artificialintelligence #gpt4 #llms #autogpt
Automata Learning Lab
55 DocGPT - Chat with Github #artificialintelligence #gpt4 #chatgpt
DocGPT - Chat with Github #artificialintelligence #gpt4 #chatgpt
Automata Learning Lab
56 LLMs for Research and Planning #artificialintelligence #gpt4 #llms
LLMs for Research and Planning #artificialintelligence #gpt4 #llms
Automata Learning Lab
57 How I Use ChatGPT for Interactive Language Learning
How I Use ChatGPT for Interactive Language Learning
Automata Learning Lab
58 Building an Audio Transcription App with Gradio and Whisper
Building an Audio Transcription App with Gradio and Whisper
Automata Learning Lab
59 Summarizing and Querying Multiple Papers with LangChain
Summarizing and Querying Multiple Papers with LangChain
Automata Learning Lab
60 Mojo - The New AI Programming Language?
Mojo - The New AI Programming Language?
Automata Learning Lab

This video teaches how to automate Notion with Python, covering setup, database interaction, and task management using the Notion API and CLI tools. It's essential for those looking to streamline their workflow and enhance productivity. By following the steps outlined, viewers can create custom automation scripts tailored to their needs.

Key Takeaways
  1. Set up a new integration in Notion and generate an API key
  2. Create a database for a to-do list and get the corresponding ID
  3. Initialize variables holding API keys and database URLs
  4. Write functions to read, list tasks, check/uncheck tasks, and add tasks to the database
  5. Use Notion API to read and write tasks from a database
  6. Create a task in Notion with a title, description, and parent database
  7. Set up a CLI tool with arguments for getting tasks, checking tasks, and creating new tasks
💡 The Notion API provides a powerful interface for automating tasks and interacting with databases, and when combined with Python and CLI tools, it enables the creation of custom automation scripts that can significantly enhance productivity.

Related AI Lessons

Chapters (11)

Introduction
0:30 Steps
1:06 Set up a new integration in Notion and generate an API key
1:26 Create a database for the todo list and get its corresponding id
2:13 Initialize the variables holding the API keys and relevant database URLs
4:11 Write a function to read the database
5:22 Write a function to list all the tasks present in that database
6:52 Write a function to check and uncheck tasks in the todo list
8:32 Write a function to add tasks to that database
10:08 Wrap everything into a cli tool
13:01 Testing the automation
Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →