#| default_exp core
API details for fastkaggle.
#|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())
(#20) [contradictory-my-dear-watson,gan-getting-started,store-sales-time-series-forecasting,tpu-getting-started,digit-recognizer,titanic,house-prices-advanced-regression-techniques,connectx,nlp-getting-started,spaceship-titanic...]
#|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')
Path('titanic')
If you pass a list of space separated modules to install, they'll be installed if running on Kaggle.
#|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')
{'id': 'jhoward/my-notebook',
'title': 'My notebook',
'code_file': 'my-notebook.ipynb',
'language': 'python',
'kernel_type': 'notebook',
'is_private': True,
'enable_gpu': False,
'enable_internet': True,
'keywords': [],
'dataset_sources': [],
'kernel_sources': [],
'competition_sources': ['competitions/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))
Note that Kaggle recommends that the id match the slug for the title -- i.e it should be the same as the title, but lowercase, no punctuation, and spaces replaced with dashes. E.g:
push_notebook('jhoward', 'first-steps-road-to-the-top-part-1',
title='First Steps: Road to the Top, Part 1',
file='first-steps-road-to-the-top-part-1.ipynb',
competition='paddy-disease-classification',
private=False, gpu=True)
#|hide
#|eval: false
from nbprocess.doclinks import nbprocess_export
nbprocess_export()