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()