Hello,
I’m having some trouble getting a basic example of package extensions working. I might’ve missed it, but there is no “starting from zero” example of how to create package extensions in the docs here. So hopefully someone can help me understand how to do this properly.
I want to use package extensions to add CairoMakie plotting support for my simple package. Here is my simple package defined in src/MyPackage.jl
.
module MyPackage
export MyObj
struct MyObj
a::Vector{Float64}
U::Float64
L::Float64
end
end # module MyPackage
Now I want to add CairoMakie support by defining lines()
for MyObj
. So in ext/
I create PlotsExt.jl
.
module PlotsExt
using MyPackage, CairoMakie, LinearAlgebra
function CairoMakie.lines(obj::MyObj)
n = length(obj.a)
x = collect(LinRange(obj.L, obj.U, n))
y = obj.a
return fig, ax, ln = lines([x y])
end
end # module
I don’t think I am ready to start using this package yet though, right? I need to update the Project.toml
file. I need to manually add the following
[extensions]
PlotsExt = "CairoMakie"
And so then my Project.toml
looks like this.
name = "MyPackage"
uuid = "..."
authors = ...
version = "0.1.0"
[extensions]
PlotsExt = "CairoMakie"
At this point though I am unsure I’m done? I think I still need to add CairoMakie as a dependency. So then I do that and now my Project.toml
looks like this
name = "MyPackage"
uuid = "..."
authors = ...
version = "0.1.0"
[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
[extensions]
PlotsExt = "CairoMakie"
Hm, so the example in the link above says weakdeps
. Am I supposed to manually change that or does Pkg do it? For now I’ll just do it myself.
name = "MyPackage"
uuid = "..."
authors = ...
version = "0.1.0"
[weakdeps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
[extensions]
PlotsExt = "CairoMakie"
I go to my Example environment and add my local package.
Pkg.develop(path="MyPackage/")
I also add CairoMakie
.
Finally I try to run the following example script.
using MyPackage
L = 10
M1 = MyObj([(x / L)^2 for x in 1:10], 1, 10)
using CairoMakie
tmp = lines(M1)
I get the following error
`Makie.convert_arguments` for the plot type Lines{Tuple{MyObj}} and its conversion trait PointBased() was unsuccessful.
Any hints on where I went wrong here? Or the proper workflow to utilize package extensions?