Notion API master series 05 - Change the content (First)

소찬 (Chan)·2022년 8월 8일
0
post-thumbnail

However, if we request to update rich_text information to Notion API, rich_text composition is complicated. It doesn't allow a slight difference from the demanded structure.
But if you follow these steps, you can change the content exactly.

Read page or block information.
Save into temporary variable about rich_text content from page or block.
Modify the value from the temporary variable
Update the info with the changed variable then Notion API will update it without error.

Firstly, read the page for changing info.
If you read until Notion API master series 04, you can find page_id with title name.
We assume written content on a page and launch the code to read block.

page_id = '2c9258e1-43ed-4713-8895-123d42268d2f'
block_list = notion.blocks.children.list(block_id=page_id)
pprint(block_list)

The interesting point is that we can use page_id as block_id. It means that the page and block are compatible. In conclusion, notion concept is like this
database > page > block
A database is a bunch of pages. A page is a bunch of blocks.

Now, look into the block_list value. Then you can find a block containing above '페퍼로니가 취향저격' (The pepperoni is right up my alley).
block_list is a composed list array.

'id': '27b0ad2f-5698-4f7c-ab07-00f174042264'
...
'paragraph': {'color': 'default',
              'rich_text': [{'annotations': {'bold': False,
                                             'code': False,
                                             'color': 'default',
                                             'italic': False,
                                             'strikethrough': False,
                                             'underline': False},
                             'href': None,
                             'plain_text': '페퍼로니가 취향저격',
                             'text': {'content': '페퍼로니가 취향저격',
                                      'link': None},
                             'type': 'text'}]}, ...

At this, save and keep the block_id value from block info that contains '페퍼로니가 취향저격'(The pepperoni is right up my alley).
If that value contains the first block, then bring the ['id'] value from ['results'] order is [0].
If that value contains the second block, then bring the ['id'] value from ['results'] order is [1].

That value is in [0], block id saves to target_block_id variable, then we can declare like this.

target_block_id = block_list['results'][0]['id']

Now we try to change from '페퍼로니가 취향저격'(The pepperoni is right up my alley) to '크러스트가 취향저격'(The crust is right up my alley)
We will save the value from rich_text.

{'block': {},
 'has_more': False,
 'next_cursor': None,
 'object': 'list',
 'results': [{'archived': False,
              'created_by': {'id': 'edfacc72-eb15-467b-9fdb-d5b656e1c77b',
                             'object': 'user'}, ...
              'paragraph': {'color': 'default',
                            'rich_text': [{'annotations': {'bold': False,

Change the value from block_list['results'] - order is [0] - ['paragraph'] - ['rich_text'] variable.
rich_text_temp = block_list['results'][0]['paragraph']['rich_text']

Now we print rich_text_temp value via pprint.

[{'annotations': {'bold': False,
                  'code': False,
                  'color': 'default',
                  'italic': False,
                  'strikethrough': False,
                  'underline': False},
  'href': None,
  'plain_text': '페퍼로니가 취향저격',
  'text': {'content': '페퍼로니가 취향저격', 'link': None},
  'type': 'text'}]

If you are sensitive, then you realize something is different.
That is dictionary {} is covered with list {}.
When updating Notion API, we have to cover with list [] array.

Now, we need to change parts 'plain_text' and 'content' of 'text'.
Read original text and save a variable from rich_text_temp variable.

text_value = rich_text_temp[0]['plain_text']
text_value = text_value.replace('페퍼로니', '크러스트')

Replace text_value's 'plain_text' and 'content' of 'text' from rich_text_temp. and then update with replaced value. API will work.

rich_text_temp[0]['plain_text'] = text_value
rich_text_temp[0]['text']['content'] = text_value
pprint(rich_text_temp)

pprint will show the value as below.

[{'annotations': {'bold': False,
                  'code': False,
                  'color': 'default',
                  'italic': False,
                  'strikethrough': False,
                  'underline': False},
  'href': None,
  'plain_text': '크러스트가 취향저격',
  'text': {'content': '크러스트가 취향저격', 'link': None},
  'type': 'text'}]

Make a dictionary for updating rich_text. Then we are ready to update.

rich_text = {}
rich_text['rich_text'] = rich_text_temp
profile
QA Specialist

0개의 댓글