Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Nature of Code - py5 Edition

A py5 (Python) port of The Nature of Code by Daniel Shiffman.

About This Project

This is a derivative work of The Nature of Code by Daniel Shiffman, published by No Starch Press® Inc. The original book uses p5.js (JavaScript), and this repository contains a port to py5 (Python/Processing).

Original Work

  • Title: The Nature of Code
  • Author: Daniel Shiffman
  • Publisher: No Starch Press® Inc.
  • Website: https://natureofcode.com
  • License: CC BY-NC-SA 4.0

Changes Made

This port converts all code examples from p5.js (JavaScript) to py5 (Python), adapting explanations where necessary to reflect Python-specific concepts and syntax. Where the original relies on a browser-specific library, an idiomatic Python equivalent is used: Matter.js / Toxiclibs.js → pymunk (Chapter 6) and ml5.js → scikit-learn / hand-rolled NumPy networks (Chapters 10–11).

Chapters

All twelve chapters are available as Jupyter notebooks in notebooks/:

Notebook Chapter
00_introduction.ipynb Front matter — Introduction, Dedication & Acknowledgments
00_randomness.ipynb Chapter 0 — Randomness & Perlin Noise
01_vectors.ipynb 1. Vectors
02_forces.ipynb 2. Forces
03_oscillation.ipynb 3. Oscillation
04_particles.ipynb 4. Particle Systems
05_steering.ipynb 5. Autonomous Agents
06_libraries.ipynb 6. Physics Libraries (pymunk)
07_ca.ipynb 7. Cellular Automata
08_fractals.ipynb 8. Fractals
09_ga.ipynb 9. Genetic Algorithms
10_nn.ipynb 10. Neural Networks (scikit-learn)
11_nn_ga.ipynb 11. Neuroevolution

License

Copyright © 2024 by Daniel Shiffman.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.

Attribution: When using this work, you must credit the author as follows: "Daniel Shiffman, published by No Starch Press® Inc." and indicate that this is a py5 port of the original work.

See LICENSE for full license details, and CREDITS.md for the full copyright notice and per-chapter image credits.

Additional Resources

See RESOURCES.md for the original book's list of code-example ports to other languages and its further-reading bibliography (books, papers, and articles).

Prerequisites

  • Python 3.11 or higher
  • uv package manager
  • Git

Getting Started

1. Clone the repository

git clone git@github.com:hollygrimm/py5-noc-book-2.git
cd py5-noc-book-2

2. Create virtual environment

uv venv

3. Activate the virtual environment

source .venv/bin/activate

4. Install dependencies

uv sync

5. Install a Java 17 JDK and set JAVA_HOME (required for py5)

py5 runs on Processing/Java and needs a Java 17 JDK. If you don't already have one, the bundled install-jdk package can fetch one into ~/.jdk/:

uv run python -c "import jdk; print(jdk.install('17'))"

This prints the install path (something like ~/.jdk/jdk-17.0.17+10). Point JAVA_HOME at it:

export JAVA_HOME="$HOME/.jdk/jdk-17.0.17+10"   # use the path printed above

If you already have a JDK 17, set JAVA_HOME to that instead — e.g. $(/usr/libexec/java_home -v 17) on macOS, or a /usr/lib/jvm/java-17-* path on Linux. py5 needs Java 17 specifically; newer JDKs (e.g. 21) may not work.

Note: Add the export line to your ~/.bashrc or ~/.zshrc to make it permanent, then source it.

6. Start JupyterLab

jupyter lab

JupyterLab will open in your browser at http://localhost:8888

Project Structure

.
├── notebooks/          # Jupyter notebooks (chapters), solutions/, and images/
├── pyproject.toml      # Project dependencies
├── CREDITS.md          # Copyright, license, and image credits
├── RESOURCES.md        # Ports and further reading
└── README.md           # This file

Using py5 in Notebooks

All notebooks use the standard Python kernel from the virtual environment. Import py5 at the top of your notebook:

import py5

Then use py5 functions with the py5. prefix (e.g., py5.size(), py5.background(), py5.run_sketch()).

Dependencies

Key packages included:

  • py5[jupyter] - Processing for Python
  • jupyterlab - Interactive notebook environment
  • numpy - Numerical computing
  • matplotlib - Plotting and visualization
  • pymunk - 2D physics engine (Chapter 6)
  • scikit-learn - Machine learning (Chapter 10)
  • opencv-python - Computer vision
  • shapely - Geometric operations
  • trimesh - 3D geometry processing

See pyproject.toml for the complete list.

Development

The project uses uv for dependency management. To add new dependencies:

uv add <package-name>

Auditing dependencies for vulnerabilities

Before publishing, and after any dependency bump, check the locked dependency tree for known CVEs:

uv run --with pip-audit pip-audit

To fix a flagged package without disturbing the pinned, run-reviewed versions (py5, numpy, matplotlib, pymunk, scikit-learn, pillow), upgrade just that package:

uv lock --upgrade-package <name>
uv sync

Troubleshooting

JAVA_HOME not set

If you get Java-related errors, ensure JAVA_HOME is set:

echo $JAVA_HOME

About

A Python (py5) port of Daniel Shiffman's The Nature of Code — all 12 chapters as runnable Jupyter notebooks (full book text + code).

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages