{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1dc7df1d",
   "metadata": {},
   "source": [
    "# Notion DB 2/2\n",
    "\n",
    ">[Notion](https://www.notion.so/) is a collaboration platform with modified Markdown support that integrates kanban boards, tasks, wikis and databases. It is an all-in-one workspace for notetaking, knowledge and data management, and project and task management.\n",
    "\n",
    "`NotionDBLoader` is a Python class for loading content from a `Notion` database. It retrieves pages from the database, reads their content, and returns a list of Document objects.\n",
    "\n",
    "## Requirements\n",
    "\n",
    "- A `Notion` Database\n",
    "- Notion Integration Token\n",
    "\n",
    "## Setup\n",
    "\n",
    "### 1. Create a Notion Table Database\n",
    "Create a new table database in Notion. You can add any column to the database and they will be treated as metadata. For example you can add the following columns:\n",
    "\n",
    "- Title: set Title as the default property.\n",
    "- Categories: A Multi-select property to store categories associated with the page.\n",
    "- Keywords: A Multi-select property to store keywords associated with the page.\n",
    "\n",
    "Add your content to the body of each page in the database. The NotionDBLoader will extract the content and metadata from these pages.\n",
    "\n",
    "## 2. Create a Notion Integration\n",
    "To create a Notion Integration, follow these steps:\n",
    "\n",
    "1. Visit the [Notion Developers](https://www.notion.com/my-integrations) page and log in with your Notion account.\n",
    "2. Click on the \"+ New integration\" button.\n",
    "3. Give your integration a name and choose the workspace where your database is located.\n",
    "4. Select the require capabilities, this extension only need the Read content capability\n",
    "5. Click the \"Submit\" button to create the integration.\n",
    "Once the integration is created, you'll be provided with an `Integration Token (API key)`. Copy this token and keep it safe, as you'll need it to use the NotionDBLoader.\n",
    "\n",
    "### 3. Connect the Integration to the Database\n",
    "To connect your integration to the database, follow these steps:\n",
    "\n",
    "1. Open your database in Notion.\n",
    "2. Click on the three-dot menu icon in the top right corner of the database view.\n",
    "3. Click on the \"+ New integration\" button.\n",
    "4. Find your integration, you may need to start typing its name in the search box.\n",
    "5. Click on the \"Connect\" button to connect the integration to the database.\n",
    "\n",
    "\n",
    "### 4. Get the Database ID\n",
    "To get the database ID, follow these steps:\n",
    "\n",
    "1. Open your database in Notion.\n",
    "2. Click on the three-dot menu icon in the top right corner of the database view.\n",
    "3. Select \"Copy link\" from the menu to copy the database URL to your clipboard.\n",
    "4. The database ID is the long string of alphanumeric characters found in the URL. It typically looks like this: https://www.notion.so/username/8935f9d140a04f95a872520c4f123456?v=.... In this example, the database ID is 8935f9d140a04f95a872520c4f123456.\n",
    "\n",
    "With the database properly set up and the integration token and database ID in hand, you can now use the NotionDBLoader code to load content and metadata from your Notion database.\n",
    "\n",
    "## Usage\n",
    "NotionDBLoader is part of the langchain package's document loaders. You can use it as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6c3a314c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "········\n",
      "········\n"
     ]
    }
   ],
   "source": [
    "from getpass import getpass\n",
    "\n",
    "NOTION_TOKEN = getpass()\n",
    "DATABASE_ID = getpass()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "007c5cbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import NotionDBLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a1caec59",
   "metadata": {},
   "outputs": [],
   "source": [
    "loader = NotionDBLoader(\n",
    "    integration_token=NOTION_TOKEN,\n",
    "    database_id=DATABASE_ID,\n",
    "    request_timeout_sec=30,  # optional, defaults to 10\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b1c30ff7",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4f5789a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "print(docs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
