Skip to content
Draft
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
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The **Gnuplot.jl** package allows easy and fast use of [gnuplot](http://gnuplot.

- export to a huge number of formats such as `pdf`, `png`, `gif`, ``\LaTeX``, `svg`, etc. (actually all those supported by gnuplot);

- compatibility with Jupyter and Juno;
- compatibility with Jupyter, VS Code, Pluto, Juno, Weave.jl, etc.

- save sessions into gnuplot scripts, to enable easy plot customization and reproducibility.

Expand Down
8 changes: 3 additions & 5 deletions docs/src/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ saveas(file) = save(term="pngcairo size 550,350 fontscale 0.8", output="assets/$

The display behaviour of **Gnuplot.jl** depends on the value of the `Gnuplot.options.gpviewer` flag:

- if `true` the plot is displayed in a gnuplot window, using one of the interactive terminals such as `wxt`, `qt` or `aqua`. This is the default setting when running a Julia REPL session; The terminal options can be customized using `Gnuplot.options.term`;

- if `false` the plot is displayed through the Julia [multimedia interface](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O-1), i.e. it is exported as either a `png`, `svg` or `html` file, and displayed in an external viewer. This is the default setting when running a Jupyter, JupyterLab or Juno session. The terminal options can be customized using the `Gnuplot.options.mime` dictionary.

The `Gnuplot.options.gpviewer` flag is automatically set when the package is first loaded according to the runtime environment, however the user can change its value at any time to fit specific needs. Further informations and examples for both options are available in this Jupyter [notebook](https://github.com/gcalderone/Gnuplot.jl/blob/gh-pages/v1.3.0/options/display.ipynb).
- if `false` (the default) the plot is displayed through the Julia [multimedia interface](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O-1), i.e. depending on the current environment, it is displayed either in a gnuplot window (REPL) or exported as either a `png`, `svg` or `html` file, and displayed in an external viewer (IDE, notebook). The terminal options for the gnuplot window can be customized using `Gnuplot.options.term`; options for exporting can be customized using the `Gnuplot.options.mime` dictionary.

- if `true` the plot is always displayed in a gnuplot window, using one of the interactive terminals such as `wxt`, `qt` or `aqua`. The terminal options can be customized using `Gnuplot.options.term`;

Further information and examples for both options are available in this Jupyter [notebook](https://github.com/gcalderone/Gnuplot.jl/blob/gh-pages/v1.3.0/options/display.ipynb).


# Package options and initialization
Expand Down
63 changes: 39 additions & 24 deletions src/Gnuplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Structure containing the package global options, accessible through `Gnuplot.opt
- `default::Symbol`: default session name (default: `:default`)
- `term::String`: default terminal for interactive use (default: empty string, i.e. use gnuplot settings);
- `mime::Dict{DataType, String}`: dictionary of MIME types and corresponding gnuplot terminals. Used to export images with either [`save()`](@ref) or `show()` (see [Display options](@ref));
- `gpviewer::Bool`: use a gnuplot terminal as main plotting device (if `true`) or an external viewer (if `false`);
- `gpviewer::Bool`: force using a gnuplot terminal as main plotting device (if `true`) or use Julia multimedia I/O, which automatically selects between the gnuplot terminal and external viewers (if `false`);
- `init::Vector{String}`: commands to initialize the session when it is created or reset (e.g., to set default palette);
- `verbose::Bool`: verbosity flag (default: `false`)
- `preferred_format::Symbol`: preferred format to send data to gnuplot. Value must be one of:
Expand Down Expand Up @@ -236,14 +236,15 @@ const sessions = OrderedDict{Symbol, Session}()
const options = Options()

function __init__()
# Check whether we are running in an IJulia, Juno, VSCode or Pluto session.
# (copied from Gaston.jl).
options.gpviewer = !(
((isdefined(Main, :IJulia) && Main.IJulia.inited) ||
(isdefined(Main, :Juno) && Main.Juno.isactive()) ||
(isdefined(Main, :VSCodeServer)) ||
(isdefined(Main, :PlutoRunner)) )
)
# Copied from Plots - insert GnuplotDisplay before text displays,
# but after graphic displays such as IJulia.
insert!(Base.Multimedia.displays, findlast(x -> x isa Base.TextDisplay || x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
atreplinit(i -> begin
while GnuplotDisplay() in Base.Multimedia.displays
popdisplay(GnuplotDisplay())
end
insert!(Base.Multimedia.displays, findlast(x -> x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
end)
if isdefined(Main, :VSCodeServer)
# VS Code shows "dynamic" plots with fixed and small size :-(
options.mime[MIME"image/svg+xml"] = replace(options.mime[MIME"image/svg+xml"], "dynamic" => "")
Expand Down Expand Up @@ -585,6 +586,10 @@ function gp_write_table(args...; kw...)
reset(gp)
gpexec(sid, "set term unknown")
driver(sid, "set table '$tmpfile'", args...; kw...)
if !Gnuplot.options.gpviewer
# driver() did not send plots to the gnuplot process - do it now
execall(gp, term="unknown")
end
gpexec(sid, "unset table")
quit(sid)
out = readlines(tmpfile)
Expand Down Expand Up @@ -766,21 +771,20 @@ function reset(gp::Session)
gpexec(gp, "set output")
gpexec(gp, "reset session")

if options.gpviewer
# Use gnuplot viewer
(options.term != "") && gpexec(gp, "set term " * options.term)

# Set window title (if not already set)
term = writeread(gp, "print GPVAL_TERM")[1]
if term in ("aqua", "x11", "qt", "wxt")
opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
if findfirst("title", opts) == nothing
writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'")
end
# Configure the gnuplot viewer. If options.gpviewer is false, it would be more
# appropriate to do this in display(), but doing so leads to hang of the gnuplot
# process in some situations (after manual close of the Qt window, gnuplot
# receives the CAPTURE_END marker, but does not print it back). Therefore, we do
# in here unconditionally.
(options.term != "") && gpexec(gp, "set term " * options.term)

# Set window title (if not already set)
term = writeread(gp, "print GPVAL_TERM")[1]
if term in ("aqua", "x11", "qt", "wxt")
opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
if findfirst("title", opts) == nothing
writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'")
end
else
# Use external viewer
gpexec(gp, "set term unknown")
end

# Note: the reason to keep Options.term and .init separate are:
Expand Down Expand Up @@ -1547,10 +1551,21 @@ end


# ╭───────────────────────────────────────────────────────────────────╮
# │ Interfacing Julia's show
# │ Interfacing Julia's Multimedia I/O
# ╰───────────────────────────────────────────────────────────────────╯
# --------------------------------------------------------------------

struct GnuplotDisplay <: AbstractDisplay end

function Base.display(d::GnuplotDisplay, obj::SessionID)
if !options.gpviewer && obj.dump
gp = getsession(obj.sid)
execall(gp)
else
throw(MethodError(display, (d, obj)))
end
end

function show(obj::SessionID)
gp = getsession(obj.sid)
@info "Gnuplot session" sid=gp.sid datasets=length(gp.datas) plots=length(gp.plots)
Expand Down
22 changes: 21 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ try
catch
Gnuplot.options.dry = true
end
Gnuplot.options.gpviewer = true

# Test the default, i.e. false
#Gnuplot.options.gpviewer = true

x = [1, 2, 3]
y = [4, 5, 6]
Expand Down Expand Up @@ -110,6 +112,24 @@ dummy = terminals()
# Force unknown on Travis CI
Gnuplot.options.term = "unknown"

if Gnuplot.options.gpviewer == false
# Wrap @gp and @gsp macrocalls in display() to ensure that the whole
# plot is sent to the gnuplot process during testing. In interactive
# sessions display() is called by the REPL.
macro gp(args...)
gpcall = :(Gnuplot.@gp)
push!(gpcall.args, args...)
out = :(display($gpcall))
return esc(out)
end
macro gsp(args...)
gpcall = :(Gnuplot.@gsp)
push!(gpcall.args, args...)
out = :(display($gpcall))
return esc(out)
end
end

@gp 1:9
@info "using terminal: " terminal()
#test_terminal("unknown")
Expand Down