Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions GeneralisedFilters/docs/literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,18 @@ end

const SCRIPTJL = joinpath(EXAMPLEPATH, "script.jl")

# The example's declared dependencies (Literate is build-only), so the notebook can install
# everything it needs on Colab — including packages used only by included helper files.
function example_packages()
deps = get(Pkg.TOML.parsefile(joinpath(EXAMPLEPATH, "Project.toml")), "deps", Dict())
return sort(filter(!=("Literate"), collect(keys(deps))))
end

# Notebook postprocess: use a generic Julia kernel (so Colab picks its default Julia
# runtime, with no pinned version) and prepend a single Pkg.add for the example's
# dependencies so the notebook runs on a fresh Colab runtime.
# Notebook postprocess: only what can't be expressed as `#nb` source lines — a generic Julia
# kernel (so Colab picks its default Julia runtime, with no pinned version) and dropping the
# Documenter-only `#hide` lines Literate otherwise leaves in the notebook. The Colab
# dependency setup lives in the example script as `#nb` lines.
function prepare_notebook(nb)
nb["metadata"]["kernelspec"] = Dict(
"display_name" => "Julia", "language" => "julia", "name" => "julia"
)
# `#hide` is a Documenter-only directive that Literate leaves in the notebook; drop
# those lines (e.g. the repo-relative paths that only work during the docs build).
for cell in nb["cells"]
cell["cell_type"] == "code" || continue
cell["source"] = filter(line -> !endswith(rstrip(line), "#hide"), cell["source"])
end
packages = example_packages()
if !isempty(packages)
setup = Dict(
"cell_type" => "code",
"execution_count" => nothing,
"metadata" => Dict(),
"outputs" => [],
"source" =>
["import Pkg\n", string("Pkg.add([", join(repr.(packages), ", "), "])")],
)
idx = findfirst(cell -> cell["cell_type"] == "code", nb["cells"])
insert!(nb["cells"], something(idx, lastindex(nb["cells"]) + 1), setup)
end
return nb
end

Expand Down
15 changes: 15 additions & 0 deletions GeneralisedFilters/examples/trend-inflation/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
# Watson, 2016) using GeneralisedFilters to define a heirarchical model for use in Rao-
# Blackwellised particle filtering.

#nb # Install dependencies so the notebook runs on a fresh Colab runtime. GeneralisedFilters
#nb # and SSMProblems are added from the repo's main branch (registered versions may be too
#nb # old, e.g. lack ReferenceTrajectory):
#nb import Pkg, Downloads
#nb Downloads.download(
#nb "https://raw.githubusercontent.com/TuringLang/SSMProblems.jl/main/GeneralisedFilters/examples/trend-inflation/Project.toml",
#nb "Project.toml",
#nb )
#nb Pkg.activate(".")
#nb Pkg.add([
#nb Pkg.PackageSpec(; url="https://github.com/TuringLang/SSMProblems.jl", subdir="GeneralisedFilters", rev="main"),
#nb Pkg.PackageSpec(; url="https://github.com/TuringLang/SSMProblems.jl", subdir="SSMProblems", rev="main"),
#nb ])
#nb Pkg.instantiate()

using GeneralisedFilters
using SSMProblems
using Distributions
Expand Down
29 changes: 4 additions & 25 deletions SSMProblems/docs/literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,18 @@ end

const SCRIPTJL = joinpath(EXAMPLEPATH, "script.jl")

# The example's declared dependencies (Literate is build-only), so the notebook can install
# everything it needs on Colab — including packages used only by included helper files.
function example_packages()
deps = get(Pkg.TOML.parsefile(joinpath(EXAMPLEPATH, "Project.toml")), "deps", Dict())
return sort(filter(!=("Literate"), collect(keys(deps))))
end

# Notebook postprocess: use a generic Julia kernel (so Colab picks its default Julia
# runtime, with no pinned version) and prepend a single Pkg.add for the example's
# dependencies so the notebook runs on a fresh Colab runtime.
# Notebook postprocess: only what can't be expressed as `#nb` source lines — a generic Julia
# kernel (so Colab picks its default Julia runtime, with no pinned version) and dropping the
# Documenter-only `#hide` lines Literate otherwise leaves in the notebook. The Colab
# dependency setup lives in the example script as `#nb` lines.
function prepare_notebook(nb)
nb["metadata"]["kernelspec"] = Dict(
"display_name" => "Julia", "language" => "julia", "name" => "julia"
)
# `#hide` is a Documenter-only directive that Literate leaves in the notebook; drop
# those lines (e.g. the repo-relative paths that only work during the docs build).
for cell in nb["cells"]
cell["cell_type"] == "code" || continue
cell["source"] = filter(line -> !endswith(rstrip(line), "#hide"), cell["source"])
end
packages = example_packages()
if !isempty(packages)
setup = Dict(
"cell_type" => "code",
"execution_count" => nothing,
"metadata" => Dict(),
"outputs" => [],
"source" =>
["import Pkg\n", string("Pkg.add([", join(repr.(packages), ", "), "])")],
)
idx = findfirst(cell -> cell["cell_type"] == "code", nb["cells"])
insert!(nb["cells"], something(idx, lastindex(nb["cells"]) + 1), setup)
end
return nb
end

Expand Down
13 changes: 12 additions & 1 deletion SSMProblems/examples/kalman-filter/script.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# # Kalman Filter
#
# This example implements a Kalman filter for a linear Gaussian state space model using the
# SSMProblems interface.
# SSMProblems interface.

#nb # Install dependencies so the notebook runs on a fresh Colab runtime. SSMProblems is
#nb # added from the repo's main branch (the registered version may be too old):
#nb import Pkg, Downloads
#nb Downloads.download(
#nb "https://raw.githubusercontent.com/TuringLang/SSMProblems.jl/main/SSMProblems/examples/kalman-filter/Project.toml",
#nb "Project.toml",
#nb )
#nb Pkg.activate(".")
#nb Pkg.add(Pkg.PackageSpec(; url="https://github.com/TuringLang/SSMProblems.jl", subdir="SSMProblems", rev="main"))
#nb Pkg.instantiate()

using AbstractMCMC
using Distributions
Expand Down
Loading