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.
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
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?
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.