Skip to article frontmatterSkip to article content

Notebook Development Best Practices


Provide a Conda Environment Capable of Running the Notebooks


Also, Provide a Pinned environment.yml So Users Can Run the Notebook in the Expected Software Environment and Reduce the Chance of Dependency Conflicts

The open-source Python packages used in your Conda environments are routinely updated, often introducing breaking dependencies. If the environment.yml file does not pin packages to specific versions, the default behavior is to build the latest available versions. This can introduce new dependency conflicts at any time.

Even if you pin every package, there is still a risk that an environment.yml will no longer build correctly at some point. This can occur if one of its package versions is removed due to a security risk, legal issue, or the discovery of a critical error, though this is relatively rare.

Provide both a pinned and an unpinned environment.yml. An unpinned or minimally pinned config is helpful if you need to make an environment update. A pinned environment.yml is very long, and the pinned packages will interfere with the Conda Package Manager’s ability to find the latest complementary dependencies. It is much easier to start developing an environment update from a shorter unpinned config.

To reduce the chances of your environment.yml breaking over time, provide a pinned version to your users:

  1. Open a terminal.
  2. Create the conda environment if it does not yet exist.
  3. Run the command mamba env export -n your_environement_name > environement.yml
  4. Open the newly created environment.yml in a text editor.
  5. Remove the prefix line at the bottom of the file
    • prefix is the path to the environment on your system, which will likely differ between users.
  6. Save the file.
  7. Push the file to your notebook repository.

Set the Notebook Metadata to Use the Correct Environment

  1. Open your notebook, change into your conda environment’s kernel, and save the notebook.
  2. Push the update to your notebook repository.
  3. When users clone your repository, the notebooks will automatically use the correct kernel (as long as the required environment has been created).

Consider Clearing Your Notebook Output Before Saving It, Or Not

Saving a notebook with its output increases the file size and slows down the time it takes to open, save, and download. This can cause difficulty for users accessing a JupyterHub over a weak internet connection.

However, saving a notebook with its output in place has benefits. It allows you to display results without running the notebook. They are also visible on GitHub.

If you wish to clear the notebook output:

  1. Open the notebook.
  2. Select the menu option: Kernel -> Restart Kernel and Clear...
  3. Save the notebook.
  4. Push the cleared notebook to its repository.

Test Notebooks Ahead of Time.


Plan for Students with Poor Internet Access

To avoid these issues, consider allowing students to turn in work with one of the following options:

  1. Print the notebook as a PDF by selecting Print... from the File menu.
  2. Screenshot relevant portions of the notebook and paste them into a word processor.

Avoid Changing Directories in a Notebook

Why?

How do I avoid changing directories in my notebook?

Provide a Path to Seek Help and Report Issues

Define Terms of Service