# Julia interfaces


{{< include ../_common_code.qmd >}}

```{julia}
#| echo: false
#| results: "hidden"
using CalculusWithJulia
using Plots
nothing
```

`Julia` can be used in many different manners. This page describes a few.


## The `REPL`


Base `Julia` comes with a `REPL` package, which  provides a means to interact with `Julia` at the command line.


```{julia}
#| eval: false
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.7.0 (2021-11-30)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> 2 + 2
4
```

The `julia>` prompt is where commands are typed. The `return` key will send a command to the interpreter and the results are displayed in the REPL terminal.


The REPL has many features for editing, for interacting with the package manager, or interaction with the shell. However it is command-line based, which no support for mouse interaction. For that, other options are available.


## `Pluto`


The `Pluto` package provides a notebook interface for interacting with `Julia`, which has a few idiosyncrasies, as compared to other interfaces.


Pluto is started from the REPL terminal with these two commands:


```{julia}
#| eval: false
using Pluto
Pluto.run()
```

Primarily, the variables in the notebook are **reactive**, meaning if a variable's value is modified, all references to that variables are also modified. This reactive nature makes it very easy to see the results of slight modifications and when coupled with HTML controls, allows easy user interfaces to be developed.


As a result, a variable name may only be used once in the top-level scope. (Names can be reused inside functions, which create their own scope and in "`let`" blocks, a trick used within these notes.) In the notes, subscripting and unicode variants are used for symbols which are typically repurposed (e.g., `x` or `f`).


Pluto cells may only contain one command, the result of which is displayed *above* the cell. This one command can be a `begin` or `let` block to join multiple statements.


Pluto has a built-in package management system that manages the installation of packages on demand.


`Pluto` notebooks can be easily run locally using `Pluto`.


`Pluto` notebooks are just `.jl` scripts, so can easily be shared.


## `IJulia`


"Project [Jupyter](https://jupyter.org/)  exists to develop open-source software, open-standards, and services for interactive computing across dozens of programming languages." The `IJulia` package allows `Julia` to be one of these programming languages. This package must be installed prior to use.


The Jupyter Project provides two web-based interfaces to `Julia`: the Jupyter notebook and the newer JupyterLab. The [binder](https://mybinder.org/) project use Juptyer notebooks for their primary interface to `Julia`. To use a binder notebook, follow this link:


[launch binder](https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master)


To run locally, these interfaces are available once `IJulia` is installed. Since version 1.7, the following commands should do this:


```{julia}
#| eval: false
using IJulia
notebook()
```

Should that not work, then this should as well:


```{julia}
#| eval: false
using Pkg
Pkg.add("PyCall")
Pkg.add("IJulia")
```

---


The notebook interface has "cells" where one or more commands can be entered.


In `IJulia`, a block of commands is sent to the kernel (the `Julia` interpreter) by typing "shift+return" or clicking on a "run" button. The output is printed below a cell, including graphics.


When a cell is evaluating, the leading `[]` has an asterick (`[*]`) showing the notebook is awaiting the results of the calculation.


Once a cell is evaluated, the leading `[]` has a number inserted (e.g., `[1]`, as in the figure). This number indicates the order of cell evaluation. Once a notebook is interacted with, the state of the namespace need not reflect the top-to-bottom order of the notebook, but rather reflects the order of cell evaluations.


To be specific, a variable like `x` may be redefined in a cell above where the variable is initially defined and this redefinition will hold the current value known to the interpreter. As well, a notebook, when reloaded, may have unevaluated cells with output showing. These will not influence the state of the kernel until they are evaluated.


When a cell's commands are evaluated, the last command executed is displayed. If it is desirable that multiple values be displayed, they can be packed into a tuple. This is done by using commas to separate values. `IJulia` will also display other means to print output (e.g., `@show`, `display`, `print`, ...).


To run all cells in a notebook from top to bottom, the "run all" command under the "Cell" menu is available.


If a calculation takes much longer than anticipated, the "kernel" can be interrupted through a menu item of "Kernel".


If the kernel appears unresponsive, it can be restarted through  a menu item of "Kernel".


Notebooks can be saved (as `*.ipynb` files) for sharing or for reuse. Notebooks can be printed at HTML pages, and if the proper underlying software is available, as formatted pages.


JupyterLab, a variant, has more features, commonly associated with an integrated development environment (IDE).


## VSCode


[Julia for Visual Studio Code](https://www.julia-vscode.org/)  provides support for the julia programming language for [VS Code](https://code.visualstudio.com/). VS Code is an open-sourced code editor supported by Microsoft. VS Code provides a cross-platform interface to `Julia` geared towards programming within the language.
