Notion API master series 12 - Analysis - Create a page (Last)

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

Reference API URL : Create a page

  • children (array of mixed types)
    Page content for the new page as an array of block objects
    We can add block arrays through children. By the way, what does block mean?

    (I'm not 네오피자(Neo Pizza) Promoter. But 네오피자(Neo Pizza) is really tasty. What on earth do you mean?)
    Above sky-colored long shape means to block. Put block info into children, then when the page creates, block also create together.
    How can we figure out block arrays? For reading the above block, use the Retrieve block children method.
# input page_id with as above page id
# Refer to Notion API master series 08, 09 - Analysis - Search if you need the way to find page_id
page_id = '1f217cc1-225f-4580-8e22-cd7eb8db3c5e'

blocks = notion.blocks.children.list(block_id=page_id)
pprint(blocks)

Block information is as below.

{'block': {},
 'has_more': False,
 'next_cursor': None,
 'object': 'list',
 'results': [{'archived': False,
              'created_by': {'id': 'edfacc72-eb15-467b-9fdb-d5b656e1c77b',
                             'object': 'user'},
              'created_time': '2022-04-02T11:59:00.000Z',
              'has_children': False,
              'id': '27b0ad2f-5698-4f7c-ab07-00f174042264',
              'last_edited_by': {'id': 'edfacc72-eb15-467b-9fdb-d5b656e1c77b',
                                 'object': 'user'},
              'last_edited_time': '2022-07-07T00:43:00.000Z',
              'object': 'block', ...

'results' contain arrays with block information. Put these arrays into children, then when the block creates, block create together.
Run pprint(blocks['results'][0]) and find out the block info details which contains 페페로니가 취향저격(Pepperoni is my taste).

...
 'object': 'block',
 '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'},
                             {'annotations': {'bold': False,
                                              'code': False,
                                              'color': 'default',
                                              'italic': False,
                                              'strikethrough': False,
                                              'underline': False},
                              'href': None,
                              'plain_text': ' 취향저격',
                              'text': {'content': ' 취향저격', 'link': None},
                              'type': 'text'}]}, ...

Refer to paragraph key value. We can process forms for children.

paragraph_value = blocks['results'][0]['paragraph']
children_block = {}
children_block['paragraph'] = paragraph_value

Every time we cover with {} that's inconvenient. Let's make def function.

def dict_wrap(type, object):
    wrapped = {}
    wrapped[type] = object
    return wrapped

children_block = dict_wrap(type='paragraph', object=blocks['results'][0]['paragraph'])

We prepared with this information, and then we can create but it is not the end.
Can you remember children's type? An array of mixed types means array, so its type is the list.
Then API will recognize with a list type variable.
I had trouble inputting with dictionary type, but body.parent.database_id should be defined, instead was 'undefined'. error occurred continuously. First time if the warning message mentioned you put dictionary type so bind with list type, it would be better. If we need to create many blocks, then try to append children_block.

children_blocks = []
children_blocks.append(children_block)

Now children are set as a form for adding. Add parameters with essential value, parent, and properties, and then you can create without any problem.

parent_object = {'type': 'database_id', 'database_id': '021eb424-7a41-47ae-9252-f67d4f6e6b05'}
notion.pages.create(parent=parent_object,
                    properties=properties_new,
                    children=children_blocks)
  • icon (json)
    Page icon for the new page.
    The icon means Emoji Icon before the page name.
icon_value = {'emoji': '🍕', 'type': 'emoji'}
notion.pages.create(parent=parent_object, properties=properties_new, icon=icon_value)
  • cover (json)
    Page cover for the new page
    Example for adding cover
url = 'https://images.unsplash.com/photo-1566843972142-a7fcb70de55a?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjYzOTIxfQ'
cover_value = {'external': {'url': url},'type': 'external'}
notion.pages.create(parent=parent_object, properties=properties_new, cover=cover_value)
profile
QA Specialist

0개의 댓글