diff --git a/GeneralisedFilters/docs/literate.jl b/GeneralisedFilters/docs/literate.jl index abee89f3..fb1e9ff3 100644 --- a/GeneralisedFilters/docs/literate.jl +++ b/GeneralisedFilters/docs/literate.jl @@ -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 diff --git a/GeneralisedFilters/examples/trend-inflation/script.jl b/GeneralisedFilters/examples/trend-inflation/script.jl index 966c8e44..34b97961 100644 --- a/GeneralisedFilters/examples/trend-inflation/script.jl +++ b/GeneralisedFilters/examples/trend-inflation/script.jl @@ -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 diff --git a/SSMProblems/docs/literate.jl b/SSMProblems/docs/literate.jl index cc1196b3..61e70dcb 100644 --- a/SSMProblems/docs/literate.jl +++ b/SSMProblems/docs/literate.jl @@ -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 diff --git a/SSMProblems/examples/kalman-filter/script.jl b/SSMProblems/examples/kalman-filter/script.jl index d924f69a..7d7536b4 100644 --- a/SSMProblems/examples/kalman-filter/script.jl +++ b/SSMProblems/examples/kalman-filter/script.jl @@ -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