Here are some useful features of JupyterLab:
-----
Split View
You can split the screen by dragging and dropping a notebook.
When you have two notebooks open:
- Vertical Split
- Horizontal Split
Similarly, you can freely create 3 splits, 4 splits, and so on.
- 3 splits
- 4 splits
Copying Cells
You can move or copy cells by drag and drop.
- Dragging and dropping a cell within the same notebook moves the cell.
- Dragging and dropping a cell between different notebooks copies the cell.
Show in File Browser
Use this when you want to open the location of the open notebook in the File Browser.
- Right-click on the file name and click "Show in File Browser"(at this time, the File Browser is displaying a different location than where the notebook is located).
- Then, it shows the location of the file in the File Browser.
" ! "
If you want to use system commands such as Linux commands in a Jupyter Notebook, prefix the command with "!".
!ls
The "-a" option displays hidden folders and hidden files.
!ls -a
!pwd
It returns the current working directory to the standard output. You can also assign the return value to a variable. The difference is that %pwd outputs to the standard output, while !pwd returns it as a string.
!pwd
!pip
It is the standard package management tool for Python. You can install and uninstall packages that are publicly available on the internet.
Main command examples:
- ' install [package_name] ': Install a package
- ' uninstall [package_name] ': Uninstall a package
- ' list ': List installed packages
Here is the "pip" manual
!tar
It extracts the tar file. If the extraction is successful, the extracted files will be displayed.
Example: Extracting a tar.gz file
!tar xvf test.tar.gz
!du
It displays the disk usage of the directory.
Example: Display the top 10 directories consuming the most disk space.
!du -sh /home/jovyan/* | sort -hr | head -10
Bonus Tip:
!apt-get moo
Magic Command
IPython kernel provides this feature, and by prefixing with % or %%, you can combine terminal commands or languages other than Python.
% applies to the rest of the line, while %% applies to the entire cell. These are called line magics and cell magics, respectively.
%pwd
It returns the working directory. You can also assign the return value to a variable. The difference is that !pwd outputs to the standard output, while %pwd returns it as text.
Example:
current_dir = %pwd
current_dir
%time, %timeit
The %time outputs the CPU time and Wall time for a line of code.
The %timeit outputs the average execution time (by default, CPU time) over several iterations for a line of code.
Example: Measure the time taken for energy inference using `%time` and `%timeit`.
%time atoms.get_potential_energy()
%timeit atoms.get_potential_energy()
%%bash, %%script bash
You can execute bash commands.
"%%bash" is a shortcut for the bash mode of the magic command "%%script bash", which allows you to run shell commands, and it performs the same operation.
Example: `grep` to search for notebook files (.ipynb) containing a string.
%%bash
grep -rIl --include="*.ipynb" "surface_editor" /home/jovyan/
%debug, %pdb on
It launches pdb, the standard debugger for Python. %debug is used to debug the most recent error if executed in a new cell after an error occurs. On the other hand, %pdb on is inserted at the beginning of the cell you want to debug and then executed.
For instructions on how to use pdb after starting it, please refer to the following manual.
https://docs.python.org/ja/3/library/pdb.html
Example of pdb command
-
- print([variable_name]) or p [変数名] : print the variable
- list or l,ll : Display the code around the error (ll displays a broader range)
%debug (Used after an error occurs)
%pdb on (Add this to the beginning of a cell)
%lsmagic
Displays a list of available magic commands.
%lsmagic
?
If you append it after a magic command, it outputs an explanation of that magic command.
??
Appending it after a magic command outputs the source code of that magic command.