colabからkaggleのdatasetを作成する方法

必要事項

  • root/.kaggleにkaggle.jsonを配置する
import json
from kaggle.api.kaggle_api_extended import KaggleApi

ID = 'tnot_found'
DATASET_ID = '' # 任意
UPLOAD_DIR = '/content/drive/MyDrive/kaggle/HPA/output/nb19'
def dataset_create_new():
    dataset_metadata = {}
    dataset_metadata['id'] = f'{ID}/{DATASET_ID}'
    dataset_metadata['licenses'] = [{'name': 'CC0-1.0'}]
    dataset_metadata['title'] = DATASET_ID
    with open(OUTPUT_DIR + '/nb19/dataset-metadata.json', 'w') as f:
        json.dump(dataset_metadata, f, indent=4)
    api = KaggleApi()
    api.authenticate()
    api.dataset_create_new(folder=UPLOAD_DIR, convert_to_csv=False, dir_mode='tar')