|
11 | 11 |
|
12 | 12 | from notion_df.values import PageProperties, PageProperty
|
13 | 13 | from notion_df.configs import DatabaseSchema, NON_EDITABLE_TYPES
|
14 |
| -from notion_df.utils import is_uuid |
15 |
| -from notion_df.blocks import parse_blocks |
| 14 | +from notion_df.utils import is_uuid, flatten_dict |
| 15 | +from notion_df.blocks import parse_blocks, BaseNotionBlock |
16 | 16 |
|
17 | 17 | API_KEY = None
|
18 | 18 | NOT_REVERSE_DATAFRAME = -1
|
@@ -235,20 +235,36 @@ def create_database(
|
235 | 235 | return response
|
236 | 236 |
|
237 | 237 |
|
238 |
| -def upload_row_to_database(row, database_id, schema, client) -> Dict: |
| 238 | +def upload_row_to_database(row, database_id, schema, children, client) -> Dict: |
239 | 239 |
|
240 | 240 | properties = PageProperty.from_series(row, schema).query_dict()
|
241 |
| - response = client.pages.create( |
242 |
| - parent={"database_id": database_id}, properties=properties |
243 |
| - ) |
| 241 | + if children: |
| 242 | + if not isinstance(children, list): |
| 243 | + children = [children] |
| 244 | + for cid in range(len(children)): |
| 245 | + if isinstance(children[cid], BaseNotionBlock): |
| 246 | + children[cid] = flatten_dict(children[cid].dict()) |
| 247 | + |
| 248 | + response = client.pages.create( |
| 249 | + parent={"database_id": database_id}, properties=properties, children=children |
| 250 | + ) |
| 251 | + else: |
| 252 | + response = client.pages.create( |
| 253 | + parent={"database_id": database_id}, properties=properties, |
| 254 | + ) |
244 | 255 | return response
|
245 | 256 |
|
246 | 257 |
|
247 |
| -def upload_to_database(df, databse_id, schema, client, errors) -> List[Dict]: |
| 258 | +def upload_to_database(df, databse_id, schema, client, errors, children) -> List[Dict]: |
248 | 259 | all_response = []
|
249 |
| - for _, row in df[::NOT_REVERSE_DATAFRAME].iterrows(): |
| 260 | + if children is not None: |
| 261 | + assert len(children) == len(df) |
| 262 | + children = children[::NOT_REVERSE_DATAFRAME] |
| 263 | + |
| 264 | + for idx, (_, row) in enumerate(df[::NOT_REVERSE_DATAFRAME].iterrows(), ): |
250 | 265 | try:
|
251 |
| - response = upload_row_to_database(row, databse_id, schema, client) |
| 266 | + child = children[idx] if children is not None else None |
| 267 | + response = upload_row_to_database(row, databse_id, schema, child, client) |
252 | 268 | all_response.append(response)
|
253 | 269 | except Exception as e:
|
254 | 270 | if errors == "strict":
|
@@ -277,6 +293,7 @@ def upload(
|
277 | 293 | errors: str = "strict",
|
278 | 294 | resolve_relation_values: bool = False,
|
279 | 295 | create_new_rows_in_relation_target: bool = False,
|
| 296 | + children: List[Union[Dict, BaseNotionBlock]] = None, |
280 | 297 | return_response: bool = False,
|
281 | 298 | *,
|
282 | 299 | api_key: str = None,
|
@@ -317,6 +334,9 @@ def upload(
|
317 | 334 | subsequent rows.
|
318 | 335 | 3. "warn": print the error message and continue uploading
|
319 | 336 | Defaults to "strict".
|
| 337 | + children (List[Union[Dict, BaseNotionBlock]], optional): |
| 338 | + The corresponding children of the uploaded Notion page. It should be |
| 339 | + a list of the same length as the dataframe. |
320 | 340 | resolve_relation_values (bool, optional):
|
321 | 341 | If `True`, notion-df assumes the items in any relation columns
|
322 | 342 | are not notion object ids, but the value of the corresponding
|
@@ -432,7 +452,7 @@ def upload(
|
432 | 452 | lambda row: [obj_string_to_id[ele] for ele in row if ele in obj_string_to_id]
|
433 | 453 | )
|
434 | 454 |
|
435 |
| - response = upload_to_database(df, databse_id, schema, client, errors) |
| 455 | + response = upload_to_database(df, databse_id, schema, client, errors, children) |
436 | 456 |
|
437 | 457 | print(f"Your dataframe has been uploaded to the Notion page: {notion_url} .")
|
438 | 458 | if return_response:
|
|
0 commit comments