# Integrating Plots into a package

**URL:** <https://discourse.julialang.org/t/integrating-plots-into-a-package/119599>\
**Category:** General Usage\
**Tags:** plots, package-extensions\
**Created:** [September 19, 2024, 12:55pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599 "2024-09-19T12:55:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [September 19, 2024, 12:55pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/1 "2024-09-19T12:55:08Z")

</div>

Hi everyone,

I recently started with the development of [ExactFieldSolutions](https://github.com/tduretz/ExactFieldSolutions.jl).  
The package aims at providing full-field exact solutions for various PDEs, which can then be used for verifying the accuracy of numerical solutions.

The package has Plots.jl as dependency, as it is being use in part of the scripts for examples of visualisation (in folder `visualisations`) and examples of convergence tests (in folder `benchmarks`) .

I had the request to make Plots.jl an extension. It makes sense as common usage would not require any visualisation. Users may not want to load plots at all (running on a cluster) or may rely on other visualisations tools that exports visualisation functions with similar name.

For this, I’ve moved the `benchmarks` and `visualisations` folder into an `ext` folder. Then, Project.toml was edited [that way](https://github.com/tduretz/ExactFieldSolutions.jl/blob/main/Project.toml). The hope is that if the user employs any of the scripts located in any of these 2 folders, then Plots.jl would be installed. Unfortunately, this create problems, running a script from folder `visualisation` complains about Plots.jl not being installed. On the other hand, running a script from folder `benchmarks` attempts to install Plots but then complains about GR\_jll…

What would be the best way to handle this kind of situation? Is there something obviously wrong?

Many thanks in advance for your advices!

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 19, 2024, 2:44pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/2 "2024-09-19T14:44:40Z")

</div>

Extensions don’t sound like the right tool for this problem at all. The usual approach would be to have a separate environment for the examples, where both the `ExactFieldSolutions` and `Plots` packages would be available.

---

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [September 19, 2024, 6:17pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/3 "2024-09-19T18:17:13Z")

</div>

Thanks for the suggestion. I didn’t know that a package could host several environments internally. This could be a good solution. I will give it a try!

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 19, 2024, 6:45pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/4 "2024-09-19T18:45:05Z")

</div>

I use the test environment for this purpose, but there might be better ways.

If you want to activate it, use:

```julia
using TestEnv; TestEnv.activate()

```

In my examples that need plotting I have the code:

```julia
  using Pkg
  if ! ("ControlPlots" ∈ keys(Pkg.project().dependencies))
      using TestEnv; TestEnv.activate()
  end
  using ControlPlots

```

and in Project.toml I have:

```julia
[extras]
ControlPlots = "23c2ee80-7a9e-4350-b264-8e670f12517c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "ControlPlots"]

```

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [September 19, 2024, 8:34pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/5 "2024-09-19T20:34:19Z")

</div>

Can we think of a title that will make this answer more searchable in the future? Say, “Integrating Plots into a Package” or something even better?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [September 19, 2024, 9:14pm UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/6 "2024-09-19T21:14:51Z")

</div>

Maybe “What’s the best way to demonstrate my package using Plots.jl?”?

---

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [September 20, 2024, 5:15am UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/7 "2024-09-20T05:15:47Z")

</div>

Well, in fact the problem would rather be: _how to demonstrate a package, when demonstration requires dependencies that are not essential to the core of a package_. I took Plots as an example but other packages may be concerned. I have just tested the solution of @GunnarFarneback and this works. It allowed to remove Plots, Symbolics and SparseDiffTools from the environment of the core package. Now I have to see how nice it is in practice for further developing the package (i.e. I like visualising when I add new solution, so this may require switching environments back and forth). Thanks a lot for the many suggestions!

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 20, 2024, 6:59am UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/8 "2024-09-20T06:59:22Z")

</div>

For the future you probably want to check out the [Pkg workspace feature](https://pkgdocs.julialang.org/dev/toml-files/#The-%5Bworkspace%5D-section), but I’m not sure whether this will be available in Julia 1.11 or 1.12.

---

<div class="post-metadata">

**Author:** ![tduretz](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@tduretz](https://discourse.julialang.org/u/tduretz)\
**Post date:** [September 25, 2024, 6:51am UTC](https://discourse.julialang.org/t/integrating-plots-into-a-package/119599/9 "2024-09-25T06:51:13Z")

</div>

thanks again everyone for your help. I’ve tagged the first answer of @GunnarFarneback as the solution - [ExactFieldSolutions.jl](https://github.com/tduretz/ExactFieldSolutions.jl) now uses this approach. I will definitely try out the workspace feature in the future, this seems indeed ideally designed.
