IPython and Jupyter has so much more…
--
Over the past few weeks that I’ve spent at botsplash, I’ve been using a lot of IPython, mainly in jupyter lab or notebooks along with terminal interfaces when working with larger programs rather than just scripts.
At first I treated it as a replacement for IDLE, just to make writing python a little faster and easier than writing it in normal notepad like applications. But over time I discovered great IPython features that I now use very frequently. Listed below are IPython and related tools I wish I would have known about earlier.
IPython magic:
IPython comes with what are called magic commands. They’re preceded by one or two ‘%’s. Those with only one are global commands, or only effect the next line while two involve ‘cell magic’.
Autoreload
The commands I find myself using the most are a string of commands that will autoreload changes to a module to the current python session.
%load_ext autoreload
%autoreload 2
There’s other options for usage here, but this allows me to use IPython to easily test modules I’m writing in an ad hoc manner, and without the pain of reloading everything every time I hit ctrl-s.
Timing
Often times I want to time something, early on to see if it’s fast enough to just use and move on, and later on in the process to optimize it.
%time
times the next line of code.
%%time
times the next cell of code. (And will create a cell if used in command line IPython).
%timeit
will run the next line of code several times, reporting to you stats about how long it took to run on average.
%%timeit
again the same as the above, but will time an entire cell.
Note: You probably don’t want to run the timeit commands with a function that outputs text, creates a new file, or takes a very long time to run, since it may run the statement(s) in question over 500,000 times in some cases. If you still want to time a statement more than once, there are additional parameters in the timeit command that you can use to better control how it runs.
Jupyter Widgets
While in most cases, when testing something or experimenting with different variables in a notebook manually editing the values of the variables is fine, but sometimes something a bit nicer and more intuitive can be useful. Especially if you are using the jupyter dashboard view.
Setting up widgets can even allow you to have them interact with each other, some widget reacting to inputs from another.
Notebook Extensions:
Jupyter notebook has support for custom notebook extensions, however doesn’t come with too many built in. The unofficial notebook extensions linked above solve this problem, and supply many (almost too many) notebook extensions. While I don’t come close to using all of them, some of my favorites include:
bqplot
The bqplot library is a useful alternative to the more common alternative of matplotlib when interactivity is a priority, as it expresses itself as an IPython Widget, this allows you to use other notebook widgets to get data or set data, giving you the ability to create more involved data ‘playgrounds’.
Qgrid
While most people who use python for data science and related tasks quickly learn the way the Jupyter Notebook can quickly output rows of your data frame to some cell, sometimes I realized this isn’t quite enough for what I wanted to do.
Often, I’ve found myself looking over my data, viewing rows and rows of data looking for inconsistencies or other interesting values that can’t be detected as easily with basic outlier detection or via statistical measures, and aren’t easily visualized. Tasks such as manually checking model results to see what sort of patterns the model may be learning, etc.
Conclusion:
I hope you’ve found or learned something useful in reading this post! I still have a lot to discover about the jupyter ecosystem, but wanted to write a bit about the things I wish I had known when starting.
Have fun and Happy Coding! If you are someone that has explored jupyter further and have thoughts to share, please leave a note. Thanks in advance, appreciate the help.
Do you want to read more of botsplash team contributions? Check out articles here.