Here is a list of useful Jupyter Notebook tips and tricks. The list is in no particular order.
1. Shell Commands
Do you exit your notebook to run a shell command?
You can run shell commands in your Jupyter Notebook by placing an exclamation mark before a command.
!pip install Tkinter
2. View a List of Shortcuts
Learning useful shortcut keys for working with Jupyter Notebooks can streamline your workflow over time.
The list of shortcuts is huge and there is no way to remember everything at once. This is where viewing a list of shortcuts comes in handy:
- Open up a Jupyter Notebook.
- Activate the command mode (press
- Press the
- See the list of all the shortcuts.
3. Magic Commands
In Jupyter, there are a bunch of magic commands to make your life easier.
A magic command is a shortcut to solve common problems, such as listing all the files in the current directory.
A magic command is useful, as it can be embedded directly into Python code. A magic command has a
Here are a bunch of useful magic commands:
# Print the current working directory %pwd # Show all the files in the current directory %ls # Change the working directory %ls [PATH_TO_DIR] # List all the variables %Who
View all the magic commands
The list of useful magic commands is long. View the list of all the available magic commands with:
Help with magic commands
To get more info about a specific magic command, highlight it and press
Shift + Tab:
4. Measure Cell Execution Time
%%time to get the elapsed time of running a cell of code.
5. Add Multiple Cursors
Save time when editing code by working with multiple cursors.
- Windows: Hold
alt+ left-click and drag your cursor.
- Mac: Hold
option+ left-click and drag your cursor.
6. Set Alarm for Program Completion
Ring an alarm when your program has completed execution.
On Windows, you can produce a beeping alarm. For example, let’s set an alarm of 440HZ for one second (1000ms):
import winsound duration = 1000 freq = 440 winsound.Beep(freq, duration)
Instead of a beeping alarm, you can use the built-in
say command to make your Mac say something when your program completes:
import os os.system('say "Your program has now finished"')
7. Extensions to Jupyter Notebooks
Jupyter Notebook is a great tool, but a bare notebook lacks useful features. This is where the extensions kick in.
- Run the following command in the command line to install the extensions:
pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install
- Then start a Jupyter Notebook and go to the “Nbextensions” tab:
- Activate the extensions by clicking them. These are added as buttons to the notebook toolbar:
For instance, use the Code Prettify extension to make your code look structured and beautiful with one click of a button:
8. View the Documentation of a Method
To view the documentation of a method, highlight the method and press
Shift + Tab. To further expand the modal, press the
+ button on the top right to expand the modal:
9. Extend the Number of Columns and Rows Shown in pandas
A pandas table can only show a limited number of rows and columns. However, you can change that.
For instance, let’s set the max output rows and columns to
import pandas as pd
10. Hide Unnecessary Output
Use a semicolon at the end of a statement to suppress an annoying output. For example, when plotting with Matplotlib, you see a somewhat redundant output before the graph:
To get rid of this, use a semicolon after the plotting statement:
Looks better, doesn’t it?
That’s it for the list. I hope I was able to increase your productivity and save your time with these Jupyter notebook tips.
Thanks for reading. Happy coding!
50 Python Interview Questions and Answers
50+ Buzzwords of Web Development
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.03.14Best AI Tools of 2023 (Save Time & Boost Productivity)