{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Launch buttons for interactivity\n", "\n", "Because Jupyter Books are built with Jupyter notebooks, you can allow users to launch\n", "live Jupyter sessions in the cloud directly from your book. This lets readers quickly interact with your content in a traditional coding interface.\n", "There now exist numerous online notebook services - a good comparison is provided [in this article](https://www.dataschool.io/cloud-services-for-jupyter-notebook) - and the following sections describes the available integrations provided by Jupyter Book.\n", "\n", "In each case, you'll need to tell Jupyter Book where your book content lives online.\n", "To do so, use this configuration in `_config.yml`:\n", "\n", "```yaml\n", "# Information about where the book exists on the web\n", "repository:\n", " url : https://github.com/yourusername/yourbookrepo # Online location of your book\n", " path_to_book : path/to/book # Optional path to your book, relative to the repository root\n", " branch : master # Which branch of the repository should be used when creating links (optional)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The notebook interface\n", "\n", "Binder and JupyterHub sessions can be opened using either the \"classic\" Jupyter Notebook or the new JupyterLab interface backend (see [jupyter.org](https://jupyter.org) for more details).\n", "This is configured using:\n", "\n", "```yaml\n", "launch_buttons:\n", " notebook_interface: \"jupyterlab\" # or \"classic\"\n", "```\n", "\n", "One thing to take into account when choosing the interface is that notebooks written in the [MyST Markdown](../file-types/myst-notebooks.md) text-based format will not be opened as notebooks out-of-the-box.\n", "If you wish for these files to be opened as notebooks then firstly you must ensure that [`jupytext>=0.16`](https://jupytext.readthedocs.io/en/latest/formats.html#myst-markdown) is installed in the Binder/JupyterHub environment for your book (no support for this feature exists in Google Colab). You then have two options:\n", "\n", "- Use the \"classic\" interface, which will then immediately open these files as notebooks.\n", "- The \"jupyterlab\" interface (at the time of writing) has not yet implemented this behaviour, and so you will need to instruct readers to right-click the Markdown file and click \"Open in notebook editor\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## {term}`Binder` buttons for your pages\n", "\n", "{term}`BinderHub` can be used to build the environment needed to run a repository, and provides\n", "a link that lets others interact with that repository. If your Jupyter Book is hosted online\n", "on GitHub, you can automatically insert buttons that link to the Jupyter Notebook running in a BinderHub.\n", "When a user clicks the button, they'll be taken to a live version of the page. If your code\n", "doesn't require a significant amount of CPU or RAM, you can use the free, public BinderHub running\n", "at https://mybinder.org.\n", "\n", "To automatically include Binder link buttons in each page of your Jupyter Book, use the following\n", "configuration in `_config.yml`:\n", "\n", "```yaml\n", "launch_buttons:\n", " binderhub_url: \"https://mybinder.org\" # The URL for your BinderHub (e.g., https://mybinder.org)\n", "```\n", "\n", "By adding this configuration, along with the repository url configuration above, Jupyter Book\n", "will insert Binder links to any pages that were built from notebook content." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JupyterHub buttons for your pages\n", "\n", "JupyterHub lets you host an online service that gives users their own Jupyter servers\n", "with an environment that you specify for them. It allows you to give users access to\n", "resources and hardware that you provision in the cloud, and allows you to authenticate users\n", "in order to control who has access to your hardware.\n", "\n", "Similar to Binder link buttons, you can also automatically include interact links that send\n", "your readers to a JupyterHub that is running a live, interactive version of your page. This\n", "is accomplished using the [nbgitpuller](https://github.com/jupyterhub/nbgitpuller) server\n", "extension.\n", "\n", "You can configure the location of the JupyterHub (which you may set up on your own using a guide\n", "such as [zero to jupyterhub for kubernetes](https://z2jh.jupyter.org) or [the littlest jupyterhub](https://tljh.jupyter.org)) with the following configuration:\n", "\n", "```yaml\n", "launch_buttons:\n", " jupyterhub_url: \"your-hub-url\" # The URL for your JupyterHub. (e.g., https://datahub.berkeley.edu)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## {term}`Google Colab` buttons for your pages\n", "\n", "If your Jupyter Book is hosted online on GitHub, you can automatically insert buttons that link to the Jupyter Notebook running on [Google Colab](https://colab.research.google.com/). When a user clicks the button, they'll be taken to a live version of the page.\n", "\n", "Similar to Binder link buttons, you can automatically include Google Colab link buttons with the following configuration in `_config.yml`:\n", "\n", "```yaml\n", "launch_buttons:\n", " colab_url: \"https://colab.research.google.com\"\n", "```\n", "\n", "```{note}\n", "Google Colab links will only work for pages that have the `.ipynb` extension.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(launch/thebelab)=\n", "## Live interactive pages with ThebeLab\n", "\n", "This section describes how to bring interactivity to your book. This lets users\n", "run code and see outputs *without leaving the page*. Interactivity is provided\n", "by a kernel running on the public [**MyBinder**](https://mybinder.org) service.\n", "\n", "```{warning}\n", "This is an experimental feature, and may change in the future or work unexpectedly.\n", "```\n", "\n", "To make your content interactive without requiring readers to leave the current page,\n", "you can use a project called [Thebe](https://github.com/minrk/thebelab). \n", "This provides you with a button that, when clicked, will convert each code cell into\n", "an **interactive** cell that can be edited. It also adds a \"run\" button to each cell,\n", "and connects to a Binder kernel running in the cloud.\n", "As an alternative to pressing the Thebe button at the top of the page, you \n", "can press the \"\" symbol in the top right corner of each code cell to start the \n", "interactive mode.\n", "\n", "To add a Thebe button to your Jupyter Book pages, use the following configuration:\n", "\n", "```yaml\n", "launch_buttons:\n", " thebe : true\n", "```\n", "\n", "In addition, you can configure the Binder settings that are used to provide a kernel for\n", "Thebe to run the code. These use the same configuration fields as the BinderHub interact\n", "buttons described above.\n", "\n", "For an example, click the **Thebe** button above on this page, and run the code below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.ion()\n", "\n", "x = np.arange(500)\n", "y = np.random.randn(500)\n", "\n", "fig, ax = plt.subplots()\n", "ax.scatter(x, y, c=y, s=x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running cells in Thebe when it is initialized\n", "\n", "Sometimes you'd like to initialize the kernel that Thebe uses by running\n", "some code ahead of time. This might be code that you then hide from the user\n", "in order to narrow the focus of what they interact with. This is possible\n", "by using Jupyter Notebook tags.\n", "\n", "Adding the tag `thebe-init` to any code cell will cause Thebe to\n", "run this cell after it has received a kernel. Any subsequent Thebe cells\n", "will have access to the same environment (e.g. any module imports made in the\n", "initialization cell).\n", "\n", "You can then pair this with something like `hide-input` in order to run\n", "initialization code that your user doesn't immediately see. For example,\n", "below we'll initialize a variable in a hidden cell, and then tell another\n", "cell to print the output of that variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide-input", "thebe-init" ] }, "outputs": [], "source": [ "my_hidden_variable = 'wow, it worked!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The variable for this is defined in the cell above!\n", "print(my_hidden_variable)" ] } ], "metadata": { "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.6" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }