notionhelper 0.1.8__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {notionhelper-0.1.8 → notionhelper-0.2.1}/PKG-INFO +115 -34
- notionhelper-0.2.1/README.md +310 -0
- notionhelper-0.2.1/images/helper_logo.png +0 -0
- notionhelper-0.2.1/images/logo.png +0 -0
- notionhelper-0.2.1/images/pillio.png +0 -0
- notionhelper-0.2.1/images/pillio2.png +0 -0
- notionhelper-0.2.1/notion_api_examples.md +234 -0
- notionhelper-0.2.1/notionapi_md_info.md +13308 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/pyproject.toml +3 -2
- {notionhelper-0.1.8 → notionhelper-0.2.1}/src/notionhelper/helper.py +265 -214
- notionhelper-0.2.1/uv.lock +2341 -0
- notionhelper-0.1.8/CLAUDE.md +0 -74
- notionhelper-0.1.8/README.md +0 -230
- notionhelper-0.1.8/images/helper_logo.png +0 -0
- notionhelper-0.1.8/uv.lock +0 -921
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.claude/settings.local.json +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.coverage +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.github/workflows/claude-code-review.yml +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.github/workflows/claude.yml +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.gitignore +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/.python-version +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/images/json_builder.png.png +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/pytest.ini +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/src/notionhelper/__init__.py +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/tests/README.md +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/tests/__init__.py +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/tests/conftest.py +0 -0
- {notionhelper-0.1.8 → notionhelper-0.2.1}/tests/test_helper.py +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: notionhelper
|
|
3
|
-
Version: 0.1
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: NotionHelper is a Python library that simplifies interactions with the Notion API, enabling easy management of databases, pages, and files within Notion workspaces.
|
|
5
5
|
Author-email: Jan du Plessis <drjanduplessis@icloud.com>
|
|
6
6
|
Requires-Python: >=3.10
|
|
7
7
|
Requires-Dist: mimetype>=0.1.5
|
|
8
|
+
Requires-Dist: notebook>=7.4.5
|
|
8
9
|
Requires-Dist: notion-client>=2.4.0
|
|
9
10
|
Requires-Dist: pandas>=2.3.1
|
|
10
11
|
Requires-Dist: requests>=2.32.4
|
|
@@ -28,7 +29,7 @@ Description-Content-Type: text/markdown
|
|
|
28
29
|
|
|
29
30
|

|
|
30
31
|
|
|
31
|
-
`NotionHelper` is a Python library that provides a convenient interface for interacting with the Notion API
|
|
32
|
+
`NotionHelper` is a Python library that provides a convenient interface for interacting with the Notion API, specifically designed to leverage the **Notion API Version 2025-09-03**. It simplifies common tasks such as managing databases, data sources, pages, and file uploads, allowing you to integrate Notion's powerful features into your applications with ease.
|
|
32
33
|
|
|
33
34
|
For help constructing the JSON for the properties, use the [Notion API - JSON Builder](https://notioinapiassistant.streamlit.app) Streamlit app.
|
|
34
35
|
|
|
@@ -37,10 +38,11 @@ For help constructing the JSON for the properties, use the [Notion API - JSON Bu
|
|
|
37
38
|
- **Synchronous Operations**: Uses `notion-client` and `requests` for straightforward API interactions.
|
|
38
39
|
- **Type Safety**: Full type hints for all methods ensuring better development experience and IDE support.
|
|
39
40
|
- **Error Handling**: Robust error handling for API calls and file operations.
|
|
40
|
-
- **Database Management**: Create, query, and
|
|
41
|
-
- **Page Operations**: Add new pages to
|
|
41
|
+
- **Database & Data Source Management**: Create, retrieve, query, and update Notion databases and their associated data sources.
|
|
42
|
+
- **Page Operations**: Add new pages to data sources and append content to existing pages.
|
|
42
43
|
- **File Handling**: Upload files and attach them to pages or page properties with built-in validation.
|
|
43
|
-
- **Pandas Integration**: Convert Notion
|
|
44
|
+
- **Pandas Integration**: Convert Notion data source pages into a Pandas DataFrame for easy data manipulation.
|
|
45
|
+
- **API Version 2025-09-03 Compliance**: Fully updated to support the latest Notion API changes, including the separation of databases and data sources.
|
|
44
46
|
|
|
45
47
|
## Installation
|
|
46
48
|
|
|
@@ -83,29 +85,52 @@ notion_token = os.getenv("NOTION_TOKEN")
|
|
|
83
85
|
helper = NotionHelper(notion_token)
|
|
84
86
|
```
|
|
85
87
|
|
|
86
|
-
### Retrieve a Database
|
|
88
|
+
### Retrieve a Database (Container)
|
|
89
|
+
|
|
90
|
+
With API version `2025-09-03`, `get_database` now returns the database object, which acts as a container for one or more data sources. To get the actual schema (properties), you need to retrieve a specific data source using `get_data_source`.
|
|
87
91
|
|
|
88
92
|
```python
|
|
89
|
-
database_id = "your_database_id"
|
|
90
|
-
|
|
91
|
-
print(
|
|
93
|
+
database_id = "your_database_id" # ID of the database container
|
|
94
|
+
database_object = helper.get_database(database_id)
|
|
95
|
+
print(database_object)
|
|
96
|
+
|
|
97
|
+
# To get the schema of a specific data source within this database:
|
|
98
|
+
data_source_id = database_object["data_sources"][0]["id"] # Get the ID of the first data source
|
|
99
|
+
data_source_schema = helper.get_data_source(data_source_id)
|
|
100
|
+
print(data_source_schema)
|
|
92
101
|
```
|
|
93
102
|
|
|
94
|
-
### Create a New Page in a
|
|
103
|
+
### Create a New Page in a Data Source
|
|
104
|
+
|
|
105
|
+
With API version `2025-09-03`, pages are parented by `data_source_id`, not `database_id`. When creating a new page, ensure you use the `data_source_id` of the specific table you want to add the page to.
|
|
106
|
+
|
|
107
|
+
**Important Note on Property Definitions:** When defining properties for the *schema* of a database or data source, use an empty object `{}` for the property type (e.g., `"My Title Column": {"title": {}}`). However, when defining properties for a *new page* (as shown below), you provide the actual content using rich text arrays or other specific property value objects.
|
|
95
108
|
|
|
96
109
|
```python
|
|
110
|
+
data_source_id = "your_data_source_id" # The ID of the specific data source (table)
|
|
111
|
+
|
|
97
112
|
page_properties = {
|
|
98
|
-
"Name": {
|
|
113
|
+
"Task Name": { # This must match a 'title' property in your data source schema
|
|
99
114
|
"title": [
|
|
100
115
|
{
|
|
101
116
|
"text": {
|
|
102
|
-
"content": "New
|
|
117
|
+
"content": "New Task from NotionHelper"
|
|
103
118
|
}
|
|
104
119
|
}
|
|
105
120
|
]
|
|
121
|
+
},
|
|
122
|
+
"Status": { # This must match a 'select' property in your data source schema
|
|
123
|
+
"select": {
|
|
124
|
+
"name": "Not Started" # Must be one of the options defined in your data source
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"Due Date": { # This must match a 'date' property in your data source schema
|
|
128
|
+
"date": {
|
|
129
|
+
"start": "2025-12-31"
|
|
130
|
+
}
|
|
106
131
|
}
|
|
107
132
|
}
|
|
108
|
-
new_page = helper.
|
|
133
|
+
new_page = helper.new_page_to_data_source(data_source_id, page_properties)
|
|
109
134
|
print(new_page)
|
|
110
135
|
```
|
|
111
136
|
|
|
@@ -139,11 +164,69 @@ helper.append_page_body(page_id, blocks)
|
|
|
139
164
|
print(f"Successfully appended content to page ID: {page_id}")
|
|
140
165
|
```
|
|
141
166
|
|
|
142
|
-
### Get all pages as a Pandas DataFrame
|
|
167
|
+
### Get all pages from a Data Source as a Pandas DataFrame
|
|
143
168
|
|
|
144
169
|
```python
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
data_source_id = "your_data_source_id" # The ID of the specific data source (table)
|
|
171
|
+
df = helper.get_data_source_pages_as_dataframe(data_source_id)
|
|
172
|
+
print(df.head())
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Update a Data Source
|
|
176
|
+
|
|
177
|
+
This example demonstrates how to update the schema (properties/columns), title, icon, or other attributes of an existing data source.
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
data_source_id = "your_data_source_id" # The ID of the data source to update
|
|
181
|
+
|
|
182
|
+
# Example 1: Rename a property and add a new one
|
|
183
|
+
update_payload_1 = {
|
|
184
|
+
"properties": {
|
|
185
|
+
"Old Property Name": { # Existing property name or ID
|
|
186
|
+
"name": "New Property Name" # New name for the property
|
|
187
|
+
},
|
|
188
|
+
"New Text Property": { # Add a new rich text property
|
|
189
|
+
"rich_text": {}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
updated_data_source_1 = helper.update_data_source(data_source_id, properties=update_payload_1["properties"])
|
|
194
|
+
print(f"Updated data source (rename and add): {updated_data_source_1}")
|
|
195
|
+
|
|
196
|
+
# Example 2: Update data source title and remove a property
|
|
197
|
+
update_payload_2 = {
|
|
198
|
+
"title": [
|
|
199
|
+
{
|
|
200
|
+
"type": "text",
|
|
201
|
+
"text": {
|
|
202
|
+
"content": "Updated Data Source Title"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
],
|
|
206
|
+
"properties": {
|
|
207
|
+
"Property To Remove": None # Set to None to remove a property
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
updated_data_source_2 = helper.update_data_source(data_source_id, title=update_payload_2["title"], properties=update_payload_2["properties"])
|
|
211
|
+
print(f"Updated data source (title and remove): {updated_data_source_2}")
|
|
212
|
+
|
|
213
|
+
# Example 3: Update a select property's options
|
|
214
|
+
update_payload_3 = {
|
|
215
|
+
"properties": {
|
|
216
|
+
"Status": { # Assuming 'Status' is an existing select property
|
|
217
|
+
"select": {
|
|
218
|
+
"options": [
|
|
219
|
+
{"name": "To Do", "color": "gray"},
|
|
220
|
+
{"name": "In Progress", "color": "blue"},
|
|
221
|
+
{"name": "Done", "color": "green"},
|
|
222
|
+
{"name": "Blocked", "color": "red"} # Add a new option
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
updated_data_source_3 = helper.update_data_source(data_source_id, properties=update_payload_3["properties"])
|
|
229
|
+
print(f"Updated data source (select options): {updated_data_source_3}")
|
|
147
230
|
```
|
|
148
231
|
|
|
149
232
|
### Upload a File and Attach to a Page
|
|
@@ -218,20 +301,22 @@ The NotionHelper library includes several quality improvements:
|
|
|
218
301
|
|
|
219
302
|
The `NotionHelper` class provides the following methods:
|
|
220
303
|
|
|
221
|
-
### Database Operations
|
|
222
|
-
- **`get_database(database_id)`** - Retrieves the
|
|
223
|
-
- **`
|
|
224
|
-
- **`
|
|
304
|
+
### Database & Data Source Operations
|
|
305
|
+
- **`get_database(database_id)`** - Retrieves the database object (container), which includes a list of its data sources.
|
|
306
|
+
- **`get_data_source(data_source_id)`** - Retrieves a specific data source, including its properties (schema).
|
|
307
|
+
- **`create_database(parent_page_id, database_title, initial_data_source_properties, initial_data_source_title=None)`** - Creates a new database with an initial data source.
|
|
308
|
+
- **`update_data_source(data_source_id, properties=None, title=None, icon=None, in_trash=None, parent=None)`** - Updates the attributes of a specified data source.
|
|
309
|
+
- **`notion_search_db(query="", filter_object_type="page")`** - Searches for pages or data sources in Notion.
|
|
225
310
|
|
|
226
|
-
### Page Operations
|
|
227
|
-
- **`
|
|
228
|
-
- **`append_page_body(page_id, blocks)`** - Appends blocks of
|
|
229
|
-
- **`
|
|
311
|
+
### Page Operations
|
|
312
|
+
- **`new_page_to_data_source(data_source_id, page_properties)`** - Adds a new page to a Notion data source with the specified properties.
|
|
313
|
+
- **`append_page_body(page_id, blocks)`** - Appends blocks of text to the body of a Notion page.
|
|
314
|
+
- **`get_page(page_id)`** - Retrieves the JSON of the page properties and an array of blocks on a Notion page given its page_id.
|
|
230
315
|
|
|
231
316
|
### Data Retrieval & Conversion
|
|
232
|
-
- **`
|
|
233
|
-
- **`
|
|
234
|
-
- **`
|
|
317
|
+
- **`get_data_source_page_ids(data_source_id)`** - Returns the IDs of all pages in a given data source.
|
|
318
|
+
- **`get_data_source_pages_as_json(data_source_id, limit=None)`** - Returns a list of JSON objects representing all pages in the given data source, with all properties.
|
|
319
|
+
- **`get_data_source_pages_as_dataframe(data_source_id, limit=None, include_page_ids=True)`** - Retrieves all pages from a Notion data source and returns them as a Pandas DataFrame.
|
|
235
320
|
|
|
236
321
|
### File Operations
|
|
237
322
|
- **`upload_file(file_path)`** - Uploads a file to Notion and returns the file upload object
|
|
@@ -241,16 +326,12 @@ The `NotionHelper` class provides the following methods:
|
|
|
241
326
|
|
|
242
327
|
### One-Step Convenience Methods
|
|
243
328
|
- **`one_step_image_embed(page_id, file_path)`** - Uploads and embeds an image in one operation
|
|
244
|
-
- **`one_step_file_to_page(page_id, file_path)`** - Uploads and attaches a file to a page in one operation
|
|
329
|
+
- **`one_step_file_to_page(page_id, file_path)`** - Uploads and attaches a file to a page in one operation
|
|
245
330
|
- **`one_step_file_to_page_property(page_id, property_name, file_path, file_name)`** - Uploads and attaches a file to a page property in one operation
|
|
246
331
|
|
|
247
|
-
### Utility Methods
|
|
248
|
-
- **`info()`** - Displays comprehensive library information with all available methods (Jupyter notebook compatible)
|
|
249
|
-
|
|
250
332
|
## Requirements
|
|
251
333
|
|
|
252
334
|
- Python 3.10+
|
|
253
|
-
-
|
|
254
|
-
- pandas >= 2.3.1
|
|
335
|
+
- pandas >= 2.3.1
|
|
255
336
|
- requests >= 2.32.4
|
|
256
337
|
- mimetype >= 0.1.5
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# NotionHelper
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
`NotionHelper` is a Python library that provides a convenient interface for interacting with the Notion API, specifically designed to leverage the **Notion API Version 2025-09-03**. It simplifies common tasks such as managing databases, data sources, pages, and file uploads, allowing you to integrate Notion's powerful features into your applications with ease.
|
|
6
|
+
|
|
7
|
+
For help constructing the JSON for the properties, use the [Notion API - JSON Builder](https://notioinapiassistant.streamlit.app) Streamlit app.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Synchronous Operations**: Uses `notion-client` and `requests` for straightforward API interactions.
|
|
12
|
+
- **Type Safety**: Full type hints for all methods ensuring better development experience and IDE support.
|
|
13
|
+
- **Error Handling**: Robust error handling for API calls and file operations.
|
|
14
|
+
- **Database & Data Source Management**: Create, retrieve, query, and update Notion databases and their associated data sources.
|
|
15
|
+
- **Page Operations**: Add new pages to data sources and append content to existing pages.
|
|
16
|
+
- **File Handling**: Upload files and attach them to pages or page properties with built-in validation.
|
|
17
|
+
- **Pandas Integration**: Convert Notion data source pages into a Pandas DataFrame for easy data manipulation.
|
|
18
|
+
- **API Version 2025-09-03 Compliance**: Fully updated to support the latest Notion API changes, including the separation of databases and data sources.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
To install `NotionHelper`, you can use `pip`:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install notionhelper
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This will also install all the necessary dependencies, including `notion-client`, `pandas`, and `requests`.
|
|
29
|
+
|
|
30
|
+
## Authentication
|
|
31
|
+
|
|
32
|
+
To use the Notion API, you need to create an integration and obtain an integration token.
|
|
33
|
+
|
|
34
|
+
1. **Create an Integration**: Go to [My Integrations](https://www.notion.so/my-integrations) and create a new integration.
|
|
35
|
+
2. **Get the Token**: Copy the "Internal Integration Token".
|
|
36
|
+
3. **Share with a Page/Database**: For your integration to access a page or database, you must share it with your integration from the "Share" menu in Notion.
|
|
37
|
+
|
|
38
|
+
It is recommended to store your Notion token as an environment variable for security.
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
export NOTION_TOKEN="your_secret_token"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
Here is an example of how to use the library:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import os
|
|
50
|
+
from notionhelper import NotionHelper
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Initialize the NotionHelper class
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
notion_token = os.getenv("NOTION_TOKEN")
|
|
57
|
+
|
|
58
|
+
helper = NotionHelper(notion_token)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Retrieve a Database (Container)
|
|
62
|
+
|
|
63
|
+
With API version `2025-09-03`, `get_database` now returns the database object, which acts as a container for one or more data sources. To get the actual schema (properties), you need to retrieve a specific data source using `get_data_source`.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
database_id = "your_database_id" # ID of the database container
|
|
67
|
+
database_object = helper.get_database(database_id)
|
|
68
|
+
print(database_object)
|
|
69
|
+
|
|
70
|
+
# To get the schema of a specific data source within this database:
|
|
71
|
+
data_source_id = database_object["data_sources"][0]["id"] # Get the ID of the first data source
|
|
72
|
+
data_source_schema = helper.get_data_source(data_source_id)
|
|
73
|
+
print(data_source_schema)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Create a New Page in a Data Source
|
|
77
|
+
|
|
78
|
+
With API version `2025-09-03`, pages are parented by `data_source_id`, not `database_id`. When creating a new page, ensure you use the `data_source_id` of the specific table you want to add the page to.
|
|
79
|
+
|
|
80
|
+
**Important Note on Property Definitions:** When defining properties for the *schema* of a database or data source, use an empty object `{}` for the property type (e.g., `"My Title Column": {"title": {}}`). However, when defining properties for a *new page* (as shown below), you provide the actual content using rich text arrays or other specific property value objects.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
data_source_id = "your_data_source_id" # The ID of the specific data source (table)
|
|
84
|
+
|
|
85
|
+
page_properties = {
|
|
86
|
+
"Task Name": { # This must match a 'title' property in your data source schema
|
|
87
|
+
"title": [
|
|
88
|
+
{
|
|
89
|
+
"text": {
|
|
90
|
+
"content": "New Task from NotionHelper"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
"Status": { # This must match a 'select' property in your data source schema
|
|
96
|
+
"select": {
|
|
97
|
+
"name": "Not Started" # Must be one of the options defined in your data source
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"Due Date": { # This must match a 'date' property in your data source schema
|
|
101
|
+
"date": {
|
|
102
|
+
"start": "2025-12-31"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
new_page = helper.new_page_to_data_source(data_source_id, page_properties)
|
|
107
|
+
print(new_page)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Append Content to the New Page
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
blocks = [
|
|
114
|
+
{
|
|
115
|
+
"object": "block",
|
|
116
|
+
"type": "heading_2",
|
|
117
|
+
"heading_2": {
|
|
118
|
+
"rich_text": [{"type": "text", "text": {"content": "Hello from NotionHelper!"}}]
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"object": "block",
|
|
123
|
+
"type": "paragraph",
|
|
124
|
+
"paragraph": {
|
|
125
|
+
"rich_text": [
|
|
126
|
+
{
|
|
127
|
+
"type": "text",
|
|
128
|
+
"text": {
|
|
129
|
+
"content": "This content was appended synchronously."
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
helper.append_page_body(page_id, blocks)
|
|
137
|
+
print(f"Successfully appended content to page ID: {page_id}")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Get all pages from a Data Source as a Pandas DataFrame
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
data_source_id = "your_data_source_id" # The ID of the specific data source (table)
|
|
144
|
+
df = helper.get_data_source_pages_as_dataframe(data_source_id)
|
|
145
|
+
print(df.head())
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Update a Data Source
|
|
149
|
+
|
|
150
|
+
This example demonstrates how to update the schema (properties/columns), title, icon, or other attributes of an existing data source.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
data_source_id = "your_data_source_id" # The ID of the data source to update
|
|
154
|
+
|
|
155
|
+
# Example 1: Rename a property and add a new one
|
|
156
|
+
update_payload_1 = {
|
|
157
|
+
"properties": {
|
|
158
|
+
"Old Property Name": { # Existing property name or ID
|
|
159
|
+
"name": "New Property Name" # New name for the property
|
|
160
|
+
},
|
|
161
|
+
"New Text Property": { # Add a new rich text property
|
|
162
|
+
"rich_text": {}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
updated_data_source_1 = helper.update_data_source(data_source_id, properties=update_payload_1["properties"])
|
|
167
|
+
print(f"Updated data source (rename and add): {updated_data_source_1}")
|
|
168
|
+
|
|
169
|
+
# Example 2: Update data source title and remove a property
|
|
170
|
+
update_payload_2 = {
|
|
171
|
+
"title": [
|
|
172
|
+
{
|
|
173
|
+
"type": "text",
|
|
174
|
+
"text": {
|
|
175
|
+
"content": "Updated Data Source Title"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
],
|
|
179
|
+
"properties": {
|
|
180
|
+
"Property To Remove": None # Set to None to remove a property
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
updated_data_source_2 = helper.update_data_source(data_source_id, title=update_payload_2["title"], properties=update_payload_2["properties"])
|
|
184
|
+
print(f"Updated data source (title and remove): {updated_data_source_2}")
|
|
185
|
+
|
|
186
|
+
# Example 3: Update a select property's options
|
|
187
|
+
update_payload_3 = {
|
|
188
|
+
"properties": {
|
|
189
|
+
"Status": { # Assuming 'Status' is an existing select property
|
|
190
|
+
"select": {
|
|
191
|
+
"options": [
|
|
192
|
+
{"name": "To Do", "color": "gray"},
|
|
193
|
+
{"name": "In Progress", "color": "blue"},
|
|
194
|
+
{"name": "Done", "color": "green"},
|
|
195
|
+
{"name": "Blocked", "color": "red"} # Add a new option
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
updated_data_source_3 = helper.update_data_source(data_source_id, properties=update_payload_3["properties"])
|
|
202
|
+
print(f"Updated data source (select options): {updated_data_source_3}")
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Upload a File and Attach to a Page
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
try:
|
|
209
|
+
file_path = "path/to/your/file.pdf" # Replace with your file path
|
|
210
|
+
upload_response = helper.upload_file(file_path)
|
|
211
|
+
file_upload_id = upload_response["id"]
|
|
212
|
+
# Replace with your page_id
|
|
213
|
+
page_id = "your_page_id"
|
|
214
|
+
attach_response = helper.attach_file_to_page(page_id, file_upload_id)
|
|
215
|
+
print(f"Successfully uploaded and attached file: {attach_response}")
|
|
216
|
+
except Exception as e:
|
|
217
|
+
print(f"Error uploading file: {e}")
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Simplified File Operations
|
|
221
|
+
|
|
222
|
+
NotionHelper provides convenient one-step methods that combine file upload and attachment operations:
|
|
223
|
+
|
|
224
|
+
#### one_step_image_embed()
|
|
225
|
+
Uploads an image and embeds it in a Notion page in a single call, combining what would normally require:
|
|
226
|
+
1. Uploading the file
|
|
227
|
+
2. Embedding it in the page
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
page_id = "your_page_id"
|
|
231
|
+
image_path = "path/to/image.png"
|
|
232
|
+
response = helper.one_step_image_embed(page_id, image_path)
|
|
233
|
+
print(f"Successfully embedded image: {response}")
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### one_step_file_to_page()
|
|
237
|
+
Uploads a file and attaches it to a Notion page in one step, combining:
|
|
238
|
+
1. Uploading the file
|
|
239
|
+
2. Attaching it to the page
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
page_id = "your_page_id"
|
|
243
|
+
file_path = "path/to/document.pdf"
|
|
244
|
+
response = helper.one_step_file_to_page(page_id, file_path)
|
|
245
|
+
print(f"Successfully attached file: {response}")
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### one_step_file_to_page_property()
|
|
249
|
+
Uploads a file and attaches it to a specific Files & Media property on a page, combining:
|
|
250
|
+
1. Uploading the file
|
|
251
|
+
2. Attaching it to the page property
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
page_id = "your_page_id"
|
|
255
|
+
property_name = "Files" # Name of your Files & Media property
|
|
256
|
+
file_path = "path/to/document.pdf"
|
|
257
|
+
file_name = "Custom Display Name.pdf" # Optional display name
|
|
258
|
+
response = helper.one_step_file_to_page_property(page_id, property_name, file_path, file_name)
|
|
259
|
+
print(f"Successfully attached file to property: {response}")
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
These methods handle all the intermediate steps automatically, making file operations with Notion much simpler.
|
|
263
|
+
|
|
264
|
+
## Code Quality
|
|
265
|
+
|
|
266
|
+
The NotionHelper library includes several quality improvements:
|
|
267
|
+
|
|
268
|
+
- **Type Hints**: All methods include comprehensive type annotations for better IDE support and code clarity
|
|
269
|
+
- **Error Handling**: Built-in validation and exception handling for common failure scenarios
|
|
270
|
+
- **Clean Imports**: Explicit imports with `__all__` declaration for better namespace management
|
|
271
|
+
- **Production Ready**: Removed debug output and implemented proper error reporting
|
|
272
|
+
|
|
273
|
+
## Complete Function Reference
|
|
274
|
+
|
|
275
|
+
The `NotionHelper` class provides the following methods:
|
|
276
|
+
|
|
277
|
+
### Database & Data Source Operations
|
|
278
|
+
- **`get_database(database_id)`** - Retrieves the database object (container), which includes a list of its data sources.
|
|
279
|
+
- **`get_data_source(data_source_id)`** - Retrieves a specific data source, including its properties (schema).
|
|
280
|
+
- **`create_database(parent_page_id, database_title, initial_data_source_properties, initial_data_source_title=None)`** - Creates a new database with an initial data source.
|
|
281
|
+
- **`update_data_source(data_source_id, properties=None, title=None, icon=None, in_trash=None, parent=None)`** - Updates the attributes of a specified data source.
|
|
282
|
+
- **`notion_search_db(query="", filter_object_type="page")`** - Searches for pages or data sources in Notion.
|
|
283
|
+
|
|
284
|
+
### Page Operations
|
|
285
|
+
- **`new_page_to_data_source(data_source_id, page_properties)`** - Adds a new page to a Notion data source with the specified properties.
|
|
286
|
+
- **`append_page_body(page_id, blocks)`** - Appends blocks of text to the body of a Notion page.
|
|
287
|
+
- **`get_page(page_id)`** - Retrieves the JSON of the page properties and an array of blocks on a Notion page given its page_id.
|
|
288
|
+
|
|
289
|
+
### Data Retrieval & Conversion
|
|
290
|
+
- **`get_data_source_page_ids(data_source_id)`** - Returns the IDs of all pages in a given data source.
|
|
291
|
+
- **`get_data_source_pages_as_json(data_source_id, limit=None)`** - Returns a list of JSON objects representing all pages in the given data source, with all properties.
|
|
292
|
+
- **`get_data_source_pages_as_dataframe(data_source_id, limit=None, include_page_ids=True)`** - Retrieves all pages from a Notion data source and returns them as a Pandas DataFrame.
|
|
293
|
+
|
|
294
|
+
### File Operations
|
|
295
|
+
- **`upload_file(file_path)`** - Uploads a file to Notion and returns the file upload object
|
|
296
|
+
- **`attach_file_to_page(page_id, file_upload_id)`** - Attaches an uploaded file to a specific page
|
|
297
|
+
- **`embed_image_to_page(page_id, file_upload_id)`** - Embeds an uploaded image into a page
|
|
298
|
+
- **`attach_file_to_page_property(page_id, property_name, file_upload_id, file_name)`** - Attaches a file to a Files & Media property
|
|
299
|
+
|
|
300
|
+
### One-Step Convenience Methods
|
|
301
|
+
- **`one_step_image_embed(page_id, file_path)`** - Uploads and embeds an image in one operation
|
|
302
|
+
- **`one_step_file_to_page(page_id, file_path)`** - Uploads and attaches a file to a page in one operation
|
|
303
|
+
- **`one_step_file_to_page_property(page_id, property_name, file_path, file_name)`** - Uploads and attaches a file to a page property in one operation
|
|
304
|
+
|
|
305
|
+
## Requirements
|
|
306
|
+
|
|
307
|
+
- Python 3.10+
|
|
308
|
+
- pandas >= 2.3.1
|
|
309
|
+
- requests >= 2.32.4
|
|
310
|
+
- mimetype >= 0.1.5
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|