What is a jupyter notebook and how do I use it?
All too often on this blog you'll be minding your own business and.... BAM jupyter notebook.
"I have no idea what those are Dom."
Have no fear. That's what we'll be looking at in this post.
What is a jupyter notebook?
Suppose you've written a neat piece of code.
It takes all the URLs in a list and changes
https://. It will save you loads of time.
You want to run it on a list of URLs.
You'd need somewhere to run that code. There are plenty of places you can do this and jupyter notebook is one of them.
For analysis (which is what we're mostly doing on this blog), I think it's rather a good one.
Most commonly it is used to run python, that's what we'll be covering here, although you can use it for others.
How can I install jupyter notebook?
There are a variety of ways. The easiest is to install it with Anaconda.
Anaconda installs python & jupyter notebooks for you, along with some other goodies. I've written about how to install python here.
How can you start jupyter notebook?
Once we've installed anaconda we can start it one of two ways:
- First your Windows Explorer Search/Mac spotlight should now have a shortcut to Jupyter Notebook.
You can just run that and Jupyter should open in a browser tab!
For reasons unknown to me, sometimes for people that shortcut just hangs forever. In that case have no fear, we need to:
Open a command line:
- On Mac search for
- On Windows search for
cmdand open command prompt.
- On Mac search for
- Then type in
jupyter notebookand hit enter.
- Give it a moment and it should open in a browser tab.
How to open/create a notebook
Once you've got the jupyter window open, we can navigate around like a normal file explorer.
We can create a notebook with the button in the top right hand corner, or open an existing notebook by clicking on it.
Let's make a notebook now.
When you click new, it will create the notebook in the folder you're in, so navigate to a folder you can easily find again.
How does jupyter notebook work?
Jupyter notebook is an interactive coding environment. It's easy to re-run and test code.
The main concept in jupyter notebook is a cell:
We type code into a cell. We run the cell.
If we save something in one cell, we can use it in another cell.
It lets us quickly test and write code.
A quick demo
This is what you should see in your new fancy notebook.
Let's quickly get it do something.
Ctrl + Enter(
Cmd + Enteron a Mac)
Hooray! We now have a place to code.
We can easily change our code and re-run just the cell we've changed.
Actually using it
There are a couple things worth knowing, when you're using it.
There are two types of actions you want to do in a notebook.
Type in a cell and write code. This is input mode.
- The cell you're on will be highlighted in green.
Add new cells, delete cells, run multiple cells at once etc. This is command mode.
- The cell you're on will be highlighted in blue.
How to enter input mode on a cell
We type in a cell by clicking on it, or hitting enter when it's selected.
How to move to command mode
We move back to command mode, by hitting escape.
How do you add new cells?
Make sure you're in command mode, then hit
b on the keyboard.
badds a cell below you.
aadds a cell above you.
How do you run a cell
Have it selected in either mode.
Ctrl + Enter on Windows.
Cmd + Enter on a Mac.
Menu's vs shortcuts
You can use the mouse for all these things, they're all options on the menu's at the top.
I would highly recommend learning the keyboard shortcuts, it will massively speed you up.
h will open up a window with all the shortcuts!
Where can I find some examples to get started?
Now you have a place to run some code. What next?
If you know a little python
Time to start writing some code to solve your problems.
I've been slowly publishing more and more analysis/marketing focused jupyter notebooks. Probably best getting starting one I have live right now, is a tutorial for using pandas for basic data analysis (to replace Excel) live.
I'm slowly putting more live. When I've done that I'll put them all in one place and link that here.
If you don't know any python
If you don't know python at all, I'd recommend going to use the code academy course and then coming back to this. This tutorial isn't about teaching you how to code.
Why are jupyter notebooks good?
We didn't cover this earlier, but it's worth briefly mentioning why notebooks are a good place to run code.
- Crucially I've found beginners find it less intimidating than trying to run a script. This is the big reason I use it again and again.
- It allows us to run our code in small segments. We can easily re-run and change parts when it doesn't work.
- When we print certain things, Jupyter notebook will automatically format them for us nicely.
- We can mix together code, output tables & graphs and comments easily.
Why is python & jupyter good in general? Why should you consider using this to replace Excel?
That is explored in more detail in this other post: using python to replace Excel.
What didn't make sense? Are you still wildly stuck on getting this up and running?
Hit me up in the comments and I'll answer what I can.