Skip to content

2.0. Notebooks

What is a Python notebook?

A Python notebook, often referred as "notebook," is an interactive computing environment that allows users to combine executable code, rich text, visuals, and other multimedia resources in a single document. This tool is invaluable for data science, machine learning projects, documentation, and educational purposes, among others. Notebooks are structured in a cell-based format, where each cell can contain either code or text. When code cells are executed, the output is displayed directly beneath them, facilitating a seamless integration of code and content.

Where can you learn how to use notebooks?

Learning how to use notebooks is straightforward, thanks to a plethora of online resources. Beginners can start with the official documentation of popular notebook applications like Jupyter or Google Colab. YouTube channels dedicated to data science and Python programming also frequently cover notebooks, providing valuable tips and tutorials for both beginners and advanced users.

Why should you use a notebook for prototyping?

Notebooks offer an unparalleled environment for prototyping due to their unique blend of features:

  • Interactive Development: Notebooks allow for real-time code execution, offering immediate feedback on code functionality. This interactivity is especially beneficial when testing new ideas or debugging.
  • Exploratory Analysis: The ability to quickly iterate over different analytical approaches and visualize results makes notebooks an ideal tool for exploratory data analysis.
  • Productive Environment: The integrated environment of notebooks helps maintain focus by minimizing the need to switch between tools or windows. This consolidation of resources boosts productivity and streamlines the development process.

In addition, the narrative structure of notebooks supports a logical flow of ideas, facilitating the documentation of thought processes and methodologies. This makes it easier to share insights with peers or stakeholders and foster collaboration.

As an alternative to notebooks, consider using the Python Interactive Window in Visual Studio Code or other text editors. These environments combine the interactivity and productivity benefits of notebooks with the robustness and feature set of an integrated development environment (IDE), such as source control integration, advanced editing tools, and a wide range of extensions for additional functionality.

Can you use your notebook in production instead of creating a Python package?

Using notebooks in the early stages of development offers many advantages; however, they are not well-suited for production environments due to several limitations:

  • Lack of Integration: Notebooks often do not integrate seamlessly with tools commonly used in the Python software development ecosystem, such as testing frameworks (pytest), linting tools (ruff), and package managers (poetry).
  • Mixed Content: The intermingling of code, output, and narrative in a single document can complicate version control and maintenance, especially with complex projects.
  • Non-Sequential Flow: Notebooks do not enforce a linear execution order, which can lead to confusion and errors if cells are run out of sequence.
  • Lack of Reusability: The format of notebooks does not naturally encourage the development of reusable and modular code, such as functions, classes, or packages.

For these reasons, it is advisable to transition from notebooks to structured Python packages for production. Doing so enables better software development practices, such as unit testing, continuous integration, and deployment, thereby enhancing code quality and maintainability.

Do you need to review this chapter even if you know how to use notebooks?

Even seasoned users can benefit from reviewing this chapter. It introduces advanced techniques, new features, and tools that you may not know about. Furthermore, the chapter emphasizes structuring notebooks effectively and applying best practices to improve readability, collaboration, and overall efficiency.

Why do you need to properly organize your Python notebooks?

Organizing your Python notebooks is key for efficiently converting them into Python packages, which is essential for scaling AI/ML projects. A well-structured notebook enhances productivity by simplifying maintenance, understanding, and debugging of the code. Proper organization involves using Markdown headers to divide the notebook into clear, logical sections, which not only facilitates code reuse and adaptation but also improves collaboration by making the notebooks easier to navigate and understand for all team members.

Should you save notebook outputs in your git repository?

Saving notebook outputs in your Git repository is generally not recommended due to several reasons:

  • Version Control: Storing outputs in the repository can bloat the repository size, making it slower to clone and more cumbersome to manage.
  • Reproducibility: Including outputs can make it harder to reproduce the notebook's results, as the outputs may change over time or across different environments.
  • Confidentiality: Outputs may contain sensitive information, such as data values or model predictions, that should not be shared publicly.
  • Collaboration: Sharing outputs can lead to conflicts and confusion when multiple users work on the same notebook, as the outputs may not match the code execution.
  • Code Focus: The primary focus of version control systems like Git is on tracking changes to code, not data or outputs. Including outputs can distract from the main purpose of the repository.

Instead of saving outputs directly in the repository, consider using tools like Jupyter's nbconvert to export notebooks to different formats (e.g., HTML, PDF) that can be shared on other platforms or included in documentation. You can also use Jupyter's built-in cell tags to hide or exclude specific cells from the exported version, allowing you to control what information is shared while keeping the repository clean and focused on code.

Notebook additional resources