{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "This notebook is part of the `nbsphinx` documentation: https://nbsphinx.readthedocs.io/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Configuring the Kernels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kernel Name\n", "\n", "If we have multiple kernels installed, we can choose to override the kernel saved in the notebook using [nbsphinx_kernel_name](usage.ipynb#nbsphinx_kernel_name):\n", "```python\n", "nbsphinx_kernel_name = 'python-upstream-dev'\n", "```\n", "which uses the kernel named `python-upstream-dev` instead of the kernel name stored in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kernel Arguments\n", "\n", "We can also pass options to the kernel by setting [nbsphinx_execute_arguments](usage.ipynb#nbsphinx_execute_arguments) in `conf.py`. These work the same way as `ipython_kernel_config.py`. For example, using\n", "```python\n", "nbsphinx_execute_arguments = [\n", " \"--InlineBackend.rc=figure.dpi=96\",\n", "]\n", "```\n", "to set [plot options](code-cells.ipynb#Plots) is the same as writing:\n", "```python\n", "c.InlineBackend.rc = {'figure.dpi': 96}\n", "```\n", "in `ipython_kernel_config.py` or using:\n", "```ipython\n", "%config InlineBackend.rc={'figure.dpi': 96}\n", "```\n", "at the top of a notebook:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_ipython().config.InlineBackend.rc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Environment Variables\n", "\n", "The contents of `os.environ` after the execution of `conf.py` will be passed as environment variables to the kernel. As an example, `MY_DUMMY_VARIABLE` has been set in [conf.py](conf.py) like this:\n", "\n", "```python\n", "import os\n", "os.environ['MY_DUMMY_VARIABLE'] = 'Hello from conf.py!'\n", "```\n", "\n", "... and it can be checked in the notebook like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['MY_DUMMY_VARIABLE']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is useful if we want to edit `PYTHONPATH` in order to compile the documentation without installing the project:\n", "```python\n", "import os\n", "\n", "src = os.path.abspath('../src')\n", "os.environ['PYTHONPATH'] = src\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are using https://mybinder.org/ and you want to define environment variables,\n", "you should create a file `.binder/start` in your repository\n", "(see [Binder docs](https://mybinder.readthedocs.io/en/latest/using/config_files.html#start-run-code-before-the-user-sessions-starts))\n", "containing definitions like this:\n", "\n", "```bash\n", "#!/bin/bash\n", "export MY_DUMMY_VARIABLE=\"Hello from .binder/start!\"\n", "exec \"$@\"\n", "```" ] } ], "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.6rc1" } }, "nbformat": 4, "nbformat_minor": 2 }