Productivity Tracking with the Notion API and Python | by Lucas Soares | Towards Data Science

1
2
3
4
5
6
7
8
9
10
Powered by Play.ht
Create audio with Play.ht
Powered by Play.ht
With the recent release of the very much anticipated beta version of the Notion API, a lot of integrations between apps are now possible, and an entire universe of possibilities is now open for data science projects.
In this post, I will show you how to build a simple project tracking dashboard with Streamlit and the Notion API.
If you prefer, you can check out my youtube video on this topic:
Let’s start with creating a page on Notion. Now, you need to create an integration token on your Notion page and then share your database with that integration. A detailed tutorial on how to do that can be found here.
Now, you can set up a simple python API to communicate with your database in the Notion app so you can fetch the data.
In my case I created a simple database with each property (column on the table) referencing a project, each line relating to a day of the month, and each entry containing a checkbox indicating whether or not that project was worked on that given day. On my Notion page, it looks like this:
To fetch the data from the Notion database we can write a class with three methods:
query_databases() to fetch the data from the Notion app;get_projects_titles to get the names of the projects you are tracking (they will be the titles of the columns in your Notion database);get_projects_data to get the projects tracking data from the resulting dictionary object obtained from the Notion API.
query_databases() to fetch the data from the Notion app;
get_projects_titles to get the names of the projects you are tracking (they will be the titles of the columns in your Notion database);
get_projects_data to get the projects tracking data from the resulting dictionary object obtained from the Notion API.
The full code for the python API:
Let’s do a quick breakdown of each component:
Querying the database
Here, we are sending a post request to our database using the integration token from the Notion API, and we return the response as a json object to facilitate formatting the data in the shape we need to visualize it later.
Getting the names of the projects
Here, we are simply retrieving all the properties in our database. More details on how to work with databases using the Notion API can be found here.
Getting the projects data
Finally, we fetch the data from each project as well as the dates column.
Now that we have the data loaded up, let’s use the plotly package to plot the projects’ information.
Let’s do something simple and plot a bar chart with the number of days spent on each project over the tracked period (in this case the last 45 days) and a project events scatter plot that shows my daily activity (whether or not I worked on each project).
Let’s start by importing our dependencies.
import plotly.graph_objs as goimport pandas as pdimport plotly.express as pxfrom notion_api import *
Now we use our python API to fetch the data from the Notion database.
nsync = NotionSync()data = nsync.query_databases()projects = nsync.get_projects_titles(data)projects_data,dates = nsync.get_projects_data(data,projects)
We set up a dataframe with the data we want to explore and plot the events tracker and the bar chart.
It works perfectly! The really cool aspect of this tool is that, as we update our Notion page, this plot will update automatically without any extra effort needed.
Now, let’s plot an events plot that tells us what we are working on each day over a given time period.
We will need to turn the boolean values from the checkbox property in the database into continuous values that we can plot in such a way that each line in the y-axis represents a different project and each dot over that line region will represent having worked on that project on that given day.
There we have it. To finish things off, let’s wrap this code on a Streamlit app so that we can look at our data anytime we want by just running a command on the terminal.
To do that, we just need a few lines of code:
The load_data function takes care of fetching the data directly from our Notion database, and the following code is just setting up a title and a subheader for the plots to be shown in Streamlit using the plotly package. The minimalistic style of Streamlit makes this really simple.
On the terminal, run the Streamlit app with: streamlit run app.py
The entire source code for this project can be found here.
This new beta version of the Notion API is really powerful. Realizing that the app had only provided an SDK in javascript, I wrote this code to make it nicely integrated with python.
The integrations that Notion now offers with this official API can really take productivity projects like this one to a whole other level and I feel like we will see more and more really interesting features appearing in the upcoming future.
If you liked this post connect with me on Twitter, LinkedIn and follow me on Medium. Thanks and see you next time! :)
Getting Started with the Notion API
working with databases in Notion
retrieve database in Notion
Python API integration tutorial