#| default_exp core #|hide from nbprocess.showdoc import * #|export import os,json from fastcore.utils import * #|export iskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '') #|export def import_kaggle(): "Import kaggle API, using Kaggle secrets `kaggle_username` and `kaggle_key` if needed" if iskaggle: from kaggle_secrets import UserSecretsClient sec = UserSecretsClient() os.environ['KAGGLE_USERNAME'] = sec.get_secret("kaggle_username") if not os.environ['KAGGLE_USERNAME']: raise Exception("Please insert your Kaggle username and key into Kaggle secrets") os.environ['KAGGLE_KEY'] = sec.get_secret("kaggle_key") from kaggle import api return api api = import_kaggle() L(api.competitions_list()) #|export def setup_comp(competition, install=''): "Get a path to data for `competition`, downloading it if needed" if iskaggle: if install: os.system(f'pip install -Uqq {install}') return Path('../input')/competition else: path = Path(competition) from kaggle import api if not path.exists(): import zipfile api.competition_download_cli(str(competition)) zipfile.ZipFile(f'{competition}.zip').extractall(str(competition)) return path setup_comp('titanic') #|export def nb_meta(user, id, title, file, competition=None, private=True, gpu=False, internet=True): "Get the `dict` required for a kernel-metadata.json file" d = { "id": f"{user}/{id}", "title": title, "code_file": file, "language": "python", "kernel_type": "notebook", "is_private": private, "enable_gpu": gpu, "enable_internet": internet, "keywords": [], "dataset_sources": [], "kernel_sources": [] } if competition: d["competition_sources"] = [f"competitions/{competition}"] return d nb_meta('jhoward', 'my-notebook', 'My notebook', 'my-notebook.ipynb', competition='paddy-disease-classification') #|export def push_notebook(user, id, title, file, path='.', competition=None, private=True, gpu=False, internet=True): "Push notebook `file` to Kaggle Notebooks" meta = nb_meta(user, id, title, file=file, competition=competition, private=private, gpu=gpu, internet=internet) path = Path(path) nm = 'kernel-metadata.json' path.mkdir(exist_ok=True, parents=True) with open(path/nm, 'w') as f: json.dump(meta, f, indent=2) from kaggle import api api.kernels_push_cli(str(path)) #|hide #|eval: false from nbprocess.doclinks import nbprocess_export nbprocess_export()