Julia for Plebs, Part I: Six Different tools to write Julia code
… in which I discuss the different ways you can code in Julia
( Use this link to read this article for free )
Julia is an amazing programming language and excellent for data science and machine learning. I am planning to write a series of posts here where I am going to write about the different ways you can use Julia to do data science, read data, plot data, clean data, analyse data, and discover new knowledge. Julia is incredibly fast, and you can do (practically) everything you can do with SAS, SPSS, R, Matlab, or indeed many other programming languages. Your imagination is your limit. You can create a package. Developing a package in Julia is as easy as it gets and Julia is very intuitive. Among other things, Julia has an excellent friendly supportive community. In a nutshell, Julia is the best thing if you want to work with data
First things first, how many tools can you use to code in Julia?
In this post, I am going to write six ways you can code in Julia programming language from easiest to the more complex. These are the ways to write “static” documents. In a later version, we will dive into creating web apps and web pages in Julia. When you choose to use Julia, start with deciding what you want to do. To use editing documents, you do not need to install Julia. Julia is flexible.
One: if you want to install Julia on your own machine
Say you want to learn Julia the hard way: install Julia. Regardless of which operating system you are on, the process is super easy. In order to install Julia, visit https://julialang.org, and click on Download, which will bring up the following:
If you are on Mac or Linux, open up a terminal and type the following:
curl -fsSL https://install.julialang.org | sh
What does the above command do?
- It calls a programme named “curl”; if you do not have curl installed then you need to install it. Macs come with it, but if your flavour of Linux does not have curl, then install curl from the package manager you have. For example, in OpenSuse that I use, the command might be
sudo zypper install curl
For a comprehensive list of installation, see
Once it installs, all you need to do is to type julia
next to the command prompt and you will get what is referred to as REPL (Read Eval Print Loop), and you are all set to code in Julia. So if you type julia
at the command prompt, it will result in a window like below:
If you want to learn Julia step by step, this is all you need. Or may be, not. Read on …
If you want to save Julia files and work …
Then the first thing you do is to open a plain text editor, then open a file and save it in the name of foo.jl
. If you do not want to use a text editor on your computer, you can use an online editor as well, see this. You can give it any name you like so replace foo
with the name of the file you want to use. Generally, avoid spaces in filenames and leave filenames in “eight characters” and use an underscore between words if you want to have multiple words (this is referred to as “snake case”) or you can use something like fooBar.jl
where the first letter of the second word is in CAPS but the rest are in small cases. This is called “camel case”. For more details on these “cases”, see this. Just so you want to use text editors, here is a selection:
- If you are on Windows, use something like a Notepad (or Notepad++)
- If you are on Mac, I strongly encourage you to use something like BBEdit (this is a fantastic Mac App, you can use it for free, although you may want to pay for the full app)
- If you are on Linux, you can use Sublime Text (think of it like BBEdit for Linux, it is also free but it is nagware, meaning that it will nag you from time to time to purchase, you can use it without purchasing)
- You can use neovim (any platform)
- Emacs (any platform)
I have omitted integrated development environments (aka IDEs) for now. They are a set of apps that let you create codes and you can compile the codes without leaving the software, in other words, you will not need to switch between Julia REPL and code editor. The codes are also marked with colour codes. There are many, and you can turn text editors to IDEs if you like, but here we are going for what is simple and intuitive. You will save the file with the filename extension .jl
and that’s all you need. I find working with .jl
files intuitive for all my work. YMMV, see below.
In order to work with a simple plain text editor, open a file, give it a name and an extension .jl
and remember the folder. You will need to open Julia in the same folder. So, open Julia and type
cd("~/path/to/your/folder")
See the screenshot below:
That’s all you need to get started. You can write code in your text editor, save it, and in Julia, you can execute the code with the following command:
include("<your_file_name.jl">)
Replace <your_file_name>
with the name of your file where you write your code. Remember that your file and Julia now sits in the same folder
What if you do not want to install Julia?
Two: use Google Colab
There are several options. For now, in my opinion, the easiest is to use Google Colab notebook to open and write Julia codes and compile them. Here’s what you do:
- Visit https://colab.research.google.com/ and open a new notebook
- In the notebook that opens, click on Runtime, and then select Julia from the dropdown list and you are done
The good thing about this is that, you can run a google colab notebook running Julia and not worry about configuring or installing packages.
Three, use Juliahub
You definitely need to check out Juliahub. It’s, in my opinion, the best online best repository of Julia related software and you can learn a lot using Juliahub. I strongly encourage you to set up a free account in Juliahub and explore. This is how it looks like at the time:
Once you have created an account and logged in, Juliahub will provide you several options to run Julia online. I recommend you use Pluto notebooks (although you can use Julia IDE as well, but more on IDEs later), you will see something like as follows:
Four, use Pluto.jl for all your work
Clicking on Pluto.jl will bring up a Pluto interface. Pluto is an interactive notebook that runs Julia code and it is also reactive, which means that you can write and distribute interactive codes in Julia.
I have found it easy and intuitive to use Pluto for learning Julia. Here’s how to bring up Pluto
- First, open Julia with
julia
at the command prompt and this way, access the REPL - Then, assuming that you do not have Pluto installed in Julia, type from within REPL
]
- Step 2 will bring up an interface named
Pkg
- Then type
add Pluto
; this will install Pluto on your machine - Once Pluto is installed, in Julia REPL, type
using Pluto; Pluto.run()
and this will launch Pluto that will appear as below:
Then if you click on Create a new notebook
, it will open the Pluto Notebook that will appear as follows:
Pluto is excellent for learning Julia, and it is also useful for practically anything you want to do with Julia. You can prototype ideas, write papers and blogs, and do complex data analyses. For now, let’s continue with the other ways you can use notebooks in Julia.
Five, use Jupyter Notebooks and use Julia in them
Jupyter Notebooks and particularly, Jupyter Lab are great computational notebooks where you can weave codes and notes. There are several ways to use them, but we will discuss here setting them up from within Julia. From within a Julia REPL, write the following:
]
this will bring up the Pkg
interface
In the package interface, type add IJulia
This will install the Julia kernel and also install the jupyter notebook
Then once the Jupyter notebook is installed, you can start a Jupyter Notebook with:
using IJulia; notebook()
in the folder where you are working.
You will see an interface as follows:
A Jupyter Notebook is very versatile where you can weave codes and notes exactly like you are able to do in a Pluto notebook. Besides, this notebook works from your default web browser. You can also “spawn” jupyter notebooks from within integrated development environments such as VSCode (more about them later).
If you do not want to use Pluto and IJulia notebooks, you can use Weave.jl
and Franklin.jl
packages if you like. Let’s dive in.
Six, use `Weave.jl` and jmd
In order to use Weave, you only have to use a plain text editor and markdown to edit your code. If you are not familiar with markdown, here is a guide to follow. You can set it up as follows:
- From within Julia REPL, first install
Weave.jl
with]
and then from]
, typingadd Weave
- Open a plain text document and save it as
weavetry.jmd
. Note the extension, it is notjl
but.jmd
. That “j” stands for julia and “md” for markdown. - Your plain text document might look like as follows
# Introduction
This is a plain text written in markdown
```julia
using DataFrames
x = [1,2,3]
y = [4,5,6]
df = DataFrame(:x => x, :y => y)
```
## The above is a code
Then we can write a plot as follows
```julia
using Plots
plot(df.x, df.y)
```
If you then did weave("foo.jmd")
, Julia would produce a document such as follows:
Conclusion
So you have six different tools to write Julia codes. All of these will produce static codes. I have not covered here IDEs and Quarto for using with Julia. The other ways are using Franklin for authoring webpages, and Genie for developing web apps. Before we get to these topics, in the next post, I will write about the first steps of using Julia to do read, plot, and model data.