Skip to content

Instantly share code, notes, and snippets.

@AdelDima
Created July 25, 2023 08:12
Show Gist options
  • Save AdelDima/91b58ab0bcdc5ce95346e246ba56c973 to your computer and use it in GitHub Desktop.
Save AdelDima/91b58ab0bcdc5ce95346e246ba56c973 to your computer and use it in GitHub Desktop.
Notion Chart with deepNote
NOTION_TOKEN=secret_UQ...
DATABASE_ID=3fb5...
import os
from notion_client import Client
from dotenv import load_dotenv
from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
from datetime import timedelta
import mplcyberpunk
load_dotenv()
notion = Client(auth=os.getenv("NOTION_TOKEN"))
# Replace this URL with the URL of your database
database_id = os.getenv("DATABASE_ID")
# Use the databases.query endpoint
response = notion.databases.query(database_id)
#print(response["results"])
data = [(page["properties"]["Date"]["date"]["start"], page["properties"]["Progress"]["formula"]["number"]) for page in response["results"]]
# Sort data by date
data.sort(key=lambda x: datetime.strptime(x[0], "%Y-%m-%d"))
# Separate dates and progress into two lists
dates, progress = zip(*data)
# Create a DataFrame
df = pd.DataFrame({"date": dates, "progress": progress})
# Convert the dates to a datetime format
df["date"] = pd.to_datetime(df["date"])
# Filter to include only the last 7 days
start_date = df["date"].max() - timedelta(days=6)
df_last_7_days = df[df["date"] >= start_date].copy()
# Aggregate by day and take the mean
df_last_7_days = df_last_7_days.groupby(df_last_7_days["date"].dt.date).mean().reset_index()
# Convert the date column to the names of the day of the week
df_last_7_days["day_of_week"] = pd.to_datetime(df_last_7_days["date"]).dt.day_name()
# Set figure size
fig = plt.figure(figsize=(12, 6))
plt.style.use("cyberpunk")
# Plot progress data for last 7 days using matplotlib
colors = ["#7c3aed"]
bars = plt.bar(df_last_7_days["day_of_week"], df_last_7_days["progress"],color=colors, zorder=2)
mplcyberpunk.add_bar_gradient(bars=bars)
plt.ylabel('Habits')
plt.tight_layout() # Adjust layout for better visibility
plt.show()
notion-client
matplotlib
numpy
notion-client
python-dotenv
mplcyberpunk
@AdelDima
Copy link
Author

  1. Set up your Notion integration and get your token. You can refer to the official Notion API documentation for instructions. Set your token as an environment variable named NOTION_TOKEN.

  2. To find the ID of your Notion database, open the database in Notion and look at the URL. The URL will be something like https://www.notion.so/myworkspace/3fb5092a050042fd8b4ec66ad3e9a65e?v=.... The database ID is the long string of characters in the URL. Replace the database_id value in the script with your database ID.

Customizing the Script
Depending on your needs and the structure of your Notion database, you might need to adjust the script. For example:

If your database uses different property names, you'll need to adjust the field names in the script.
If you want to plot data over a different time range, you can adjust the date filtering section of the script.
If you want to visualize a different metric, you can adjust the plotting section of the script.

here's the database I use: https://twitter.com/TahriAdel/status/1683457048426455040

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment