Skip to content

Commit

Permalink
replace login method (#467)
Browse files Browse the repository at this point in the history
* use !login method

* hide nb script
  • Loading branch information
tcapelle authored Sep 21, 2023
1 parent 1c3e1b6 commit 48b1044
Show file tree
Hide file tree
Showing 113 changed files with 2,331 additions and 1,694 deletions.
315 changes: 315 additions & 0 deletions .github/nb_scripts/fix_login.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"import nbformat\n",
"from pathlib import Path\n",
"from nb_helpers.utils import find_nbs"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"nbs_paths = find_nbs(Path.cwd())"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Path('/Users/tcapelle/work/examples/colabs/boosting/Credit_Scorecards_with_XGBoost_and_W&B.ipynb')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nb_path = nbs_paths[3]\n",
"nb_path"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
" for i, cell in enumerate(nb[\"cells\"]):\n",
" if cell[\"cell_type\"] == \"code\":\n",
" if delete_line:\n",
" cell_content = cell[\"source\"].split(\"\\n\")\n",
" cell[\"source\"] = \"\\n\".join(cell_content) \n",
" return i"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!wandb login"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"nb = nbformat.read(nb_path, 4)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"cell = nb[\"cells\"][1]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"nbformat.notebooknode.NotebookNode"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(cell)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'attachments': {},\n",
" 'cell_type': 'markdown',\n",
" 'idx_': 1,\n",
" 'metadata': {},\n",
" 'source': '<img src=\"http://wandb.me/logo-im-png\" width=\"400\" alt=\"Weights & Biases\" /><br>\\n\\n<!--- @wandbcode{xgboost_vehicle_loan_default} -->\\n\\n<img src=\"http://wandb.me/mini-diagram\" width=\"600\" alt=\"Weights & Biases\" />'}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cell"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"login_idx = idx_login_cell(nb)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"def insert_login_cell(nb, idx, code=\"!wandb login\"):\n",
" n_cells = len(nb[\"cells\"])\n",
" login_cell = nbformat.v4.new_code_cell(source=code)\n",
" nb[\"cells\"].insert(idx+1, login_cell)\n",
" return nb"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"nb = insert_login_cell(nb, login_idx)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'cell_type': 'code',\n",
" 'execution_count': None,\n",
" 'idx_': 19,\n",
" 'metadata': {},\n",
" 'source': ''}"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nb[\"cells\"][login_idx] "
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': '98de0a69',\n",
" 'cell_type': 'code',\n",
" 'metadata': {},\n",
" 'execution_count': None,\n",
" 'source': '!wandb login',\n",
" 'outputs': []}"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nb[\"cells\"][login_idx+1] "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'attachments': {},\n",
" 'cell_type': 'markdown',\n",
" 'idx_': 21,\n",
" 'metadata': {},\n",
" 'source': '## Vehicle Loan Dataset\\n\\nWe will be using a simplified version of the [Vehicle Loan Default Prediction dataset](https://www.kaggle.com/sneharshinde/ltfs-av-data) from L&T which has been stored in W&B Artifacts. '}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nb[\"cells\"][login_idx+2] "
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"def remove_properties(nb, props=[\"id\", \"idx_\", \"path_\"]):\n",
" for cell in nb[\"cells\"]:\n",
" for prop in props:\n",
" cell.pop(prop, None)\n",
" return nb\n",
"\n",
"def remove_empty_cells(nb):\n",
" nb[\"cells\"] = [cell for cell in nb[\"cells\"] if cell[\"source\"] != \"\"]\n",
" return nb"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"nb = remove_properties(nb)\n",
"nb = remove_empty_cells(nb)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "idx_",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/tcapelle/work/examples/fix_login.ipynb Cell 17\u001b[0m line \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m login_idx \u001b[39m=\u001b[39m idx_login_cell(nb)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39mif\u001b[39;00m login_idx \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m nb \u001b[39m=\u001b[39m insert_login_cell(nb, login_idx)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39m# delete path_ attribute\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m nb\u001b[39m.\u001b[39mpop(\u001b[39m\"\u001b[39m\u001b[39mpath_\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39mNone\u001b[39;00m)\n",
"\u001b[1;32m/Users/tcapelle/work/examples/fix_login.ipynb Cell 17\u001b[0m line \u001b[0;36minsert_login_cell\u001b[0;34m(nb, idx, code)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m# update indexes\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39mfor\u001b[39;00m cell \u001b[39min\u001b[39;00m nb[\u001b[39m\"\u001b[39m\u001b[39mcells\u001b[39m\u001b[39m\"\u001b[39m][idx\u001b[39m+\u001b[39m\u001b[39m2\u001b[39m:]:\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m cell\u001b[39m.\u001b[39midx_ \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/tcapelle/work/examples/fix_login.ipynb#X20sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m \u001b[39mreturn\u001b[39;00m nb\n",
"File \u001b[0;32m~/miniforge3/envs/nbdev2/lib/python3.10/site-packages/nbformat/_struct.py:125\u001b[0m, in \u001b[0;36mStruct.__getattr__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 123\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m[key]\n\u001b[1;32m 124\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mKeyError\u001b[39;00m:\n\u001b[0;32m--> 125\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mAttributeError\u001b[39;00m(key) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[1;32m 126\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 127\u001b[0m \u001b[39mreturn\u001b[39;00m result\n",
"\u001b[0;31mAttributeError\u001b[0m: idx_"
]
}
],
"source": [
"for nb_path in nbs_paths:\n",
" nb = nbformat.read(nb_path, 4)\n",
" login_idx = idx_login_cell(nb)\n",
" if login_idx is not None:\n",
" nb = insert_login_cell(nb, login_idx)\n",
" # delete path_ attribute\n",
" nb.pop(\"path_\", None)\n",
" nb = remove_properties(nb)\n",
" nb = remove_empty_cells(nb)\n",
" nbformat.validate(nb)\n",
" nbformat.write(nb, nb_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "nbdev2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 48b1044

Please sign in to comment.