{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter Notebook\n",
    "\n",
    "## Manipulate Notebooks from the Command Line\n",
    "\n",
    "See [StackOverflow](https://stackoverflow.com/questions/35545402/how-to-run-an-ipynb-jupyter-notebook-from-terminal).\n",
    "\n",
    "- Execute notebook and save results\n",
    "\n",
    "    ```bash\n",
    "    jupyter nbconvert --execute --to notebook --inplace notebook.ipynb\n",
    "    ```\n",
    "\n",
    "- Export notebook to `.py`\n",
    "    \n",
    "    ```bash\n",
    "    jupyter nbconvert --to python notebook.ipynb\n",
    "    ```\n",
    "\n",
    "- Export notebook to `.html`\n",
    "\n",
    "    (Convert without execution)  \n",
    "    ```bash\n",
    "    jupyter nbconvert --to html notebook.ipynb\n",
    "    ```\n",
    "    \n",
    "    (Execute before conversion, with no execution time limit)  \n",
    "    ```bash\n",
    "    jupyter nbconvert --execute --to html --ExecutePreprocessor.timeout=-1 notebook.ipynb\n",
    "    ```\n",
    "    \n",
    "    (Execute before conversion and set output dir to `./html`)  \n",
    "    ```bash\n",
    "    jupyter nbconvert --execute --to html --ExecutePreprocessor.timeout=-1 --output-dir='./html' notebook.ipynb\n",
    "    ```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Programmatically Create Jupyter Notebooks (with different parameters)\n",
    "\n",
    "Specify parameters in the first cell of the source jupyter notebook (`source.ipynb`):\n",
    "\n",
    "![](img/param-in-notebook.png)\n",
    "\n",
    "You can then use [nbparameterise](https://github.com/takluyver/nbparameterise) to programmatically replace input values in a notebook before running it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{admonition} [set-notebook-params.py](https://github.com/liao961120/pynote/blob/master/scripts/set-notebook-params.py)\n",
    ":class: dropdown\n",
    "\n",
    "````python\n",
    "import os\n",
    "import nbformat\n",
    "from nbparameterise import extract_parameters, parameter_values, replace_definitions\n",
    "\n",
    "\n",
    "# Extract parameters in source notebook\n",
    "with open(\"source.ipynb\") as f:\n",
    "\tnb = nbformat.read(f, as_version=4)\n",
    "orig_parameters = extract_parameters(nb)\n",
    "\n",
    "\n",
    "# Parameters to replace\n",
    "model_names = [\n",
    "\t'model3-small-search-space4', 'model3-small-search-space5'\n",
    "]\n",
    "\n",
    "# Create new notebooks with replaced parameters\n",
    "for name in model_names:\n",
    "    print(\"Running for model:\", name)\n",
    "\n",
    "    # Update the parameters\n",
    "    params = parameter_values(orig_parameters, model_name=name)\n",
    "    new_nb = replace_definitions(nb, params, execute=False)\n",
    "\n",
    "    # Save\n",
    "    new_nb_fp = f\"new_notebook_with_param_{name}.ipynb\"\n",
    "    with open(new_nb_fp, 'w') as f:\n",
    "        nbformat.write(new_nb, f)\n",
    "    \n",
    "    # Execute new notebook\n",
    "    os.system(f\"jupyter nbconvert --execute --ExecutePreprocessor.timeout=-1 --to notebook --inplace {new_nb_fp}\")\n",
    "    # Convert to html\n",
    "    os.system(f\"jupyter nbconvert --output-dir='./html' --to html {new_nb_fp}\")\n",
    "````\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3.7",
   "language": "python",
   "name": "py3.7"
  },
  "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}