Skip to content

Commit

Permalink
Document using Requires.jl for extending in 3rd-party packages
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Jun 6, 2024
1 parent 85f6510 commit ef91163
Showing 1 changed file with 8 additions and 32 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,39 +129,19 @@ method of the extension that run at different points during notebook execution.

As discussed above `QuartoNotebookWorker` is implemented as a full Julia package
rather than just a `Module` loaded into the worker processes. This allows for
any package to extend the functionality provided by the worker via Julia's
[package extension mechanism][package-extensions]. For example, given a package
called `PackageName` you could create a new package extension in
`PackageName/ext/PackageNameQuartoNotebookWorkerExt.jl` with contents

[package-extensions]: https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions)
any package to extend the functionality provided by the worker. To do this make
use `Requires.jl` (or the mechanism that it leverages) to load extension code
that requires `QuartoNotebookWorker`.

```julia
module PackageNameQuartoNotebookWorkerExt

import PackageName
import QuartoNotebookWorker

# ... Extension code here ...

function __init__()
@require QuartoNotebookWorker="38328d9c-a911-4051-bc06-3f7f556ffeda" include("extension.jl")
end
```

and update the `Project.toml` file for the `PackageName` package to include the
following extension configuration:

```toml
[weakdeps]
QuartoNotebookWorker = "38328d9c-a911-4051-bc06-3f7f556ffeda"

[extensions]
PackageNameQuartoNotebookWorkerExt = "QuartoNotebookWorker"
```

With these additions whenever `PackageName` is loaded into a `.qmd` file that is
being run with `engine: julia` the extension code in the
`PackageNameQuartoNotebookWorkerExt` module will be loaded. Below are the
available interfaces that are can be extended.
With this addition whenever `PackageName` is loaded into a `.qmd` file that is
being run with `engine: julia` the extension code in the `extension.jl` file
will be loaded. Below are the available interfaces that are can be extended.

#### `expand`

Expand All @@ -176,17 +156,13 @@ The below example shows how to create a `Replicate` type that will be expanded
into `n` cells of the same value.

```julia
module PackageNameQuartoNotebookWorkerExt

import PackageName
import QuartoNotebookWorker

function QuartoNotebookWorker.expand(r::PackageName.Replicate)
# Return a list of notebook `Cell`s to be rendered.
return [QuartoNotebookWorker.Cell(r.value) for _ in 1:r.n]
end

end
```

Where `PackageName` itself defines the `Replicate` type as
Expand Down

0 comments on commit ef91163

Please sign in to comment.