Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/jupyter/lang/julia/setup.jl
12923 views
1
import IJulia
2
import Base64
3
4
# The julia kernel has built in support for Revise.jl, so this is the
5
# recommended approach for long-running sessions:
6
# https://github.com/JuliaLang/IJulia.jl/blob/9b10fa9b879574bbf720f5285029e07758e50a5e/src/kernel.jl#L46-L51
7
8
# Users should enable revise within .julia/config/startup_ijulia.jl:
9
# https://timholy.github.io/Revise.jl/stable/config/#Using-Revise-automatically-within-Jupyter/IJulia-1
10
11
# clear console history
12
IJulia.clear_history()
13
14
fig_width = {fig_width}
15
fig_height = {fig_height}
16
fig_format = :{fig_format}
17
fig_dpi = {fig_dpi}
18
19
# no retina format type, use svg for high quality type/marks
20
if fig_format == :retina
21
fig_format = :svg
22
elseif fig_format == :pdf
23
fig_dpi = 96
24
# Enable PDF support for IJulia
25
IJulia.register_mime(MIME("application/pdf"))
26
end
27
28
# convert inches to pixels
29
fig_width = fig_width * fig_dpi
30
fig_height = fig_height * fig_dpi
31
32
# Intialize Plots w/ default fig width/height
33
try
34
import Plots
35
36
# Plots.jl doesn't support PDF output for versions < 1.28.1
37
# so use png (if the DPI remains the default of 300 then set to 96)
38
if (Plots._current_plots_version < v"1.28.1") & (fig_format == :pdf)
39
Plots.gr(size=(fig_width, fig_height), fmt = :png, dpi = fig_dpi)
40
else
41
Plots.gr(size=(fig_width, fig_height), fmt = fig_format, dpi = fig_dpi)
42
end
43
catch e
44
# @warn "Plots init" exception=(e, catch_backtrace())
45
end
46
47
# Initialize CairoMakie with default fig width/height
48
try
49
import CairoMakie
50
51
# CairoMakie's display() in PDF format opens an interactive window
52
# instead of saving to the ipynb file, so we don't do that.
53
# https://github.com/quarto-dev/quarto-cli/issues/7548
54
if fig_format == :pdf
55
CairoMakie.activate!(type = "png")
56
else
57
CairoMakie.activate!(type = string(fig_format))
58
end
59
CairoMakie.update_theme!(resolution=(fig_width, fig_height))
60
catch e
61
# @warn "CairoMakie init" exception=(e, catch_backtrace())
62
end
63
64
# Set run_path if specified
65
try
66
run_path = "{run_path}"
67
if !isempty(run_path)
68
run_path = String(Base64.base64decode(run_path))
69
cd(run_path)
70
end
71
catch e
72
@warn "Run path init:" exception=(e, catch_backtrace())
73
end
74
75
76
# emulate old Pkg.installed beahvior, see
77
# https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/9
78
import Pkg
79
function isinstalled(pkg::String)
80
any(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()))
81
end
82
83
# ojs_define
84
if isinstalled("JSON") && isinstalled("DataFrames")
85
import JSON, DataFrames
86
global function ojs_define(; kwargs...)
87
convert(x) = x
88
convert(x::DataFrames.AbstractDataFrame) = Tables.rows(x)
89
content = Dict("contents" => [Dict("name" => k, "value" => convert(v)) for (k, v) in kwargs])
90
tag = "<script type='ojs-define'>$(JSON.json(content))</script>"
91
IJulia.display(MIME("text/html"), tag)
92
end
93
elseif isinstalled("JSON")
94
import JSON
95
global function ojs_define(; kwargs...)
96
content = Dict("contents" => [Dict("name" => k, "value" => v) for (k, v) in kwargs])
97
tag = "<script type='ojs-define'>$(JSON.json(content))</script>"
98
IJulia.display(MIME("text/html"), tag)
99
end
100
else
101
global function ojs_define(; kwargs...)
102
@warn "JSON package not available. Please install the JSON.jl package to use ojs_define."
103
end
104
end
105
106
107
# don't return kernel dependencies (b/c Revise should take care of dependencies)
108
nothing
109
110