Jupyter Notebook
Programming Languages Supported by Jupyter Notebook#
Jupyter Notebook, formerly known as IPython Notebook, has expanded to support multiple programming languages, including but not limited to:
- Python
- R
- Julia
- Scala
- Ruby
- JavaScript
- Bash
- C++
- Go
- Kotlin
This multi-language support makes Jupyter Notebook a powerful tool in data science, machine learning, and education.
Installing Jupyter Notebook#
It is recommended to install Jupyter Notebook using pip. Run the following command in the terminal:
pip install jupyter metakernel
After installation, test if it works correctly by running:
jupyter-notebook
If using ROOT, add the following to your .bashrc
file in your home directory:
source (pathof)thisroot.sh
Then, navigate to your project directory and run:
root --notebook
Steps to Add a Kernel#
To add a new kernel in Jupyter Notebook, follow these steps:
-
Install the Required Programming Language Environment
Ensure the runtime environment for the target programming language is installed, such as Python, R, or Julia. -
Install the Jupyter Kernel
Install the corresponding Jupyter kernel for the target language. For example: - For Python, use
ipykernel
:
pip install ipykernel
- For R, use
IRkernel
:
install.packages("IRkernel") IRkernel::installspec()
- For Julia, use
IJulia
:
using Pkg Pkg.add("IJulia")
-
For C#, use
dotnet-interactive
:
dotnet tool install -g Microsoft.dotnet-interactive dotnet interactive jupyter install
Orclingkernel
:
pip install clingkernel clingkernel install --sys-prefix
-
Register the Kernel
After installation, the kernel should automatically register with Jupyter Notebook. If not, you can manually register it using a command like:
python -m ipykernel install --user --name=my_kernel_name
-
Launch Jupyter Notebook
Start Jupyter Notebook and select the newly added kernel when creating a new file.
By following these steps, you can add and use new kernels in Jupyter Notebook to support additional programming languages.
Running Bash Scripts in Jupyter Notebook#
You can run Bash scripts in Jupyter Notebook using the following methods:
-
Using the
!
Command
Run Bash commands or scripts directly in a code cell using!
. For example:
!sh script.sh
-
Using the
subprocess
Module
Use thesubprocess
module in Python to run Bash scripts. For example:
import subprocess subprocess.run(['sh', 'script.sh'])
-
Using the
%%bash
Magic Command
Use the%%bash
magic command in a code cell to run Bash scripts. For example:
%%bash # Bash script content sh script.sh
These methods allow you to easily run Bash scripts in Jupyter Notebook and interact with system commands.
Using ROOT in Jupyter Notebook#
- Install ROOT6 (> 6.05)
- Install dependencies:
pip install jupyter metakernel
Start Using ROOTbooks#
Set up the ROOT environment (. $ROOTSYS/bin/thisroot.[c]sh
) and type in your shell:
root --notebook
This will start a ROOT-flavored notebook server on your computer.
Alternatively, to use the Jupyter command directly:
jupyter kernelspec install $ROOTSYS/etc/root/notebook/kernels/root --user
Once the server is running, you can use ROOT with two kernels:
- ROOT C++: A new kernel provided by ROOT.
- Python: Already provided by Jupyter.
C++ ROOTbook#
ROOT offers a C++ kernel that transforms the notebook into a ROOT prompt. Features include embedded graphics, syntax highlighting, and tab completion.
Example of plotting a histogram in a C++ ROOTbook:
TCanvas c;
TH1F h("h","ROOT Histo;X;Y",64,-4,4);
h.FillRandom("gaus");
h.Draw();
c.Draw();
Python ROOTbook#
To use Python, create a new Python kernel and import the ROOT libraries:
import ROOT
Example:
c = ROOT.TCanvas("c")
h = ROOT.TH1F("h","ROOT Histo;X;Y",64,-4,4)
You can also mix Python and C++ in the same notebook using the %%cpp magic:
%%cpp
h->FillRandom("gaus");
h->Draw();
c->Draw();
ipyplot#
video_paths = ["my_video.mp4"]
video_labels = ["My Video"]
ipyplot.plot_videos(video_paths, video_labels, width=320)
import matplotlib.pyplot as plt
import numpy as np
import ipyplot
import tempfile
import os
# 1. Use matplotlib to create a plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Matplotlib Plot")
# 2. Save the matplotlib plot to a temporary file
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
temp_filename = tmp_file.name
plt.savefig(temp_filename)
# 3. Display the temporary image file using ipyplot
ipyplot.plot_images([temp_filename], ["Matplotlib Plot"], img_width=400)
# 4. Clean up the temporary file
os.remove(temp_filename)
Created: 2025-04-30