In this lecture we’ll start examining different features of the Julia and Jupyter environments
Recall that the easiest way to get started with these notebooks is to follow the cloning instructions earlier
To summarize, if on a desktop you should clone the notebooks repository https://github.com/quantecon/quantecon-notebooks-julia, then in a Julia REPL type
using IJulia; jupyterlab()
Hint: Julia will remember the last commands in the REPL, so you can use up-arrow to restart JupyterLab
Alternatively, if you are using an online Jupyter, then you can directly open a new notebook
Finally, if you installed Jupyter separately or have added added Jupyter to the Path then cd
to the folder location in a terminal, and run
jupyter lab
Regardless, your web browser should open to a page that looks something like this
The page you are looking at is called the “dashboard”
If you click on “Julia 1.x.x” you should have the option to start a Julia notebook
Here’s what your Julia notebook should look like
The notebook displays an active cell, into which you can type Julia commands
Notice that in the previous figure the cell is surrounded by a blue border
This means that the cell is selected, and double-clicking will place it in edit mode
As a result, you can type in Julia code and it will appear in the cell
When you’re ready to execute these commands, hit Shift-Enter
The next thing to understand about the Jupyter notebook is that it uses a modal editing system
This means that the effect of typing at the keyboard depends on which mode you are in
The two modes are
(To learn about other commands available in command mode, go to “Keyboard Shortcuts” in the “Help” menu)
Esc
keyEnter
or click in a cellThe modal behavior of the Jupyter notebook is a little tricky at first but very efficient when you get used to it
To run an existing Julia file using the notebook you can copy and paste the contents into a cell in the notebook
If it’s a long file, however, you have the alternative of
include("filename")
in a cellThe present working directory can be found by executing the command pwd()
Note that if you’re using a JupyterHub setup, you will need to first run
using InstantiateFromURL
github_project("QuantEcon/quantecon-notebooks-julia", version = "0.2.0")
Activated /home/qebuild/repos/lecture-source-jl/_build/website/jupyter/Project.toml Info quantecon-notebooks-julia 0.1.0 activated, 0.2.0 requested
in a new cell (i.e., Shift + Enter
)
This might take 15-20 minutes depending on your setup, as it installs a large set of packages for our use
Run the following cell
using Plots
gr(fmt=:png);
plot(sin, -2π, 2π, label="sin(x)")
You’ll see something like this (although the style of plot depends on your installation)
Note: The “time-to-first-plot” in Julia takes a while, since it needs to compile many functions - but is almost instantaneous the second time you run the cell
Let’s go over some more Jupyter notebook features — enough so that we can press ahead with programming
Tab completion in Jupyter makes it easy to find Julia commands and functions available
For example if you type rep
and hit the tab key you’ll get a list of all
commands that start with rep
To get help on the Julia function such as repeat
, enter ? repeat
Documentation should now appear in the browser
In addition to executing code, the Jupyter notebook allows you to embed text, equations, figures and even videos in the page
For example, here we enter a mixture of plain text and LaTeX instead of code
Next we Esc
to enter command mode and then type m
to indicate that we
are writing Markdown, a mark-up language similar to (but simpler than) LaTeX
(You can also use your mouse to select Markdown
from the Code
drop-down box just below the list of menu items)
Now we Shift + Enter
to produce this
Julia supports the use of unicode characters
such as α
and β
in your code
Unicode characters can be typed quickly in Jupyter using the tab
key
Try creating a new code cell and typing \alpha
, then hitting the tab
key on your keyboard
You can execute shell commands (system commands) in Jupyter by prepending a semicolon
For example, ; ls
will execute the UNIX style shell command ls
,
which — at least for UNIX style operating systems — lists the
contents of the current working directory
These shell commands are handled by your default system shell and hence are platform specific
You can execute package operations in the notebook by prepending a ]
For example, ] st
will give the status of installed packages in the current environment
Note: Cells where you use ;
and ]
must not have any other instructions in them (i.e., they should be one-liners)
Notebook files are just text files structured in JSON and typically end with .ipynb
A notebook can easily be saved and shared between users — you just need to
pass around the ipynb
file
To open an existing ipynb
file, import it from the dashboard (the first
browser page that opens when you start Jupyter notebook) and run the cells or edit as discussed above
The Jupyter organization has a site for sharing notebooks called nbviewer which provides a static HTML representations of notebooks
QuantEcon also hosts the QuantEcon Notes website, where you can upload and share your notebooks with other economists and the QuantEcon community
As we saw in the desktop installation, the REPL is a Julia specific terminal
It becomes increasingly important as you learn Julia, and you will find it to be a useful tool for interacting with Julia and installing packages
As a reminder, to open the REPL on your desktop, either
julia
(Linux)If you are using a JupyterHub installation, you can start the REPL in JupyterLab by choosing
Julia
ConsoleWe examine the REPL and its different modes in more detail in the tools and editors lecture
If you installed Jupyter using Julia, then you may find it convenient to add it to your system path in order to launch JupyterLab without running a Julia terminal.
The default location for the Jupyter binaries is relative to the .julia
folder (e.g., "C:\Users\USERNAME\.julia\conda\3\Scripts
on Windows)
You can find the directory in a Julia REPL using by executing
] add Conda
using Conda
Conda.SCRIPTDIR
On Linux/OSX, you could add that path to your .bashrc
On Windows, to add directly to the path, type ;
to enter shell mode and then execute
setx PATH "$(Conda.SCRIPTDIR);%PATH%"