Skip to main content

Jupyter Notebooks

Happy New Year!

Let’s start off 2016 by learning something new: Jupyter Notebooks, formerly known as IPython Notebooks.

A Jupyter notebook is an interactive document that incorporates text, math, graphics, and code. It can be viewed in a Web browser. Unlike most documents, however, you can modify and execute the code inside the document. In this sense, a Jupyter notebook is similar to a session in Mathematica or Maple. The difference is that the interpreter running behind the scenes is not Mathematica or Maple. Jupyter notebooks were designed to run Julia, Python, and R, but they support over 40 languages at present.

The best way to learn about Jupyter notebooks is to take a look at one. This notebook is a simple example from a greenhorn, but you can find many more on the Web.

Introduction.ipynb

Click on the link to view the notebook. You can download the notebook using the Save icon at the upper right corner of the notebook Web page. You can then open the notebook, run it, and see the commands used to create it.

In my very limited experience, I find that notebooks are excellent for sharing code but not for developing code. An IDE like Spyder or working from the command line is more efficient for writing and debugging code, running scripts repeatedly, and generating complex graphics. However, once the scripts and modules are working properly, you can call them within a notebook.

Jupyter notebooks are great for exploring new ideas and solving problems. A Jupyter notebook allows you to integrate formatted text, mathematical formulas, Python code, and graphics into a single document. You can write about your ideas, do mathematical analyses, write and run code, view graphics, and reflect on the results without switching applications.

Even if you do not like to do all of your work on your laptop or in the computer lab — I still prefer pencil and paper for many tasks — Jupyter notebooks are a great option for writing up your work and sharing it with others. A Jupyter notebook is like a word processor that can also run Python code. You can keep detailed notes with working code for your own personal files. You can write up homework assignments. You can write reports to share your work with other scientists. You can publish your work on the Web. (The procedure for sharing a notebook over the Internet can be found here.) A notebook can be exported in several formats, including HTML. If you have LaTeX installed on your system, you can also convert the notebook to a PDF file.

If you would like to see more examples of notebooks, explore this gallery. Enjoy!

Comments

Minkowski said…
Thank you for a wonderful book!
For physics work, do you recommend Jupyter over Spyder? Or what tool would be useful if you have equations that you want to see easily in your program?

I learned to use Spyder because of your book. Is there any tool readily available in Spyder to display equations clearly? I am talking about lengthy physics equations such as :

(x1+x2)**2/np.exp(-x1/x2)*x3/x4*np.exp(x4/x4**x3)+x2*np.sin(x3)**np.exp(x6/x4*np.cos(x1**x2)... that runs on for some 20 terms. Where one can easily make a lot of mistakes unless there is an easier way to see the equation displayed closer to how we write maths?
Jesse said…
I don't recommend either Jupyter or Spyder for everything. Each has its strengths and weaknesses. I prefer Jupyter notebooks for sharing work with others and for designing assignments and tutorials for students. I recommend Spyder for writing programs and debugging them, and for working on large projects that involve multiple files. (My own preference is to use the plain-text editor vim for writing code and IPython at a command line for running it, but I don't recommend this to new programmers.)

For symbolic mathematics, you might find the SymPy package helpful. It will "pretty print" mathematical expressions within Spyder or a Jupyter notebook. These formatted expressions look more like what you see in math and science texts. However, this will only work with SymPy objects and functions, not with NumPy functions. You could first develop your expression with SymPy, make sure it looks like what you would write down, and then use SymPy's "lambdify" method to create an equivalent NumPy function.

With practice, the transition between written mathematical expressions and Python code gets a lot easier.

Popular posts from this blog

Paths in Python

How do you get your Python interpreter to find modules that are not located in your current working directory? The answer is … you tell it where to look. When you type something like from my_module import my_function Python searches a collection of directories (i.e., folders) on your computer. If the directory containing <my_module.py> is not in this collection, you will receive an ImportError . This can be frustrating if you know the file exists, and even more so if you know where it exists. In this post, we will take a brief look at how to add paths to the collection of directories searched by Python. Paths A path is essentially a set of directions to a file: /Users/username/modules/my_module.py [Mac OS X, Linux] C:\modules\my_module.py [Windows] It tells your operating system how to navigate from a fixed starting point — the “root directory” / in Unix-based systems, or C:\ in Windows — through a co...

Raising a Figure Window to the Foreground

This post describes a utility function that will raise a plot window to the foreground of your screen. The function will only work with the Qt graphics backend, so I will start with a brief overview of graphics backends. If you just want to use the function as a black box, you can do the following: Set the Graphics Backend to “Qt” in the Spyder preferences menu. Copy this function into your working directory. Graphics Backends You may have have written a script to produce precisely the data you need, but a lot of processing is required to transform these numbers into a figure. You need to create a plot window, draw a figure inside of it, and manage all of the attributes that control a figure’s appearance: title, axis labels, line widths, colors, tick marks, etc. All of this happens in the background when you type a command like plt.plot(x,y) . A graphics backend is the software that Python uses to physically draw a figure on your computer screen. If you ha...

Illuminating Surface Plots

Matplotlib provides functions for visualizing three-dimensional data sets. One useful tool is a surface plot. A surface plot is a two-dimensional projection of a three-dimensional object. Much like a sketch artist, Python uses techniques like perspective and shading to give the illusion of a three-dimensional object in space. In this post, I describe how you can control the lighting of a surface plot. Surface Plots First, let’s look at some of the options available with the default three-dimensional plotting tools. This script will create a surface plot of a Bessel function. Its ripples will emphasize the effects of lighting later. import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # Import 3D plotting tools. from scipy.special import jn # Import Bessel function. # Define grid of points. points = np.linspace(-10, 10, 51) X, Y = np.meshgrid(points, points) R = np.sqrt(X**2 + Y**2) Z = jn(0,R) # Create 3D surfac...