Skip to content

Commit 4889c4d

Browse files
Merge pull request #17 from AtelierArith/terasaki/gnuplot
Add example of using `gnuplot`
2 parents cde2d62 + 8ca31d9 commit 4889c4d

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

examples/gnuplot.jl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
### A Pluto.jl notebook ###
2+
# v0.20.3
3+
4+
using Markdown
5+
using InteractiveUtils
6+
7+
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
8+
macro bind(def, element)
9+
#! format: off
10+
quote
11+
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
12+
local el = $(esc(element))
13+
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
14+
el
15+
end
16+
#! format: on
17+
end
18+
19+
# ╔═╡ 07ebdbbe-49bb-4d18-9f2b-14f98d137548
20+
begin
21+
using Pkg
22+
Pkg.activate(temp = true)
23+
Pkg.add("Images")
24+
Pkg.develop(path = dirname(@__DIR__))
25+
using PlutoMonacoEditor: MonacoEditor
26+
using Images
27+
end
28+
29+
# ╔═╡ 9aa40f8c-a955-47a3-8e6d-4ac54d1dc330
30+
md"""
31+
# gnuplot editor on Pluto Notebook
32+
33+
To run this notebook, you need to install the `gnuplot` command.
34+
"""
35+
36+
# ╔═╡ bf232d99-3001-4aac-afb7-1321c7407666
37+
begin
38+
targetfile = joinpath(dirname(first(split(@__FILE__, ".jl#==#"))), "main.plt")
39+
initCode = join(readlines(targetfile), '\n')
40+
codefence = "python" # hack
41+
@bind sourcecode MonacoEditor(codefence, initCode, height = 250)
42+
end
43+
44+
# ╔═╡ dce616d7-fdfe-4854-919d-420603ebc875
45+
let
46+
shouldrun = false
47+
if !ismissing(sourcecode)
48+
if initCode != sourcecode
49+
# Update file
50+
write(targetfile, sourcecode)
51+
# If updated, we should run code
52+
shouldrun = true
53+
end
54+
if shouldrun
55+
mktempdir() do d
56+
write(joinpath(d, targetfile), sourcecode)
57+
p = pwd()
58+
try
59+
cd(d)
60+
run(`gnuplot $(joinpath(d, targetfile))`)
61+
img = load(joinpath(d, "main.png"))
62+
img
63+
catch e
64+
e isa ProcessFailedException || rethrow(e)
65+
finally
66+
cd(p)
67+
end
68+
end
69+
end
70+
end
71+
end
72+
73+
# ╔═╡ b223077c-8a38-416b-998d-c6fdb274512b
74+
md"""
75+
Note that the following code runs behind the scenes.
76+
77+
```julia
78+
let
79+
shouldrun = false
80+
if !ismissing(sourcecode)
81+
if initCode != sourcecode
82+
# Update file
83+
write(targetfile, sourcecode)
84+
# If updated, we should run code
85+
shouldrun = true
86+
end
87+
if shouldrun
88+
mktempdir() do d
89+
write(joinpath(d, targetfile), sourcecode)
90+
p = pwd()
91+
try
92+
cd(d)
93+
run(`gnuplot $(joinpath(d, targetfile))`)
94+
img = load(joinpath(d, "main.png"))
95+
img
96+
catch e
97+
e isa ProcessFailedException || rethrow(e)
98+
finally
99+
cd(p)
100+
end
101+
end
102+
end
103+
end
104+
end
105+
```
106+
"""
107+
108+
# ╔═╡ Cell order:
109+
# ╠═07ebdbbe-49bb-4d18-9f2b-14f98d137548
110+
# ╟─9aa40f8c-a955-47a3-8e6d-4ac54d1dc330
111+
# ╟─bf232d99-3001-4aac-afb7-1321c7407666
112+
# ╟─dce616d7-fdfe-4854-919d-420603ebc875
113+
# ╟─b223077c-8a38-416b-998d-c6fdb274512b

examples/main.plt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set terminal pngcairo enhanced font "Arial,12"
2+
set output "main.png" # Do not change
3+
set xrange [-2*pi:2*pi]
4+
set title "sin(x) and cos(x)"
5+
set xlabel "x"
6+
set ylabel "y"
7+
set grid
8+
plot sin(x) with lines title "sin(x)", cos(x) with lines title "cos(x)"
9+
set output # close file
10+

0 commit comments

Comments
 (0)