] add InstantiateFromURL
inside of a notebook or the REPLCo-authored with Arnav Sood
While Jupyter notebooks are a great way to get started with the language, eventually you’ll want to use more powerful Tools
We assume you’ve already completed the getting started lecture
Previously, we discussed basic use of the Julia REPL (“Read-Evaluate-Print Loop”)
Here, we’ll consider some more advanced features
Hitting ;
brings you into shell mode, which lets you run bash commands (PowerShell on Windows)
; pwd
/Users/arnavsood/research/lecture-source-jl/_build/jupyter
You can also use Julia variables from shell mode
x = 2
2
; echo $x
2
Hitting ]
brings you into package mode
] add Expectations
will add a package (here, Expectations.jl
)] rm Expectations
will remove that package] st
will show you a snapshot of what you have installed] up
will (intelligently) upgrade versions of your packages] precompile
will precompile everytihng possibleYou can get a full list of package-mode commands by running
] ?
Welcome to the Pkg REPL-mode. To return to the julia> prompt, either press backspace when the input line is empty or press Ctrl+C. Synopsis pkg> cmd [opts] [args] Multiple commands can be given on the same line by interleaving a ; between the commands. Commands activate: set the primary environment the package manager manipulates add: add packages to project build: run the build script for packages develop: clone the full package repo locally for development free: undoes a pin, develop, or stops tracking a repo gc: garbage collect packages not used for a significant time generate: generate files for a new project help: show this message instantiate: downloads all the dependencies for the project pin: pins the version of packages precompile: precompile all the project dependencies preview: previews a subsequent command without affecting the current state remove: remove packages from project or manifest resolve: resolves to update the manifest from changes in dependencies of developed packages status: summarize contents of and changes to environment test: run tests for packages update: update packages in manifest
Hitting ?
will bring you into help mode
The key use case is to find docstrings for functions and macros, e.g.
? print
search: print println printstyled sprint isprint prevind parentindices precision
print([io::IO], xs...)
Write to io
(or to the default output stream stdout
if io
is not given) a canonical (un-decorated) text representation of values xs
if there is one, otherwise call show
. The representation used by print
includes minimal formatting and tries to avoid Julia-specific details.
Printing nothing
is not allowed and throws an error.
jldoctest
julia> print("Hello World!")
Hello World!
julia> io = IOBuffer();
julia> print(io, "Hello", ' ', :World!)
julia> String(take!(io))
"Hello World!"
Note that objects must be loaded for Julia to return their documentation, e.g.
? @test
No documentation found.
Binding @test
does not exist.
will fail, but
using Test
? @test
@test ex
@test f(args...) key=val ...
Tests that the expression ex
evaluates to true
. Returns a Pass
Result
if it does, a Fail
Result
if it is false
, and an Error
Result
if it could not be evaluated.
jldoctest
julia> @test true
Test Passed
julia> @test [1, 2] + [2, 1] == [3, 3]
Test Passed
The @test f(args...) key=val...
form is equivalent to writing @test f(args..., key=val...)
which can be useful when the expression is a call using infix syntax such as approximate comparisons:
jldoctest
julia> @test π ≈ 3.14 atol=0.01
Test Passed
This is equivalent to the uglier test @test ≈(π, 3.14, atol=0.01)
. It is an error to supply more than one expression unless the first is a call expression and the rest are assignments (k=v
).
will succeed
As discussed previously, eventually you’ll want to use a full-fledged text editor
The most feature-rich one for Julia development is Atom
If you follow the instructions, you should see something like this when you open a new file
If you don’t, simply go to the command palette and type “Julia standard layout”
The bottom pane is a standard REPL, which supports the different modes above
The “workspace” pane is a snapshot of currently-defined objects. For example, if we define an object in the REPL
x = 2
2
Our workspace should read
The ans
variable simply captures the result of the last computation
The Documentation
pane simply lets us query Julia documentation
The Plots
pane captures Julia plots output
May be buggy, see for ex: here
Shift + Enter
will evaluate a highlighted selection or line (as above)Ctrl+Shift+Enter
) will run the whole fileYou can plug Juno/Atom into a Julia session running in a docker container, such as the QuantEcon base container
For instructions on this, see the Juno FAQ https://docs.junolab.org/latest/man/faq.html/_
Julia’s package manager lets you set up Python-style “virtualenvs,” that draw from an underlying pool of assets on the machine
environment
is a set of packages specified by a Project.toml
(and optionally, a Manifest.toml
)registry
is a git repository corresponding to a list of (typically) registered packages, from which Julia can pulldepot
is a directory, like ~/.julia
, which contains assets (compile caches, registries, package source directories, etc.)Essentially, an environment is a dependency tree for a project, or a “frame of mind” for Julia’s package manager
We can see the default (v1.0
) environment as such
] st
Status `~/.julia/environments/v1.0/Project.toml` [c52e3926] Atom v0.7.6 [6e4b80f9] BenchmarkTools v0.4.1 [b99e7846] BinaryProvider v0.5.2 [49dc2e85] Calculus v0.4.1 [aaaa29a8] Clustering v0.12.1 [34da2185] Compat v1.3.0 [a93c6f00] DataFrames v0.14.1 [1313f7d8] DataFramesMeta v0.4.0 [0c46a032] DifferentialEquations v5.3.1 [b4f34e82] Distances v0.7.3 [31c24e10] Distributions v0.16.4 [2fe49d83] Expectations v1.0.2 [442a2c76] FastGaussQuadrature v0.3.2 [587475ba] Flux v0.6.8 [28b8d3ca] GR v0.35.0 [bc5e4493] GitHub v5.0.2 [cd3eb016] HTTP v0.7.1 [09f84164] HypothesisTests v0.8.0 [7073ff75] IJulia v1.13.0 [43edad99] InstantiateFromURL v0.1.0 [a98d9a8b] Interpolations v0.10.5 [682c06a0] JSON v0.19.0 [e5e0dc1b] Juno v0.5.3 [5ab0869b] KernelDensity v0.5.1 [b964fa9f] LaTeXStrings v1.0.3 [945b72a4] MarketData v0.11.0 [2774e3e8] NLsolve v3.0.0 [d96e819e] Parameters v0.10.1 [91a5bcdd] Plots v0.21.0 [f27b6e38] Polynomials v0.5.1 [438e738f] PyCall v1.18.5 [d330b81b] PyPlot v2.6.3 [fcd29c91] QuantEcon v0.15.0 [1a8c2f83] Query v0.10.1 [ce6b1742] RDatasets v0.6.1 [60ddc479] StatPlots v0.8.1 [2913bbd2] StatsBase v0.25.0 [fd094767] Suppressor v0.1.1 [9e3dc215] TimeSeries v0.14.0 [44cfe95a] Pkg
We can also create and activate a new environment
] generate ExampleEnvironment
Generating project ExampleEnvironment:
ExampleEnvironment/Project.toml
ExampleEnvironment/src/ExampleEnvironment.jl
will create a directory with fresh TOML files, and
; cd ExampleEnvironment
/Users/arnavsood/Research/lecture-source-jl/_build/jupyter/ExampleEnvironment
will go there
To activate the directory, simply
] activate .
where “.” stands in for the “present working directory”
Let’s make some changes to this
] add Expectations Parameters
Updating registry at `~/.julia/registries/General` Updating git-repo `https://github.com/JuliaRegistries/General.git` Resolving package versions... Updating `~/Research/lecture-source-jl/_build/jupyter/ExampleEnvironment/Project.toml` [2fe49d83] + Expectations v1.0.2 [d96e819e] + Parameters v0.10.1 Updating `~/Research/lecture-source-jl/_build/jupyter/ExampleEnvironment/Manifest.toml` [7d9fca2a] + Arpack v0.3.0 [9e28174c] + BinDeps v0.8.10 [b99e7846] + BinaryProvider v0.5.2 [34da2185] + Compat v1.3.0 [864edb3b] + DataStructures v0.14.0 [31c24e10] + Distributions v0.16.4 [2fe49d83] + Expectations v1.0.2 [442a2c76] + FastGaussQuadrature v0.3.2 [e1d29d7a] + Missings v0.3.1 [bac558e1] + OrderedCollections v1.0.2 [90014a1f] + PDMats v0.9.5 [d96e819e] + Parameters v0.10.1 [1fd47b50] + QuadGK v2.0.2 [79098fc4] + Rmath v0.5.0 [a2af1166] + SortingAlgorithms v0.3.1 [276daf66] + SpecialFunctions v0.7.2 [2913bbd2] + StatsBase v0.25.0 [4c63d2b9] + StatsFuns v0.7.0 [30578b45] + URIParser v0.4.0 [2a0f44e3] + Base64 [ade2ca70] + Dates [8bb1440f] + DelimitedFiles [8ba89e20] + Distributed [b77e0a4c] + InteractiveUtils [76f85450] + LibGit2 [8f399da3] + Libdl [37e2e46d] + LinearAlgebra [56ddb016] + Logging [d6f4376e] + Markdown [a63ad114] + Mmap [44cfe95a] + Pkg [de0858da] + Printf [3fa0cd96] + REPL [9a3f8284] + Random [ea8e919c] + SHA [9e88b42a] + Serialization [1a1011a3] + SharedArrays [6462fe0b] + Sockets [2f01184e] + SparseArrays [10745b16] + Statistics [4607b0f0] + SuiteSparse [8dfed614] + Test [cf7118a7] + UUIDs [4ec0a83e] + Unicode
Note the lack of commas
To see the changes, simply open the ExampleEnvironment
directory in an editor like Atom
The Project TOML should look something like this:
name = "ExampleEnvironment"
uuid = "14d3e79e-e2e5-11e8-28b9-19823016c34c"
authors = ["QuantEcon User <quanteconuser@gmail.com>"]
version = "0.1.0"
[deps]
Expectations = "2fe49d83-0758-5602-8f54-1f90ad0d522b"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
We can also
] precompile
Precompiling project... Precompiling ExampleEnvironment
┌ Info: Precompiling ExampleEnvironment [5a154d4e-e3ae-11e8-367f-a96751ff0317] └ @ Base loading.jl:1189
Note The TOML files are independent of the actual assets (which live in ~/.julia/packages
, ~/.julia/dev
, and ~/.julia/compiled
)
You can think of the TOML as specifying demands for resources, which are supplied by the ~/.julia
user depot
To return to the default Julia environment, simply
] activate
without any arguments
Lastly, let’s clean up
; cd ..
/Users/arnavsood/Research/lecture-source-jl/_build/jupyter
; rm -rf ExampleEnvironment
With this knowledge, we can explain the operation of the setup block
using InstantiateFromURL
activate_github("QuantEcon/QuantEconLecturePackages", tag = "v0.9.0") # activate the QuantEcon environment
using LinearAlgebra, Statistics, Compat # load common packages
What this activate_github
function does is
.projects
] activate
that environment, and] instantiate
and ] precompile
, if necessary