Skip to content
get_started.ipynb 3.89 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef929eed",
   "metadata": {},
   "source": [
    "# Getting started with Jupytext\n",
    "\n",
    "This small notebook shows you how to activate Jupytext in the JupyterLab\n",
    "environment. We'll show you a few things that you can do with Jupytext and\n",
    "a bit of what happens under the hood.\n",
    "\n",
    "**Note: to run this notebook locally, you need to first follow the Jupytext\n",
    "installation instructions and activate the JupyterLab plugin. If you're on\n",
    "Binder, it should already work.**\n",
    "\n",
    "## Enabling Jupytext in a new notebook\n",
    "\n",
    "This notebook is brand new - it hasn't had any special extra metadata added\n",
    "to it.\n",
    "\n",
    "If we want Jupytext to save files in multiple formats automatically,\n",
    "we can use the JupyterLab **command palette** to do so.\n",
    "\n",
    "* In the _View_ menu, click on _Activate Command Palette_\n",
    "* Then type **`Jupytext`**. You should see a number of commands come up. Each\n",
    "  one tells Jupytext to save the notebook in a different\n",
    "  file format automatically.\n",
    "* Select **Pair notebook with Markdown**\n",
    "\n",
    "That's it! If you have Jupytext installed, it will now save your notebook in\n",
    "markdown format automatically when you save this `.ipynb` file\n",
    "**in addition to** saving the `.ipynb` file itself.\n",
    "\n",
    "After you've done this, save the notebook. You should now see a new file called\n",
    "**`get_started.md`** in the same directory as this notebook.\n",
    "\n",
    "## How does Jupytext know to do this?\n",
    "\n",
    "Jupytext uses notebook-level metadata to keep track of what formats are paired\n",
    "with a notebook. Below we'll print the metadata of this notebook so you can see\n",
    "what the Jupytext metadata looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "350a250f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nbformat as nbf\n",
    "from IPython.display import JSON\n",
    "notebook = nbf.read('./get_started.ipynb', nbf.NO_CONVERT)\n",
    "JSON(notebook['metadata'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9615e72",
   "metadata": {},
   "source": [
    "As you select different formats from the command palette (following the instructions\n",
    "above) and save the notebook, you'll see this metadata change."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff7b741",
   "metadata": {},
   "source": [
    "## That's it!\n",
    "\n",
    "Play around with different kinds of code and outputs to see how each is\n",
    "converted into its corresponding text format. Here's a little Python code\n",
    "to get you started:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1868ebe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.scatter(*np.random.randn(2, 100), c=np.random.randn(100), s=np.random.rand(100)*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edb7fd45",
   "metadata": {},
   "source": [
    "# Experiment with the demo notebook!\n",
    "\n",
    "In the \"demo\" folder for `jupytext` there is a notebook called **`World population.ipynb`**.\n",
    "By default, saving the demo notebook will also create *many* possible Jupytext\n",
    "outputs so you can see what each looks like and which you prefer."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}