IJulia: The Julia Notebook
Tags: julialang, tutorial
Published: 2023-03-30 07:53:59

There are various tools available for developers to use for building and executing several commands. They include: Code Editors, Integrated Development Environment (IDE), and Notebooks. Each tool has its use case and purpose. The tool of special interest is the Notebook. A notebook is a type of programming editor that allows users to write, run, and debug code in an interactive environment. Notebooks are a great way to keep track of code, as they allow you to write code and see the output of your code in the same window.

Some of the benefits of using a notebook include: Support for multiple languages, Easy debugging, Rich output, and Collaboration.

In this article, we will explore the IJulia notebook, a powerful tool for data analysis and scientific computing.

## What is IJulia?

[IJulia](https://julialang.github.io/IJulia.jl/stable/#IJulia) is an interactive notebook environment powered by the Julia programming language. Its backend is integrated with that of the Jupyter environment. The interface is web-based, similar to the [iPython](https://ipython.org/) notebook. It is open-source and cross-platform.

IJulia environment can also be used by IPython and it allows for the interaction of Julia code on the web browser, including support for plotting, images, formatted text, and many more. If you’re familiar with Jupyter Notebook, it would be easy to use the IJulia notebook as they both have the same interfaces.

## Why use IJulia?

IJulia combines the power of the Julia language with the interactivity of the Jupyter notebook interface. As a Julia developer, using IJulia as a code editor serves the following purposes:

1. It enables users to build and debug code as well as evaluate and visualize data.
    
2. IJulia makes it easy to quickly get started with Julia without having to install any additional software or libraries.
    
3. It provides an intuitive interface for coding and data visualization, making it a great choice for those looking to get started with data science.
    

## Getting Started with IJulia

This section would be going through the process of getting started with the IJulia notebook.

### Installation

To begin with IJulia, the [Julia Language](https://julialang.org/downloads/) has to be downloaded first. If Julia is already installed, this part can be skipped. After installing Julia, head over to the Julia REPL and type in the following commands

```julia
using Pkg
Pkg.add("IJulia")
```

Another method of installation is by entering the pkg mode by typing in `]` in your terminal and then `add IJulia`

```julia
add IJulia
```

**Note:** To exit the package terminal, simply hit the backspace button.

Any of the commands install IJulia and the [kernel specification](https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs) that tells Jupyter how to launch Julia. For more in-depth information about the installation process, visit the [manual installation page](https://julialang.github.io/IJulia.jl/stable/manual/installation/).

### Running IJulia

After installing it to your taste and style, you can now run IJulia. In the Julia REPL and type in the following commands with the `julia>` prompt

```julia
using IJulia
notebook()
```

This would launch the notebook in your default browser. The first time you run the `notebook()` command, it is going to ask for permission to install Jupyter. Hit on enter to allow it to use the [Conda.jl](https://github.com/Luthaf/Conda.jl) package access Miniconda to install a minimal Python/Jupyter distribution. For more information on how to customize how to run IJulia on your Operating System or local machine, visit the documentation page on how to [run IJulia](https://julialang.github.io/IJulia.jl/stable/manual/running/).

It should bring up a webpage similar to this

![Screenshot of IJulia's homepage.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5wcnvonp2urm160okgc.png)

Clicking on the “New” dropdown would give you options for the available notebooks. For example, I have Notebooks Julia 1.7.2 and 1.8.1 installed. There is also a Python3 notebook available.


![Image of the list of Notebooks available on IJulia](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qonnt95p6pi5wy9ebl7.png)

## Operations in IJulia

In this section, some basic operations that can be done in the IJulia environment would be discussed. To begin, create a New Notebook - New → Julia (version\_name), in my case Julia 1.8.1.

- **Printing “Hello World”**
    
    In the new notebook created, click on the first cell and type in
    
    ```julia
    println("Hello, World!")
    ```
    
    Run the cell with the “Run” option or use the shortcut “Shift+Enter” on Windows or Mac. Your result would be in the format

![Result of printing "Hello World" on the notebook.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nm6nye53kzwn6l7kdvnt.png)
Now. you’ve printed a Julia code in your notebook! Let’s move on to writing a function.
    
- **Writing a Function**
    
    In the next cell, let’s declare a function that takes a list and returns a new list with unique elements of the first list.
    
    ```julia
    function uniquelist(list)
        new_list = Set{}()
        for el in list
            push!(new_list, el)
        end
        return collect(new_list)
    end
    ```
    
    Take, for example, a list `[1,1,2,3]`, this would return the list of unique numbers i.e. `[1,2,3]`.
    
 ![Result of the above function on the notebook.]
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2pemxsqyv9ddiyq2sfe.png)
    
    Let’s plot a graph now.
    
- **Plotting a Graph**
    
    Like it was stated earlier, IJulia supports plotting. For this purpose, the Plots.jl library would be used. To add the Plots.jl package, go to a new cell, and type in the following
    
    ```julia
    using Pkg
    Pkg.add("Plots")
    ```
    
    This would begin the process of downloading and installing the package.
    
    ![Image of downloading the "Plots" package]
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhho7naeh6ixakl613p.png)

    After downloading, let’s draw a line. The first step is to instantiate the package, then draw the line.
    
    ```julia
    using Plots
    title!("Drawing a Straight Line")
    plot!([0,0],[0,1.1],arrow=true,color=:black,linewidth=2,label="")
    ```
    
    Which would give a result below
    
    ![Image of a plot using the "Plot" package]
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzrx2lii9gk3ggpeiu6p.png)
    
    To save a plot, use the `savefig()` to save
    
    ```julia
    savefig("plot_name.png")
    ```
    
    Let’s plot the [Lorenz Attractor](https://en.m.wikipedia.org/wiki/Lorenz_system) from the Plots.jl documentation.
    
   {% embed https://gist.github.com/Ifihan/939cced2ff9e1ababff7b09f9838da1b %}
    
  Which would give us a lovely GIF!
    

<img width="100%" style="width:100%" src="https://media.giphy.com/media/yLY1l5CzrR65eIE4Y3/giphy.gif">
   
- **Viewing tables**
    
    IJulia also supports viewing and manipulating tables. To create a table, first install the [DataFrames.jl](https://dataframes.juliadata.org/stable/) package by running the following command in a new cell:
    
    ```julia
    using Pkg
    Pkg.add("DataFrames")
    ```
    
    After installing the package, create a new cell and type in the following commands to create a new table:
    
    ```julia
    using DataFrames
    df = DataFrame(A = [1, 2, 3], B = ["a", "b", "c"])
    ```
    
    This would create a table with two columns, `A` and `B`, and three rows. To view the table, simply run the cell.
    
    ![Image of a table with the "DataFrames" package]
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uax50c275sp40e4s7937.png)    

Many other operations can be carried out on the IJulia notebook like: Use of markdown and latex for text formatting and mathematical expressions, and importing and exporting data in various formats such as CSV, Excel, and JSON among others.

## Conclusion

IJulia offers a powerful and interactive environment for developers to write, manage, and test code in the Julia programming language. Its integration with Jupyter and support for data visualization make it a great choice for data scientists and other researchers.

With IJulia, users can quickly get started with Julia without having to install any additional software or libraries, and benefit from the productivity and debugging tools that come with a code editor.