我们首先来说明一下,当上传一个图片,我们看一下rows的东西。
关于rows的array结构,在上一篇文章说过。
rows输出如下json in array structure data type.
[{
‘name’: ‘第一行’,
‘age’: ‘799’,
‘file’: [{
‘name’: ‘IMG_5157.jpg’,
‘size’: 72591,
‘type’: ‘file’,
‘upload_time’: ‘2024-04-10T05:50:07.168+00:00’,
‘url’: ‘https://cloud.seatable.cn/workspace/xxxxxx/asset/c2645e9b-6cf3-40xxxxxxxba4/files/2024-04/IMG_5157.jpg’
}],
‘_locked’: None,
‘_locked_by’: None,
‘_archived’: False,
‘_creator’: ‘ed2fa69d@auth.local’,
‘_ctime’: ‘2024-04-10T10:41:36.157+08:00’,
‘_last_modifier’: ‘ed9d@auth.local’,
‘_mtime’: ‘2024-04-10T13:50:08+08:00’,
‘_id’: ‘Bla1K4zbRJ2KKmOMWPs3AA’
}, {
‘name’: ‘第二行’,
‘age’: ‘12212’,
‘file’: None,
‘_locked’: None,
‘_locked_by’: None,
‘_archived’: False,
‘_creator’: None,
‘_ctime’: ‘2024-04-10T11:22:57.49+08:00’,
‘_last_modifier’: ‘e@auth.local’,
‘_mtime’: ‘2024-04-10T13:49:53+08:00’,
‘_id’: ‘OYadXt_uTDK4h4ww4v-zCA’
}]
我们可以看到,在rows的array中,file的key又嵌套了一个array
提取出来的key-value pair 如下structure:
‘file’: [{
‘name’: ‘IMG_5157.jpg’,
‘size’: 72591,
‘type’: ‘file’,
‘upload_time’: ‘2024-04-10T05:50:07.168+00:00’,
‘url’: ‘https://cloud.seatable.cn/workspace/xxxxxx/asset/xxxx-6xxxx9ba4/files/2024-04/IMG_5157.jpg’
}],
其中[]belong to array的marker,{}belong to dictionary.
我们看到file的value中有一个 url,生成一了一个https的link
这个link是要怎么加进去的呢?
官方给出的以下代码:
正常在row 中插入代码
append_row
update_row
row_data = {'name': 'Tom', 'age': 18}
# 追加行
base.append_row('Table1', {'name': 'Tom', 'age': 18})
# 更新行
base.update_row('Table1', 'U_eTV7mDSmSd-K2P535Wzw', {'name': 'Tom', 'age': 18})
引入图片的代码
base.update_row(‘Table1’, row_id, {“file”: [info_dict]})
其中这个info_dict包含了URL
print(info_dict)
{
‘type’: ‘file’,
‘size’: 34857,
‘name’: ‘IMG_5420 (5).jpg’,
‘url’: ‘https://cloud.seatable.cn/workspace/333/asset/3-403/files/2024-04/IMG_5420%20%285%29.jpg’
}
在info_dict
引入一个函数base.upload_local_file
file_path = './IMG_5420.jpg'
info_dict=base.upload_local_file(file_path, name=None, file_type='file', replace=False)
print(info_dict)
再使用base.update_row('Table1', row_id, {"file": [info_dict]})
效果达成。