{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Content with notebooks\n",
    "\n",
    "You can also create content with Jupyter Notebooks. The content for the current page is contained\n",
    "in a Jupyter Notebook in the `notebooks/` folder of the repository. This means that we can include\n",
    "code blocks and their outputs, and export them to Jekyll markdown.\n",
    "\n",
    "**You can find the original notebook for this page [at this address](https://github.com/jupyter/jupyter-book/blob/master/jupyter_book/book_template/content/features/notebooks.ipynb)**\n",
    "\n",
    "## Markdown + notebooks\n",
    "\n",
    "As it is markdown, you can embed images, HTML, etc into your posts!\n",
    "\n",
    "![](images/cool.jpg)\n",
    "\n",
    "You an also $add_{math}$ and\n",
    "\n",
    "$$\n",
    "math^{blocks}\n",
    "$$\n",
    "\n",
    "or\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\mbox{mean} la_{tex} \\\\ \\\\\n",
    "math blocks\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "But make sure you \\$Escape \\$your \\$dollar signs \\$you want to keep!\n",
    "\n",
    "## Code blocks and image outputs\n",
    "\n",
    "Jupyter Book will also embed your code blocks and output in your book.\n",
    "For example, here's some sample Matplotlib code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "hide_cell",
     "thebe-init"
    ]
   },
   "outputs": [],
   "source": [
    "from matplotlib import rcParams, cycler\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "plt.ion()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 151540 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "filenames": {
       "image/png": "/home/aakash/Documents/Quantecon-worl/quantecon-book-theme/docs/jupyter_execute/notebooks_2_0.png"
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Fixing random state for reproducibility\n",
    "np.random.seed(19680801)\n",
    "\n",
    "N = 10\n",
    "data = [np.logspace(0, 1, 100) + np.random.randn(100) + ii for ii in range(N)]\n",
    "data = np.array(data).T\n",
    "cmap = plt.cm.coolwarm\n",
    "rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))\n",
    "\n",
    "\n",
    "from matplotlib.lines import Line2D\n",
    "custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),\n",
    "                Line2D([0], [0], color=cmap(.5), lw=4),\n",
    "                Line2D([0], [0], color=cmap(1.), lw=4)]\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10, 5))\n",
    "lines = ax.plot(data)\n",
    "ax.legend(custom_lines, ['Cold', 'Medium', 'Hot']);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the image above is captured and displayed by Jekyll."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 78436 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "filenames": {
       "image/png": "/home/aakash/Documents/Quantecon-worl/quantecon-book-theme/docs/jupyter_execute/notebooks_4_0.png"
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Fixing random state for reproducibility\n",
    "np.random.seed(19680801)\n",
    "\n",
    "N = 10\n",
    "data = [np.logspace(0, 1, 100) + .1*np.random.randn(100) + ii for ii in range(N)]\n",
    "data = np.array(data).T\n",
    "cmap = plt.cm.coolwarm\n",
    "rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))\n",
    "\n",
    "\n",
    "from matplotlib.lines import Line2D\n",
    "custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),\n",
    "                Line2D([0], [0], color=cmap(.5), lw=4),\n",
    "                Line2D([0], [0], color=cmap(1.), lw=4)]\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10, 5))\n",
    "lines = ax.plot(data)\n",
    "ax.legend(custom_lines, ['Cold', 'Medium', 'Hot'])\n",
    "ax.set(title=\"Smoother lines\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{margin} You can also pop out content to the margin\n",
    "For more information on how to do this,\n",
    "check out {doc}`layout`.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": [
     "margin"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this works for code cells too, if you add a `margin` tag to them\n"
     ]
    }
   ],
   "source": [
    "print(\"this works for code cells too, if you add a `margin` tag to them\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sum_{i=0}^n i^2 = \f",
       "rac{(n^2+n)(2n+1)}{6}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You can also include enriched outputs like Math\n",
    "from IPython.display import Math\n",
    "Math(\"\\sum_{i=0}^n i^2 = \\frac{(n^2+n)(2n+1)}{6}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Removing content before publishing\n",
    "\n",
    "You can also remove some content before publishing your book to the web. For example,\n",
    "in [the original notebook](https://github.com/jupyter/jupyter-book/blob/master/jupyter_book/book_template/content/features/notebooks.ipynb) there\n",
    "used to be a cell below..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": [
     "remove_cell"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30576 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "filenames": {
       "image/png": "/home/aakash/Documents/Quantecon-worl/quantecon-book-theme/docs/jupyter_execute/notebooks_9_0.png"
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "thisvariable = \"none of this should show up in the textbook\"\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "x = np.random.randn(100)\n",
    "y = np.random.randn(100)\n",
    "ax.scatter(x, y, s=np.abs(x*100), c=x, cmap=plt.cm.coolwarm)\n",
    "ax.text(0, .5, thisvariable, fontsize=20, transform=ax.transAxes)\n",
    "ax.set_axis_off()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also **remove only the code** so that images and other output still show up.\n",
    "\n",
    "Below we'll *only* display an image. It was generated with Python code in a cell,\n",
    "which you can [see in the original notebook](https://github.com/jupyter/jupyter-book/blob/master/jupyter_book/book_template/content/features/notebooks.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": [
     "hide_input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29496 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "filenames": {
       "image/png": "/home/aakash/Documents/Quantecon-worl/quantecon-book-theme/docs/jupyter_execute/notebooks_11_0.png"
      },
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "thisvariable = \"this plot *will* show up in the textbook.\"\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "x = np.random.randn(100)\n",
    "y = np.random.randn(100)\n",
    "ax.scatter(x, y, s=np.abs(x*100), c=x, cmap=plt.cm.coolwarm)\n",
    "ax.text(0, .5, thisvariable, fontsize=20, transform=ax.transAxes)\n",
    "ax.set_axis_off()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here we'll *only* display a Pandas DataFrame. Again, this was generated with Python code\n",
    "from [this original notebook](https://github.com/jupyter/textbooks-with-jupyter/blob/master/notebooks/introduction/notebooks.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": [
     "hide_input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Word A</th>\n",
       "      <th>Word B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>hi</td>\n",
       "      <td>there</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>this</td>\n",
       "      <td>is</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>DataFrame</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Word A     Word B\n",
       "0     hi      there\n",
       "1   this         is\n",
       "2      a  DataFrame"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.DataFrame([['hi', 'there'], ['this', 'is'], ['a', 'DataFrame']], columns=['Word A', 'Word B'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "popout"
    ]
   },
   "source": [
    "Testing popouts before headers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interactive outputs\n",
    "\n",
    "We can even do the same for *interactive* material. Below we'll display a map using [folium](https://python-visualization.github.io/folium/). When the notebook\n",
    "is converted to Markdown, the code for creating the interactive map is retained.\n",
    "\n",
    "**Note that this will only work for some packages.** They need to be able to output standalone HTML/Javascript, and not\n",
    "depend on an underlying Python kernel to work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import folium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe src=\"about:blank\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" [encoded data, 8502 characters] onload=\"this.contentDocument.open();this.contentDocument.write(atob(this.getAttribute('data-html')));this.contentDocument.close();\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
      ],
      "text/plain": [
       "<folium.folium.Map at 0x7f48d7ecbd10>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = folium.Map(\n",
    "    location=[45.372, -121.6972],\n",
    "    zoom_start=12,\n",
    "    tiles='Stamen Terrain'\n",
    ")\n",
    "\n",
    "folium.Marker(\n",
    "    location=[45.3288, -121.6625],\n",
    "    popup='Mt. Hood Meadows',\n",
    "    icon=folium.Icon(icon='cloud')\n",
    ").add_to(m)\n",
    "\n",
    "folium.Marker(\n",
    "    location=[45.3311, -121.7113],\n",
    "    popup='Timberline Lodge',\n",
    "    icon=folium.Icon(color='green')\n",
    ").add_to(m)\n",
    "\n",
    "folium.Marker(\n",
    "    location=[45.3300, -121.6823],\n",
    "    popup='Some Other Location',\n",
    "    icon=folium.Icon(color='red', icon='info-sign')\n",
    ").add_to(m)\n",
    "\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rich outputs from notebook cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because notebooks have rich text outputs, you can store these in\n",
    "your Jupyter Book as well!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]\r\n",
      "               [--paths] [--json]\r\n",
      "               [subcommand]\r\n",
      "\r\n",
      "Jupyter: Interactive Computing\r\n",
      "\r\n",
      "positional arguments:\r\n",
      "  subcommand     the subcommand to launch\r\n",
      "\r\n",
      "optional arguments:\r\n",
      "  -h, --help     show this help message and exit\r\n",
      "  --version      show the jupyter command's version and exit\r\n",
      "  --config-dir   show Jupyter config dir\r\n",
      "  --data-dir     show Jupyter data dir\r\n",
      "  --runtime-dir  show Jupyter runtime dir\r\n",
      "  --paths        show all Jupyter paths. Add --json for machine-readable\r\n",
      "                 format.\r\n",
      "  --json         output paths as machine-readable json\r\n",
      "\r\n",
      "Available subcommands: book bundlerextension kernel kernelspec migrate\r\n",
      "nbconvert nbextension notebook run serverextension troubleshoot trust\r\n"
     ]
    }
   ],
   "source": [
    "# The ! causes this to run as a shell command\n",
    "!jupyter -h"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,md:myst",
   "text_representation": {
    "extension": ".md",
    "format_name": "myst",
    "format_version": "0.8",
    "jupytext_version": "1.4.2"
   }
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.7"
  },
  "source_map": [
   13,
   51,
   60,
   79,
   83,
   105,
   112,
   117,
   121,
   129,
   140,
   147,
   158,
   163,
   170,
   174,
   184,
   188,
   215,
   219,
   224
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
