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
30 changes: 14 additions & 16 deletions GeneralisedFilters/docs/literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,27 @@ end

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

# Top-level `using`/`import` package names, so the notebook can install them on Colab.
function script_packages(path)
pkgs = String[]
for line in eachline(path)
m = match(r"^\s*(?:using|import)\s+(.+)", line)
m === nothing && continue
for part in split(first(split(m.captures[1], ':')), ',')
token = first(split(strip(part), r"\s+as\s+"))
name = strip(first(split(token, '.')))
isempty(name) || name in pkgs || push!(pkgs, String(name))
end
end
return sort(pkgs)
# 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 all imported packages
# so the notebook runs on a fresh Colab runtime.
# 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.
function prepare_notebook(nb)
nb["metadata"]["kernelspec"] = Dict(
"display_name" => "Julia", "language" => "julia", "name" => "julia"
)
packages = script_packages(SCRIPTJL)
# `#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",
Expand Down
9 changes: 8 additions & 1 deletion GeneralisedFilters/examples/trend-inflation/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ using PDMats
const GF = GeneralisedFilters

INFL_PATH = joinpath(@__DIR__, "..", "..", "..", "examples", "trend-inflation"); #hide
# INFL_PATH = joinpath(@__DIR__)
include(joinpath(INFL_PATH, "utilities.jl")); #hide
#nb # Download the helper script and data so the notebook is self-contained on Colab:
#nb using Downloads
#nb INFL_URL = "https://raw.githubusercontent.com/TuringLang/SSMProblems.jl/main/GeneralisedFilters/examples/trend-inflation"
#nb for f in ("utilities.jl", "data.csv")
#nb isfile(f) || Downloads.download("$(INFL_URL)/$(f)", f)
#nb end
#nb INFL_PATH = pwd()
#nb include(joinpath(INFL_PATH, "utilities.jl"))

# ## Model Definition

Expand Down
30 changes: 14 additions & 16 deletions SSMProblems/docs/literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,27 @@ end

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

# Top-level `using`/`import` package names, so the notebook can install them on Colab.
function script_packages(path)
pkgs = String[]
for line in eachline(path)
m = match(r"^\s*(?:using|import)\s+(.+)", line)
m === nothing && continue
for part in split(first(split(m.captures[1], ':')), ',')
token = first(split(strip(part), r"\s+as\s+"))
name = strip(first(split(token, '.')))
isempty(name) || name in pkgs || push!(pkgs, String(name))
end
end
return sort(pkgs)
# 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 all imported packages
# so the notebook runs on a fresh Colab runtime.
# 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.
function prepare_notebook(nb)
nb["metadata"]["kernelspec"] = Dict(
"display_name" => "Julia", "language" => "julia", "name" => "julia"
)
packages = script_packages(SCRIPTJL)
# `#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",
Expand Down
Loading