{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp core" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# fastkaggle.core\n", "\n", "> API details for fastkaggle." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|hide\n", "from nbprocess.showdoc import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "import os,json\n", "from fastcore.utils import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "iskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "def import_kaggle():\n", " \"Import kaggle API, using Kaggle secrets `kaggle_username` and `kaggle_key` if needed\"\n", " if iskaggle:\n", " from kaggle_secrets import UserSecretsClient\n", " sec = UserSecretsClient()\n", " os.environ['KAGGLE_USERNAME'] = sec.get_secret(\"kaggle_username\")\n", " if not os.environ['KAGGLE_USERNAME']: raise Exception(\"Please insert your Kaggle username and key into Kaggle secrets\")\n", " os.environ['KAGGLE_KEY'] = sec.get_secret(\"kaggle_key\")\n", " from kaggle import api\n", " return api" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(#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...]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api = import_kaggle()\n", "L(api.competitions_list())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "def setup_comp(competition, install=''):\n", " \"Get a path to data for `competition`, downloading it if needed\"\n", " if iskaggle:\n", " if install:\n", " os.system(f'pip install -Uqq {install}')\n", " return Path('../input')/competition\n", " else:\n", " path = Path(competition)\n", " from kaggle import api\n", " if not path.exists():\n", " import zipfile\n", " api.competition_download_cli(str(competition))\n", " zipfile.ZipFile(f'{competition}.zip').extractall(str(competition))\n", " return path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Path('titanic')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "setup_comp('titanic')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you pass a list of space separated modules to `install`, they'll be installed if running on Kaggle." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "def nb_meta(user, id, title, file, competition=None, private=True, gpu=False, internet=True):\n", " \"Get the `dict` required for a kernel-metadata.json file\"\n", " d = {\n", " \"id\": f\"{user}/{id}\",\n", " \"title\": title,\n", " \"code_file\": file,\n", " \"language\": \"python\",\n", " \"kernel_type\": \"notebook\",\n", " \"is_private\": private,\n", " \"enable_gpu\": gpu,\n", " \"enable_internet\": internet,\n", " \"keywords\": [],\n", " \"dataset_sources\": [],\n", " \"kernel_sources\": []\n", " }\n", " if competition: d[\"competition_sources\"] = [f\"competitions/{competition}\"]\n", " return d" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'id': 'jhoward/my-notebook',\n", " 'title': 'My notebook',\n", " 'code_file': 'my-notebook.ipynb',\n", " 'language': 'python',\n", " 'kernel_type': 'notebook',\n", " 'is_private': True,\n", " 'enable_gpu': False,\n", " 'enable_internet': True,\n", " 'keywords': [],\n", " 'dataset_sources': [],\n", " 'kernel_sources': [],\n", " 'competition_sources': ['competitions/paddy-disease-classification']}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nb_meta('jhoward', 'my-notebook', 'My notebook', 'my-notebook.ipynb', competition='paddy-disease-classification')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "def push_notebook(user, id, title, file, path='.', competition=None, private=True, gpu=False, internet=True):\n", " \"Push notebook `file` to Kaggle Notebooks\"\n", " meta = nb_meta(user, id, title, file=file, competition=competition, private=private, gpu=gpu, internet=internet)\n", " path = Path(path)\n", " nm = 'kernel-metadata.json'\n", " path.mkdir(exist_ok=True, parents=True)\n", " with open(path/nm, 'w') as f: json.dump(meta, f, indent=2)\n", " from kaggle import api\n", " api.kernels_push_cli(str(path))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "\n", "```python\n", "push_notebook('jhoward', 'first-steps-road-to-the-top-part-1',\n", " title='First Steps: Road to the Top, Part 1',\n", " file='first-steps-road-to-the-top-part-1.ipynb',\n", " competition='paddy-disease-classification',\n", " private=False, gpu=True)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|hide\n", "#|eval: false\n", "from nbprocess.doclinks import nbprocess_export\n", "nbprocess_export()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }