Skip to content

Commit f9ddee8

Browse files
authored
Examples in the docs: base PR for cleaning up notebook workflow (#303)
Following #314, this PR adds READMEs for how to build & add to the docs and examples, as well as tidying the workflow up a bit more. It makes all current examples run (with a warning to say they're under construction).
1 parent 037cd74 commit f9ddee8

File tree

19 files changed

+3514
-40
lines changed

19 files changed

+3514
-40
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
/Manifest.toml
44
/test/Manifest.toml
55
coverage/
6-
src/update_v0.8.0
76
.DS_store

docs/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
build/
22
site/
3-
src/examples/
3+
src/examples/

docs/Manifest.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# This file is machine-generated - editing it directly is not advised
22

3+
[[AbstractFFTs]]
4+
deps = ["LinearAlgebra"]
5+
git-tree-sha1 = "051c95d6836228d120f5f4b984dd5aba1624f716"
6+
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
7+
version = "0.5.0"
8+
39
[[ArgTools]]
410
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
511

@@ -115,6 +121,12 @@ path = ".."
115121
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
116122
version = "0.10.6"
117123

124+
[[Kronecker]]
125+
deps = ["FillArrays", "LinearAlgebra", "NamedDims", "SparseArrays", "StatsBase"]
126+
git-tree-sha1 = "90e082a267982069e624ea0f825d324c86a01b4e"
127+
uuid = "2c470bb0-bcc8-11e8-3dad-c9649493f05e"
128+
version = "0.4.3"
129+
118130
[[LibCURL]]
119131
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
120132
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
@@ -173,6 +185,12 @@ uuid = "a63ad114-7e13-5084-954f-fe012c677804"
173185
[[MozillaCACerts_jll]]
174186
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
175187

188+
[[NamedDims]]
189+
deps = ["AbstractFFTs", "LinearAlgebra", "Pkg", "Requires", "Statistics"]
190+
git-tree-sha1 = "e91d3ee8ac1514651b6e85686ca31257d17b1eb2"
191+
uuid = "356022a1-0364-5f58-8944-0da4b18d706f"
192+
version = "0.2.33"
193+
176194
[[NetworkOptions]]
177195
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
178196

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
KernelFunctions = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
4+
Kronecker = "2c470bb0-bcc8-11e8-3dad-c9649493f05e"
45
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
56

67
[compat]
78
Documenter = "0.27"
89
KernelFunctions = "0.10"
910
PDMats = "0.11"
11+
Kronecker = "0.4"
1012
julia = "1.3"

docs/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# How to build the docs locally
2+
3+
If you just want to modify or add an example, you can [build just the example](../examples/README.md) without having to build the full documentation locally.
4+
5+
If you want to build the documentation, navigate to this (docs/) directory and open a Julia REPL.
6+
7+
## Instantiation
8+
9+
First activate the documentation environment:
10+
```julia
11+
julia> ] activate .
12+
```
13+
Alternatively, you can start Julia with `julia --project=.`.
14+
15+
Then install all packages:
16+
```julia
17+
julia> ] instantiate
18+
```
19+
By default, this will use the development version of KernelFunctions in the parent directory.
20+
21+
## Build
22+
23+
To build the documentation, run (after activating the documentation environment)
24+
```julia
25+
julia> include("make.jl")
26+
```
27+
You can speed up the process if you do not execute the examples and comment out the
28+
relevant sections in `docs/make.jl`.
29+
30+
The output is in the `docs/build/` directory and best viewed in a browser.
31+
The documentation uses pretty URLs which can be a hindrance if you browse the documentation locally.
32+
The [Documenter documentation](https://juliadocs.github.io/Documenter.jl/stable/man/guide/#Building-an-Empty-Document) suggests that
33+
34+
> You can run a local web server out of the `docs/build` directory. One way to accomplish
35+
> this is to install the [LiveServer](https://github.com/tlienart/LiveServer.jl) Julia
36+
> package. You can then start the server with `julia -e 'using LiveServer; serve(dir="docs/build")'`.
37+
> Alternatively, if you have Python installed, you can start one with
38+
> `python3 -m http.server --bind localhost` (or `python -m SimpleHTTPServer` with Python 2).
39+
40+
If you make any changes, you can run
41+
```julia
42+
julia> include("make.jl")
43+
```
44+
again to rebuild the documentation.
45+
46+
# How to contribute to the docs
47+
48+
## To add additional docs dependencies
49+
50+
To add additional dependencies, run (after activating the documentation environment)
51+
```julia
52+
julia> ] add NewDependency
53+
```
54+
or, as a shell one-liner:
55+
```shell
56+
julia --project=. -e 'using Pkg; Pkg.add("NewDependency")'
57+
```
58+
and commit the changes to Project.toml and Manifest.toml.
59+
60+
61+
## To add examples
62+
63+
See [../examples](../examples/README.md)

docs/literate.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const OUTDIR = ARGS[2]
88
# Activate environment
99
using Pkg: Pkg
1010
Pkg.activate(dirname(SCRIPTJL))
11+
# Note that each example's Project.toml must include Literate as a dependency
1112
Pkg.instantiate()
1213
using Literate: Literate
1314

@@ -23,14 +24,13 @@ function preprocess(content)
2324
#
2425
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/examples/@__NAME__.ipynb)
2526
#md #
26-
# You are seeing the
27+
# *You are seeing the
2728
#md # HTML output generated by [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) and
2829
#nb # notebook output generated by
2930
# [Literate.jl](https://github.com/fredrikekre/Literate.jl) from the
3031
# [Julia source file](@__REPO_ROOT_URL__/examples/@__NAME__/script.jl).
31-
# The corresponding
32-
#md # notebook can be viewed in [nbviewer](@__NBVIEWER_ROOT_URL__/examples/@__NAME__.ipynb).
33-
#nb # HTML output can be viewed [here](https://juliagaussianprocesses.github.io/KernelFunctions.jl/dev/examples/@__NAME__/).
32+
#md # The corresponding notebook can be viewed in [nbviewer](@__NBVIEWER_ROOT_URL__/examples/@__NAME__.ipynb).*
33+
#nb # The rendered HTML can be viewed [in the docs](https://juliagaussianprocesses.github.io/KernelFunctions.jl/dev/examples/@__NAME__/).*
3434
#
3535
""",
3636
)

docs/make.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# run examples
1+
### Process examples
22
const EXAMPLES_SRC = joinpath(@__DIR__, "..", "examples")
33
const EXAMPLES_OUT = joinpath(@__DIR__, "src", "examples")
44
const LITERATEJL = joinpath(@__DIR__, "literate.jl")
@@ -24,10 +24,12 @@ end
2424
# Check that all examples were run successfully
2525
isempty(processes) || success(processes) || error("some examples were not run successfully")
2626

27-
# Build documentation
28-
using KernelFunctions
27+
### Build documentation
2928
using Documenter
3029

30+
using KernelFunctions
31+
using PDMats, Kronecker # we have to load all optional packages to generate the full API documentation
32+
3133
# Print `@debug` statements (https://github.com/JuliaDocs/Documenter.jl/issues/955)
3234
if haskey(ENV, "GITHUB_ACTIONS")
3335
ENV["JULIA_DEBUG"] = "Documenter"
@@ -39,10 +41,7 @@ DocMeta.setdocmeta!(
3941
:DocTestSetup,
4042
quote
4143
using KernelFunctions
42-
using LinearAlgebra
43-
using Random
44-
using PDMats: PDMats
45-
end;
44+
end; # we have to load all packages used (implicitly) within jldoctest blocks in the API docstrings
4645
recursive=true,
4746
)
4847

docs/src/create_kernel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Here are a few ways depending on how complicated your kernel is:
1010

1111
If your kernel function is of the form `k(x, y) = f(d(x, y))` where `d(x, y)` is a `PreMetric`,
1212
you can construct your custom kernel by defining `kappa` and `metric` for your kernel.
13-
Here is for example how one can define the `SqExponentialKernel` again :
13+
Here is for example how one can define the `SqExponentialKernel` again:
1414

1515
```julia
1616
struct MyKernel <: KernelFunctions.SimpleKernel end

examples/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Example notebooks
2+
3+
The examples in this directory are stored in [Literate.jl](https://github.com/fredrikekre/Literate.jl) format.
4+
5+
To run them locally, navigate to the directory with the example that you want to run and
6+
start the Julia REPL and activate the project environment of the example:
7+
```julia
8+
julia> ] activate .
9+
```
10+
Alternatively, you can start Julia with `julia --project=.`. Then install all required
11+
packages with
12+
```julia
13+
julia> ] instantiate
14+
```
15+
Afterwards simply run
16+
```julia
17+
julia> include("script.jl")
18+
```
19+
In particular when editing an example, it can be convenient to (re-)run only some parts of
20+
an example.
21+
Many editors with Julia support such as VSCode, Juno, and Emacs support the evaluation of individual lines or code chunks.
22+
23+
You can convert a notebook to markdown and Jupyter notebook formats, respectively, by executing
24+
```julia
25+
julia> using Literate
26+
julia> Literate.markdown("script.jl", "output_directory")
27+
julia> Literate.notebook("script.jl", "output_directory")
28+
```
29+
(see the [Literate.jl docs](https://fredrikekre.github.io/Literate.jl/v2/) for additional options) or run
30+
```shell
31+
julia docs/literate.jl examples/myexample/script.jl output_directory
32+
```
33+
which also executes the code and generates embedded plots etc. in the same way as in building the KernelFunctions documentation.
34+
35+
## Add a new example
36+
37+
Create a new subdirectory in here, and put your code in a file called `script.jl` so that it will get picked up by the automatic docs build.
38+
39+
Every example uses a separate project environment. Therefore you should also create a new
40+
project environment in the directory of the example that contains all packages required by your script.
41+
Note that the dependencies of your example *must* include the `Literate` package.
42+
43+
From a Julia REPL started in your example script's directory, you can run
44+
```julia
45+
julia> ] activate .
46+
julia> ] add Literate
47+
julia> ] add KernelFunctions
48+
julia> # add any other example-specific dependencies
49+
```
50+
to generate the project files.
51+
52+
Make sure to commit both the `Project.toml` and the `Manifest.toml` file when you want to contribute your example in a pull request.

0 commit comments

Comments
 (0)